using System; using System.Text; using System.Collections.Generic; using System.Runtime.InteropServices; namespace ocean { public enum EM_GROUP {//权限类别 EUser_Level0 = 0, //未登录 EUser_Level1, //OP EUser_Level2, //admin EUser_Level3, //system EUser_Level4, //root }; public struct TUser {//用户信息 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string name; public uint group; public uint limit; }; public struct TUserLimit {//用户权限 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string name; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string desc; }; public class user { /**************************************************** *Function: 初始化模块 *Intput: NULL *Output: NULL *Return: 成功返回0, 失败返回错误码 ******************************************************/ [DllImport(@"user.dll", EntryPoint = "user_init", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern int init(); /**************************************************** *Function: 释放模块 资源 *Intput: NULL *Output: NULL *Return: 成功返回0, 失败返回错误码 ******************************************************/ [DllImport(@"user.dll", EntryPoint = "user_deinit", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern int deinit(); /**************************************************** *Function: 用户登陆,成功后调用get_cur_limit获取权限 *Intput: lpUser 用户名 lpPwd 密码 *Output: NULL *Return: 成功返回0, 失败返回错误码 ******************************************************/ [DllImport(@"user.dll", EntryPoint = "login", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern int login(string lpUser, string lpPwd); /**************************************************** *Function: 用户退出登陆 *Intput: 无 *Output: NULL *Return: 成功返回0, 失败返回错误码 ******************************************************/ [DllImport(@"user.dll", EntryPoint = "logout", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern int logout(); /**************************************************** *Function: 修改用户信息, 权限足够才能调用 *Intput: lpOldUser 要修改的用户名 lpNewUser 新的用户名 默认值不修改 group 新的组别 admin/system 默认值不修改 limit 新的权限 默认值不修改 *Output: NULL *Return: 成功返回0, 失败返回错误码 ******************************************************/ [DllImport(@"user.dll", EntryPoint = "change_user", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern int update(string lpOldUser, string lpNewUser, uint group = 0, uint limit = 0); /**************************************************** *Function: 修改用户密码 *Intput: lpUser 用户名 lpOldPwd 旧密码 lpNewPwd 新密码 *Output: NULL *Return: 成功返回0,失败返回错误码 ******************************************************/ [DllImport(@"user.dll", EntryPoint = "change_pwd", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern int change_pwd(string lpUser, string lpOldPwd, string lpNewPwd); /**************************************************** *Function: 恢复默认密码, 权限足够才能调用 *Intput: lpUser 用户名 *Output: NULL *Return: 成功返回0,失败返回错误码 ******************************************************/ [DllImport(@"user.dll", EntryPoint = "clear_pwd", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern int clear_pwd(string lpUser); /**************************************************** *Function: 增加一个用户 *Intput: lpUser 用户名 lpPwd 密码 group 组别 admin/system limit 权限 *Output: NULL *Return: 成功返回0,失败返回错误码 ******************************************************/ [DllImport(@"user.dll", EntryPoint = "add_user", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern int create(string lpUser, string lpPwd, uint group, uint limit); /**************************************************** *Function: 删除一个用户 *Intput: lpUser 用户名 *Output: NULL *Return: 成功返回0,失败返回错误码 ******************************************************/ [DllImport(@"user.dll", EntryPoint = "del_user", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern int remove(string lpUser); /**************************************************** *Function: 获取当前用户名 *Intput: NULL *Output: pszname 从这里返回名称 *Return: 成功返回0,失败返回错误码 ******************************************************/ [DllImport(@"user.dll", EntryPoint = "get_cur_users", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int get_cur_users(StringBuilder pszname); public static string cur_user() { StringBuilder pszname = new StringBuilder(32); get_cur_users(pszname); return pszname.ToString(); } /**************************************************** *Function: 获取当前用户组别 *Intput: NULL *Output: NULL *Return: 返回当前用户的组别 ******************************************************/ [DllImport(@"user.dll", EntryPoint = "get_user_group", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern uint cur_group(uint lpuser = 0); /**************************************************** *Function: 获取当前用户权限 *Intput: NULL *Output: NULL *Return: 返回当前用户的权限 ******************************************************/ [DllImport(@"user.dll", EntryPoint = "get_user_limit", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern uint cur_limit(uint lpuser = 0); /**************************************************** *Function: 获取用户列表 *Intput: pstUser 用户缓冲区,可以传NULL获取数量 *Output: pstUser 用户缓冲区 *Return: 成功返回0, 失败返回错误码<0 ******************************************************/ [DllImport(@"user.dll", EntryPoint = "get_user_list", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int get_user_list(IntPtr ptr); public static int GetUserCount() { return get_user_list(IntPtr.Zero); } public static int GetAllUser(string []puser) { IntPtr ptr; int len = 0; int count = 0; TUser info = new TUser(); count = get_user_list(IntPtr.Zero); if (count > 0) { len = Marshal.SizeOf(info); ptr = Marshal.AllocHGlobal(len * count); get_user_list(ptr); for (int i = 0; i < count; i++) { info = (TUser)Marshal.PtrToStructure( ptr + len * i, typeof(TUser)); puser[i] = info.name; } Marshal.FreeHGlobal(ptr); //释放非托管内存 } return count; } /**************************************************** *Function: 判断当前用户是否有某项权限 *Intput: pLimit 权限项名称 *Output: NULL *Return: 返回1标识有,0标识没有 ******************************************************/ [DllImport(@"user.dll", EntryPoint = "chk_user_limit", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern int check(string pLimit, uint lpuser = 0); /**************************************************** *Function: 增加一项权限,最多支持32项权限 *Intput: index 索引,区间[0,32) pszname 名称,不能重复 pszdesc 描述标题,显示在界面上的,若为空则显示名称 *Output: NULL *Return: 成功返回0,失败返回错误码 *PS: 在需要检测权限的代码中使用chk_user_limit函数, 可根据权限名判断当前用户是否有该项权限 ******************************************************/ [DllImport(@"user.dll", EntryPoint = "add_limit", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern int add_limit(string pLimit, string title); /**************************************************** *Function: 删除最后一项权限 *Intput: NULL *Output: NULL *Return: 成功返回0,失败返回错误码 ******************************************************/ [DllImport(@"user.dll", EntryPoint = "del_last_limit", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern int del_limit(); } }