封装的对windows账户的一些操作,因为是在Qt下使用,所以传参都是使用QString
msdn相关api说明链接地址:https://docs.microsoft.com/en-us/windows/win32/api/lmaccess/nf-lmaccess-netuserenum#return-value
获取所有账户
void getAllSysUser(QStringList &userList)
{
#ifdef Q_OS_WIN
_USER_INFO_1* l_pUserInfo=NULL;
DWORD l_dwentriesread=0;
DWORD l_dwtotalentries=0;
NetUserEnum(NULL,1,0,(LPBYTE*)&l_pUserInfo,MAX_PREFERRED_LENGTH,&l_dwentriesread,&l_dwtotalentries,NULL);
for(int i=0; i<l_dwentriesread; i++) {
if(l_pUserInfo[i].usri1_flags & UF_ACCOUNTDISABLE)
continue;
QString userName = QString::fromWCharArray((l_pUserInfo+i)->usri1_name);
userList.append(userName);
}
NetApiBufferFree(l_pUserInfo);
#endif
}
登录账户
bool loginSys(const QString &userName, const QString &password)
{
#ifdef Q_OS_WIN
WCHAR userNameCh[ArrayMax] = {0};
int size = userName.toWCharArray(userNameCh);
WCHAR passwordCh[ArrayMax] = {0};
password.toWCharArray(passwordCh);
HANDLE hUser;
qDebug("usrname:%ls, password:%ls", userNameCh, passwordCh);
return LogonUser(userNameCh, L".", passwordCh, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &hUser);
#endif
}
可用于验证账户名密码是否正确
创建账户
NET_API_STATUS cerateSysAccount(const QString &userName, const QString &password)
{
#ifdef Q_OS_WIN
USER_INFO_1 userInfo;
DWORD dwLevel = 1;
DWORD dwError = 0;
NET_API_STATUS nStatus;
//
// Set up the USER_INFO_1 structure.
// USER_PRIV_USER: name identifies a user,
// rather than an administrator or a guest.
// UF_SCRIPT: required
//
WCHAR userNameCh[ArrayMax] = {0};
userName.toWCharArray(userNameCh);
WCHAR passwordCh[ArrayMax] = {0};
password.toWCharArray(passwordCh);
userInfo.usri1_name = userNameCh;
userInfo.usri1_password = passwordCh;
userInfo.usri1_priv = USER_PRIV_USER;
userInfo.usri1_home_dir = NULL;
userInfo.usri1_comment = NULL;
userInfo.usri1_flags = UF_SCRIPT|UF_DONT_EXPIRE_PASSWD|UF_PASSWD_CANT_CHANGE;
userInfo.usri1_script_path = NULL;
//
// Call the NetUserAdd function, specifying level 1.
//
nStatus = NetUserAdd(L"",
dwLevel,
(LPBYTE)&userInfo,
&dwError);
return nStatus;
#endif
}
添加到组
DWORD addAccountToGroup(const QString &userName)
{
_LOCALGROUP_MEMBERS_INFO_3 account;
WCHAR userNameCh[ArrayMax] = {0};
userName.toWCharArray(userNameCh);
account.lgrmi3_domainandname=userNameCh;
NET_API_STATUS nStatus=NetLocalGroupAddMembers(NULL,L"Users",3,(LPBYTE)&account,1);
return nStatus;
}
创建的账户得添加到组中才有效,当前是添加到Users组中
删除账户
DWORD delSysAccount(const QString &userName)
{
NET_API_STATUS nStatus;
WCHAR userNameCh[ArrayMax] = {0};
userName.toWCharArray(userNameCh);
nStatus = NetUserDel(L"", userNameCh);
return nStatus;
}
获取当前账户所在的组
void getGroupMamber(const QString &groupName, QStringList &userList)
{
LOCALGROUP_MEMBERS_INFO_1 *pBuf = nullptr;
DWORD dwLevel = 1;
DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
DWORD dwEntriesRead = 0;
DWORD dwTotalEntries = 0;
NET_API_STATUS nStatus;
WCHAR groupNameCh[ArrayMax] = {0};
groupName.toWCharArray(groupNameCh);
nStatus = NetLocalGroupGetMembers(
nullptr,
groupNameCh,
dwLevel,
(LPBYTE *) &pBuf,
dwPrefMaxLen,
&dwEntriesRead,
&dwTotalEntries,
nullptr
);
// qDebug() << __FUNCTION__ << "song" << GetLastError();
if (nStatus == ERROR_SUCCESS || nStatus == ERROR_MORE_DATA) {
for(DWORD i = 0; i < dwEntriesRead; ++i) {
// qDebug() << __FUNCTION__ << "song" << QString::fromWCharArray(pBuf[i].lgrmi3_domainandname);
userList.append(QString::fromWCharArray(pBuf[i].lgrmi1_name));
}
}
NetApiBufferFree(pBuf);
}
可用于判别某个用户是管理员还是普通账户