Windows 用户
Wnndows 用户
1.1 创建用户
NetUserAdd API函数可以实现在系统中创建用户的功能,函数原型如下:
NET_API_STATUS NetUserAdd(
LPCWSTR servername, //用户的主机名,如果是本机设置为NULL
DWORD level, //设置为1,2,3,4决定buf参数的结构
LPBYTE buf, //USER_INFO_1, USER_INFO_2, USER_INFO_3, USER_INFO_4
LPDWORD parm_err//
);
typedef struct _USER_INFO_1 {
LPWSTR usri1_name;
LPWSTR usri1_password;
DWORD usri1_password_age;
DWORD usri1_priv;
LPWSTR usri1_home_dir;
LPWSTR usri1_comment;
DWORD usri1_flags;
LPWSTR usri1_script_path;
}USER_INFO_1, *PUSER_INFO_1, *LPUSER_INFO_1;
typedef struct _USER_INFO_2 {
LPWSTR usri2_name;
LPWSTR usri2_password;
DWORD usri2_password_age;
DWORD usri2_priv;
LPWSTR usri2_home_dir;
LPWSTR usri2_comment;
DWORD usri2_flags;
LPWSTR usri2_script_path;
DWORD usri2_auth_flags;
LPWSTR usri2_full_name;
LPWSTR usri2_usr_comment;
LPWSTR usri2_parms;
LPWSTR usri2_workstations;
DWORD usri2_last_logon;
DWORD usri2_last_logoff;
DWORD usri2_acct_expires;
DWORD usri2_max_storage;
DWORD usri2_units_per_week;
PBYTE usri2_logon_hours;
DWORD usri2_bad_pw_count;
DWORD usri2_num_logons;
LPWSTR usri2_logon_server;
DWORD usri2_country_code;
DWORD usri2_code_page;
}USER_INFO_2, *PUSER_INFO_2, *LPUSER_INFO_2;
/**************************************************
*函数名:AddUser
*功能: 增加用户
*参数: szServerName,主机名,如果是本机设为NULL
* szUserName, 用户名
* szPassword,密码
***************************************************/
int AddUser(LPWSTR szServerName, LPWSTR szUserName, LPWSTR szPassword)
{
USER_INFO ui;
DWORD dwLevel = 1; //使用USER_INFO_1作为参数
DWORD dwError = 0;
NET_API_STATUS nStatus;
//填充 USER_INFO_1
ui.usril_name = szUserName; //用户名
ui.usril_password = szPassword; //密码
ui.usril_priv = USER_PRIV_USER; //级别
ui.usril_home_dir = NULL;
ui.usril_comment = NULL;
ui.usril_flags = UF_SCRIPT;
ui.usril_script_path = NULL;
// 增加用户
nStatus = NetUserAdd(szServerName, dwLevel, (LPBYTE)&ui, &dwError);
//判断结果
if(nStatus == NERR_Success)
{
wprintf(stderr, L”User %s has been successfully added on%s\n”, szUserName, szServerName);
}
else
{
fprintf(stderr, “A system errror has occrred: %d\n”, nStatus);
}
return 0;
}
2 用户组
NetGroupAddUser和NetGroupDelUser的功能分别是将用户添加到用户组和从用户组删除用户。增加用户组和删除用户组,使用API函数:NetGroupAdd和NetGroupDel
2.1 几个函数
NET_API_STATUS NetGroupAddUser(
LPCWSTR servername,
LPCWSTR GroupName,
LPCWSTR username );
NET_API_STATUS NetGroupDelUser(
LPCWSTR servername,
LPCWSTR GroupName,
LPCWSTR Username);
NET_API_STATUS NetGroupAdd(
LPCWSTR servername,
DWORD level,
LPBYTE buf,
LPDWORD parm_err );
NET_API_STATUS NetGroupDel(
LPCWSTR servername,
LPCWSTR groupname );
typedef struct _GROUP_INFO_1 {
LPWSTR grpi1_name;
LPWSTR grpi1_comment;
}GROUP_INFO_1, *PGROUP_INFO_1, *LPGROUP_INFO_1;
/*****************************************************
* 函数名; AddUserToGroup
* 功能: 为用户增加用户
* 参数: szServerName. 主机名
* szUserName, 用户名
* szGroup, 用户组名
*****************************************************
int AddUserToGroup(LPWSTR szServerName, LPWSTR szUsername, LPWSTR szGroup)
{
NET_API_STATUS nStatus;
nStatus = NetsGroupAddUser(szServerName, szGroup, szUserName);
}
3. 删除用户
/*****************************************************
* 函数名; DelUser
* 功能: 删除用户
* 参数: szServerName. 主机名
* szUserName, 用户名
*****************************************************
int DelUser(LPWSTR szServerName, LPWSTR szUserName)
{
DWORD dwError = 0;
NET_API_STATUS nStatus;
//删除用户
nStatus = NetUserDel(szServerName, szUserName);
//判断显示结果
…
}
4 列举用户和用户组,获取用户信息
NetUserEnum和NetLocalGroupEnum API函数分别用于列举指定主机中当前的所有用户和用户组
NET_API_STATUS NetUserEnum(
LPCWSTR servername,
DWORD level,
DWORD filter,
LPBYTE *bufptr,
DWORD prefmaxlen,
LPDWORD entriesread,
LPDWORD totalentries,
LPDWORD resume_handle );
NET_API_STATUS NetLocalGroupEnum(
LPCWSTR servername,
DWORD level,
LPBYTE *bufptr,
DWORD prefmaxlen,
LPDWORD entriesread,
LPDWORD totalentries,
PDWORD_PTR resumehandle );
NetUserGetInfo函数的功能是获取指定用户的信息
NET_API_STATUS NetUserGetInfo(
LPCWSTR servername,
LPCWSTR username,
DWORD level,
LPBYTE *bufptr);
/********************************************************
*函数: ListUsers
*功能: 列举用户
*参数: szServerName主机名
**************************************************************************/
int ListUsers(LPWSTR pszServerName)
{
LPUSER_INFO_0 pBuf = NULL;
}