本demo是为了获取windows系统下用户有哪些,并判断对应的用户是否设置密码(仅供参考)
#include "stdafx.h"
#include <string>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <map>
#ifdef WIN32
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#include <WtsApi32.h>
#include <atlconv.h>
#include <atlstr.h>
#include <lm.h>
#include <assert.h>
#include <winbase.h>
#include <Strsafe.h>
#pragma comment(lib,"Advapi32.lib")
#pragma comment(lib,"Wtsapi32.lib")
#pragma comment(lib, "netapi32.lib")
#endif
std::string getLoginUserNameBySession()
{
std::string strUserName = "";
LPWSTR buff[64] = {0};
char *user_name_buff = nullptr;
#ifdef WIN32
DWORD infoSize = 0;
WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTS_INFO_CLASS::WTSUserName, buff, &infoSize);
strUserName = CW2A(*buff);
#endif
return strUserName;
}
std::vector<std::string > getAllLocalMachineUserNames()
{
std::vector<std::string > vecNames;
LPUSER_INFO_0 pBuf = NULL;
LPUSER_INFO_0 pTmpBuf;
DWORD dwLevel = 0;
DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
DWORD dwEntriesRead = 0;
DWORD dwTotalEntries = 0;
DWORD dwResumeHandle = 0;
DWORD i;
DWORD dwTotalCount = 0;
NET_API_STATUS nStatus;
LPTSTR pszServerName = NULL;
do
{
nStatus = NetUserEnum((LPCWSTR)pszServerName,
dwLevel,
FILTER_NORMAL_ACCOUNT,
(LPBYTE*)&pBuf,
dwPrefMaxLen,
&dwEntriesRead,
&dwTotalEntries,
&dwResumeHandle);
if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
{
if ((pTmpBuf = pBuf) != NULL)
{
for (i = 0; (i < dwEntriesRead); i++)
{
assert(pTmpBuf != NULL);
if (pTmpBuf == NULL)
{
fprintf(stderr, "An access violation has occurred\n");
break;
}
vecNames.push_back(std::string(CW2A(pTmpBuf->usri0_name)));
pTmpBuf++;
dwTotalCount++;
}
}
}
else
fprintf(stderr, "A system error has occurred: %d\n", nStatus);
if (pBuf != NULL)
{
NetApiBufferFree(pBuf);
pBuf = NULL;
}
}
while (nStatus == ERROR_MORE_DATA);
if (pBuf != NULL)
NetApiBufferFree(pBuf);
fprintf(stderr, "\nTotal of %d entries enumerated\n", dwTotalCount);
return vecNames;
}
std::map<std::string, bool> getAllLocalMachineWithPwdUserNames(std::vector<std::string >& vecUsers )
{
std::map<std::string, bool> mapUserNames;
for (auto sn : vecUsers)
{
HANDLE phToken[128] = { 0 };
std::wstring wsn = CA2W(sn.c_str());
::LogonUser(wsn.c_str(), L".", TEXT(""), LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, phToken);
DWORD dwErr = ::GetLastError();
if (dwErr == ERROR_ACCOUNT_RESTRICTION || dwErr == ERROR_ACCOUNT_DISABLED || dwErr == ERROR_SUCCESS)
{
mapUserNames.insert(std::make_pair(sn, false));
}
else
{
mapUserNames.insert(std::make_pair(sn, true));
}
}
return mapUserNames;
}
int main(int argc, char *argv[])
{
std::cout << "get Login UserName by Session: " << getLoginUserNameBySession() << std::endl;
std::cout << "========================================" << std::endl;
{
std::vector<std::string > vecNames = getAllLocalMachineUserNames();
std::cout << "========================================" << std::endl;
std::map<std::string, bool> retMapUsers = getAllLocalMachineWithPwdUserNames(vecNames);
for (auto iter : retMapUsers)
{
std::cout << "UserName: " << iter.first << " is has pwd: "<< iter.second << std::endl;
}
}
system("pause");
return 0;
}