|
|
|
|
using System;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
namespace ocean
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
#region log
|
|
|
|
|
public class log
|
|
|
|
|
{//日志类
|
|
|
|
|
/****************************************************
|
|
|
|
|
*Function: 打印log函数
|
|
|
|
|
*Intput: level打印级别,分为info1, info2, info3, warn error crit
|
|
|
|
|
pszLog 要打印内容
|
|
|
|
|
*Output: NULL
|
|
|
|
|
*Return: 成功返回0,失败返回错误码
|
|
|
|
|
******************************************************/
|
|
|
|
|
[DllImport(@"log.dll", EntryPoint = "traces", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
private static extern int trace(ELogLevel level, string pszLog);
|
|
|
|
|
public static int Crit(string pszLog)
|
|
|
|
|
{
|
|
|
|
|
return trace(ELogLevel.LEVEL_CRIT, pszLog);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int Err(string pszLog)
|
|
|
|
|
{
|
|
|
|
|
return trace(ELogLevel.LEVEL_ERR, pszLog);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int Warn(string pszLog)
|
|
|
|
|
{
|
|
|
|
|
return trace(ELogLevel.LEVEL_WARN, pszLog);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int Info1(string pszLog)
|
|
|
|
|
{
|
|
|
|
|
return trace(ELogLevel.LEVEL_INFO1, pszLog);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int Info2(string pszLog)
|
|
|
|
|
{
|
|
|
|
|
return trace(ELogLevel.LEVEL_INFO2, pszLog);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int Info3(string pszLog)
|
|
|
|
|
{
|
|
|
|
|
return trace(ELogLevel.LEVEL_INFO3, pszLog);
|
|
|
|
|
}
|
|
|
|
|
#region nouse
|
|
|
|
|
private enum ELogLevel
|
|
|
|
|
{//打印级别
|
|
|
|
|
LEVEL_CRIT = 0,
|
|
|
|
|
LEVEL_ERR,
|
|
|
|
|
LEVEL_WARN,
|
|
|
|
|
LEVEL_INFO1,
|
|
|
|
|
LEVEL_INFO2,
|
|
|
|
|
LEVEL_INFO3,
|
|
|
|
|
LEVEL_INFO4,
|
|
|
|
|
};
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region config
|
|
|
|
|
public class config
|
|
|
|
|
{//配置类
|
|
|
|
|
|
|
|
|
|
[DllImport(@"config.dll", EntryPoint = "cfg_init", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
public static extern int init();
|
|
|
|
|
|
|
|
|
|
[DllImport(@"config.dll", EntryPoint = "cfg_deinit", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
public static extern int deinit();
|
|
|
|
|
|
|
|
|
|
/****************************************************
|
|
|
|
|
*Function: 增加/修改一个配置项-跟随产品目录
|
|
|
|
|
*Intput: key 段落名 [xxx]
|
|
|
|
|
name 关键字 xxx=?
|
|
|
|
|
lpValue 值 ?=xxx
|
|
|
|
|
*Output: NULL
|
|
|
|
|
*Return: 成功返回0,失败返回错误码
|
|
|
|
|
******************************************************/
|
|
|
|
|
[DllImport(@"config.dll", EntryPoint = "modify_cfg", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
private static extern int modify_cfg(string key, string name, string lpValue);
|
|
|
|
|
public static int setstr(string key, string name, string val)
|
|
|
|
|
{
|
|
|
|
|
return modify_cfg(key, name, val);
|
|
|
|
|
}
|
|
|
|
|
public static int setint(string key, string name, int val)
|
|
|
|
|
{
|
|
|
|
|
return modify_cfg(key, name, val.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int setbool(string key,string name,bool val)
|
|
|
|
|
{
|
|
|
|
|
string v = val == true ? "1" : "0";
|
|
|
|
|
return modify_cfg(key, name, v);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int setdouble(string key, string name, double val)
|
|
|
|
|
{
|
|
|
|
|
return modify_cfg(key, name, val.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/****************************************************
|
|
|
|
|
*Function: 获取一个配置项-跟随产品目录
|
|
|
|
|
*Intput: key 段落名 [xxx]
|
|
|
|
|
name 关键字 xxx=?
|
|
|
|
|
index 配置文件索引 默认为0 最多5个
|
|
|
|
|
*Output: pval 配置值
|
|
|
|
|
*Return: 成功返回0,失败返回错误码
|
|
|
|
|
******************************************************/
|
|
|
|
|
[DllImport(@"config.dll", EntryPoint = "get_cfgs", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
private static extern int get_cfgs(string key, string name, StringBuilder pval);
|
|
|
|
|
public static string getstr(string key, string name)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder pval = new StringBuilder(256);
|
|
|
|
|
int ret = get_cfgs(key, name, pval);
|
|
|
|
|
return ret == 0 ? pval.ToString() : "";
|
|
|
|
|
}
|
|
|
|
|
public static int getint(string key, string name)
|
|
|
|
|
{
|
|
|
|
|
string str = getstr(key, name);
|
|
|
|
|
if (str == "")
|
|
|
|
|
str = "0";
|
|
|
|
|
return Convert.ToInt32(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T getvalue<T>(string key, string name)
|
|
|
|
|
{
|
|
|
|
|
string va = getstr(key, name);
|
|
|
|
|
if (string.IsNullOrEmpty(va)) return default(T);
|
|
|
|
|
return (T)Convert.ChangeType(va,typeof(T));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool getbool(string key,string name)
|
|
|
|
|
{
|
|
|
|
|
string val=getstr(key, name);
|
|
|
|
|
return val == "1";
|
|
|
|
|
}
|
|
|
|
|
public static double getdouble(string key, string name)
|
|
|
|
|
{
|
|
|
|
|
string str = getstr(key, name);
|
|
|
|
|
return str == "" ? 0.0f : Convert.ToDouble(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/****************************************************
|
|
|
|
|
*Function: 增加/修改一个配置项-系统目录data//sys.dat
|
|
|
|
|
*Intput: key 段落名 [xxx]
|
|
|
|
|
name 关键字 xxx=?
|
|
|
|
|
lpValue 值 ?=xxx
|
|
|
|
|
*Output: NULL
|
|
|
|
|
*Return: 成功返回0,失败返回错误码
|
|
|
|
|
******************************************************/
|
|
|
|
|
[DllImport(@"config.dll", EntryPoint = "modify_sys_cfg", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
private static extern int modify_sys_cfg(string key, string name, string lpValue);
|
|
|
|
|
public static int setsysstr(string key, string name, string val)
|
|
|
|
|
{
|
|
|
|
|
return modify_sys_cfg(key, name, val);
|
|
|
|
|
}
|
|
|
|
|
public static int setsysint(string key, string name, int val)
|
|
|
|
|
{
|
|
|
|
|
return modify_sys_cfg(key, name, val.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int setsysbool(string key, string name, bool val)
|
|
|
|
|
{
|
|
|
|
|
string v = val == true ? "1" : "0";
|
|
|
|
|
return modify_sys_cfg(key, name, v);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int setsysdouble(string key, string name, double val)
|
|
|
|
|
{
|
|
|
|
|
return modify_sys_cfg(key, name, val.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/****************************************************
|
|
|
|
|
*Function: 获取一个系统配置项
|
|
|
|
|
*Intput: key 段落名 [xxx]
|
|
|
|
|
name 关键字 xxx=?
|
|
|
|
|
index 配置文件索引 默认为0 最多5个
|
|
|
|
|
*Output: pval 配置值
|
|
|
|
|
*Return: 成功返回0,失败返回错误码
|
|
|
|
|
******************************************************/
|
|
|
|
|
[DllImport(@"config.dll", EntryPoint = "get_sys_cfgs", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
private static extern int get_sys_cfgs(string key, string name, StringBuilder pval);
|
|
|
|
|
public static string getsysstr(string key, string name)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder pval = new StringBuilder(256);
|
|
|
|
|
int ret = get_sys_cfgs(key, name, pval);
|
|
|
|
|
return ret == 0 ? pval.ToString() : "";
|
|
|
|
|
}
|
|
|
|
|
public static int getsysint(string key, string name)
|
|
|
|
|
{
|
|
|
|
|
string str = getsysstr(key, name);
|
|
|
|
|
if (str == "")
|
|
|
|
|
str = "0";
|
|
|
|
|
return Convert.ToInt32(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T getsysvalue<T>(string key, string name)
|
|
|
|
|
{
|
|
|
|
|
string va = getsysstr(key, name);
|
|
|
|
|
if (string.IsNullOrEmpty(va)) return default(T);
|
|
|
|
|
return (T)Convert.ChangeType(va, typeof(T));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool getsysbool(string key, string name)
|
|
|
|
|
{
|
|
|
|
|
string val = getsysstr(key, name);
|
|
|
|
|
return val == "1";
|
|
|
|
|
}
|
|
|
|
|
public static double getsysdouble(string key, string name)
|
|
|
|
|
{
|
|
|
|
|
string str = getsysstr(key, name);
|
|
|
|
|
return str == "" ? 0.0f : Convert.ToDouble(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************
|
|
|
|
|
*Function: 获取当前日志保存目录
|
|
|
|
|
*Intput: NULL
|
|
|
|
|
*Output: path 当前日志保存目录 保证256字节
|
|
|
|
|
*Return: 成功返回0 失败返回错误码
|
|
|
|
|
*PS: 该配置保存在系统配置文件
|
|
|
|
|
******************************************************/
|
|
|
|
|
[DllImport(@"config.dll", EntryPoint = "get_log_paths", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
private static extern int get_log_paths(StringBuilder path);
|
|
|
|
|
public static string get_log_path()
|
|
|
|
|
{
|
|
|
|
|
int ret = 0;
|
|
|
|
|
StringBuilder p = new StringBuilder(256);
|
|
|
|
|
ret = get_log_paths(p);
|
|
|
|
|
return (0 == ret) ? p.ToString() : "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/****************************************************
|
|
|
|
|
*Function: 与files_write功能类似,只是不能格式化字符串
|
|
|
|
|
*Intput: file 文件名 <128字节
|
|
|
|
|
pszContent 要写入的内容 <1024字节
|
|
|
|
|
npos 可选参数,默认-1为从当前位置写入
|
|
|
|
|
*Output: 无
|
|
|
|
|
*Return: 成功返回0,失败返回错误码
|
|
|
|
|
*PS: 基于日志配置目录读写
|
|
|
|
|
*PS: 此接口需要外部写入换行符
|
|
|
|
|
*PS:写入数据后,文件游标会自动移动到写入后位置
|
|
|
|
|
******************************************************/
|
|
|
|
|
[DllImport(@"config.dll", EntryPoint = "files_writes", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
public static extern int files_writes(string file, string pszContent, int npos = -1);
|
|
|
|
|
|
|
|
|
|
/****************************************************
|
|
|
|
|
*Function: 逐行读取文件内容,可以传NULL获取文件大小
|
|
|
|
|
*Intput: file 文件名/相对路径 <128字节
|
|
|
|
|
pszContent 要读取的内容缓冲区,必须外部保证缓冲区大小
|
|
|
|
|
nline 可选参数,读取第几行,从0开始?默认-1为从当前位置读取
|
|
|
|
|
*Output: 无
|
|
|
|
|
*Return: 成功返回读取到的字节大小,失败返回错误码<0,文件结尾返回0
|
|
|
|
|
*PS: 基于日志配置目录读写
|
|
|
|
|
*PS:读取数据后,文件游标会自动移动到读取后位置
|
|
|
|
|
*PS:若不传入pszContent 则返回该文件大小,但不会实际读取
|
|
|
|
|
*PS:打开文件后,游标会在最后,所以首次读取最好传入nline=0
|
|
|
|
|
*PS: 此接口会自动过滤换行符
|
|
|
|
|
******************************************************/
|
|
|
|
|
[DllImport(@"config.dll", EntryPoint = "files_read", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
private static extern int files_read(string file, IntPtr ptr, int npos = -1);
|
|
|
|
|
public static string files_reads(string file, int npos = -1)
|
|
|
|
|
{
|
|
|
|
|
IntPtr ptr;
|
|
|
|
|
int ret = 0;
|
|
|
|
|
string str = "";
|
|
|
|
|
ptr = Marshal.AllocHGlobal(1024);
|
|
|
|
|
ret = files_read(file, ptr, npos);
|
|
|
|
|
if (ret > 0)
|
|
|
|
|
{
|
|
|
|
|
str = Marshal.PtrToStringAnsi(ptr);
|
|
|
|
|
}
|
|
|
|
|
Marshal.FreeHGlobal(ptr);
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
public static bool files_exist(string file)
|
|
|
|
|
{
|
|
|
|
|
int ret = files_read(file, IntPtr.Zero);
|
|
|
|
|
return ret > 0 ? true : false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/****************************************************
|
|
|
|
|
*Function: 逐行读取文件内容,可以传NULL获取文件大小
|
|
|
|
|
*Intput: file 文件名/相对路径 <128字节
|
|
|
|
|
pszContent 要读取的内容缓冲区,必须外部保证缓冲区大小
|
|
|
|
|
nline 可选参数,读取第几行,从0开始?默认-1为从当前位置读取
|
|
|
|
|
*Output: 无
|
|
|
|
|
*Return: 成功返回读取到的字节大小,失败返回错误码<0,文件结尾返回0
|
|
|
|
|
*PS: 基于日志配置目录读写
|
|
|
|
|
*PS:读取数据后,文件游标会自动移动到读取后位置
|
|
|
|
|
*PS:若不传入pszContent 则返回该文件大小,但不会实际读取
|
|
|
|
|
*PS:打开文件后,游标会在最后,所以首次读取最好传入nline=0
|
|
|
|
|
*PS: 此接口会自动过滤换行符
|
|
|
|
|
******************************************************/
|
|
|
|
|
[DllImport(@"config.dll", EntryPoint = "files_close", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
private static extern int files_close_all(IntPtr ptr);
|
|
|
|
|
public static int close_all()
|
|
|
|
|
{
|
|
|
|
|
return files_close_all(IntPtr.Zero);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DllImport(@"config.dll", EntryPoint = "files_close", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
public static extern int files_close(string file);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region msg
|
|
|
|
|
|
|
|
|
|
public enum MessageType
|
|
|
|
|
{//消息类型
|
|
|
|
|
MSG_NORMAL = 0, //提示消息框 -- OK
|
|
|
|
|
MSG_SELECT, //选择消息框 -- 是+否
|
|
|
|
|
MSG_RETRY, //重试消息框 -- 重试+忽略+取消 / 自定义 备用
|
|
|
|
|
MSG_WARN, //警告消息框 -- OK
|
|
|
|
|
MSG_ERR, //错误消息框 -- OK
|
|
|
|
|
MSG_ASCII, //字符串消息
|
|
|
|
|
MSG_EXITSYS, //退出系统
|
|
|
|
|
MSG_RESTART, //重启系统
|
|
|
|
|
}; //ui弹窗消息类型
|
|
|
|
|
|
|
|
|
|
public enum MessageResult
|
|
|
|
|
{
|
|
|
|
|
IDR_OK = 0, //ok
|
|
|
|
|
IDR_CANCEL, //取消
|
|
|
|
|
IDR_ABORT, //忽略
|
|
|
|
|
IDR_RETRY, //重试
|
|
|
|
|
IDR_YES, //选择是
|
|
|
|
|
IDR_NO, //选择否
|
|
|
|
|
IDR_CUS, //自定义 备用
|
|
|
|
|
}; //弹窗消息返回结果
|
|
|
|
|
|
|
|
|
|
//函数指针委托说明
|
|
|
|
|
public delegate MessageResult FunMessageShow(string pszmsg, MessageType type, IntPtr lparam);
|
|
|
|
|
|
|
|
|
|
public class msg
|
|
|
|
|
{//消息类
|
|
|
|
|
/****************************************************
|
|
|
|
|
*Function: 注册消息弹窗提示,注册后可显示底层提示消息,一般ui层注册
|
|
|
|
|
*Intput: fun 回调函数地址
|
|
|
|
|
param 附加参数
|
|
|
|
|
*Output: NULL
|
|
|
|
|
*Return: 成功返回0, 失败返回错误码
|
|
|
|
|
******************************************************/
|
|
|
|
|
[DllImport(@"sysapi.dll", EntryPoint = "regist_message", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
public static extern int regist(FunMessageShow fun, IntPtr lparam);
|
|
|
|
|
|
|
|
|
|
/****************************************************
|
|
|
|
|
*Function: 反注册消息弹窗提示,反注册后会返回到上一次注册状态
|
|
|
|
|
*Intput: fun 回调函数地址
|
|
|
|
|
*Output: NULL
|
|
|
|
|
*Return: 成功返回0, 失败返回错误码
|
|
|
|
|
*PS: 在程序退出时反注册
|
|
|
|
|
******************************************************/
|
|
|
|
|
[DllImport(@"sysapi.dll", EntryPoint = "unregist_message", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
public static extern int unregist(FunMessageShow fun);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************
|
|
|
|
|
*Function: 同步回调消息,注册后可接收,一般用于UI注册接收,底层发送
|
|
|
|
|
*Intput: type 消息类型 MessageType
|
|
|
|
|
pszmsg 提示消息内容
|
|
|
|
|
*Output: NULL
|
|
|
|
|
*Return: 成功返回消息返回值(MessageResult),失败返回错误码(<0)
|
|
|
|
|
*PS: 支持多线程,内部包含线程保护,回调函数若有延时,将影响其它线程效率
|
|
|
|
|
******************************************************/
|
|
|
|
|
[DllImport(@"msg.dll", EntryPoint = "send_messages", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
public static extern int send(MessageType type, string pszmsg);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region sys
|
|
|
|
|
|
|
|
|
|
public class sys
|
|
|
|
|
{//系统函数
|
|
|
|
|
|
|
|
|
|
/****************************************************
|
|
|
|
|
*Function: 递归复制一个文件夹所有内容到另外一个文件夹
|
|
|
|
|
*Intput: src 源目标文件夹路径
|
|
|
|
|
des 拷贝到哪?目的地路径
|
|
|
|
|
ext 扩展名 "txt or dat" -- 为空则全部拷贝
|
|
|
|
|
funcall 进度回调函数,若传入参数,则会自动回调
|
|
|
|
|
*Output: NULL
|
|
|
|
|
*Return: 成功返回0, 失败返回错误码
|
|
|
|
|
*PS:阻塞操作,可能执行会比较久
|
|
|
|
|
******************************************************/
|
|
|
|
|
[DllImport(@"sysapi.dll", EntryPoint = "copy_path", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
private static extern int copy_path(string src, string des, IntPtr ext, IntPtr funcall);
|
|
|
|
|
public static int CopyPath(string src, string des)
|
|
|
|
|
{
|
|
|
|
|
return copy_path(src, des, IntPtr.Zero, IntPtr.Zero);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int CopyCsvByPath(string src, string des, string ext)
|
|
|
|
|
{
|
|
|
|
|
return copy_path(src, des, Marshal.StringToHGlobalAnsi(ext), IntPtr.Zero);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/****************************************************
|
|
|
|
|
*Function: 获取一个字符串类型
|
|
|
|
|
*Intput: pstr 待检测字符串
|
|
|
|
|
count 要检测的字符数量 为0时检测全部
|
|
|
|
|
*Output: NULL
|
|
|
|
|
*Return: double返回2 整数返回1, 其它返回0 参考EMStrType
|
|
|
|
|
******************************************************/
|
|
|
|
|
[DllImport(@"sysapi.dll", EntryPoint = "get_str_type", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
private static extern int get_str_type(string src, int count = 0);
|
|
|
|
|
public static bool is_number(string src) { return 1 == get_str_type(src); }
|
|
|
|
|
public static bool is_double(string src) { return 2 == get_str_type(src); }
|
|
|
|
|
public static bool is_string(string src) { return 0 == get_str_type(src); }
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|