获取本机所有用户名并判断是否有密码(Windows)

本文档展示了如何在Windows系统下获取所有本地机器用户名称,并判断每个用户是否设置了密码。使用了WTSQuerySessionInformation和NetUserEnum函数进行用户信息查询和遍历。
摘要由CSDN通过智能技术生成

本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;

	//
	// Call the NetUserEnum function, specifying level 0; 
	//   enumerate global user account types only.
	//
	do // begin do
	{
		nStatus = NetUserEnum((LPCWSTR)pszServerName,
			dwLevel,
			FILTER_NORMAL_ACCOUNT, // global users
			(LPBYTE*)&pBuf,
			dwPrefMaxLen,
			&dwEntriesRead,
			&dwTotalEntries,
			&dwResumeHandle);
		//
		// If the call succeeds,
		//
		if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
		{
			if ((pTmpBuf = pBuf) != NULL)
			{
				//
				// Loop through the entries.
				//
				for (i = 0; (i < dwEntriesRead); i++)
				{
					assert(pTmpBuf != NULL);

					if (pTmpBuf == NULL)
					{
						fprintf(stderr, "An access violation has occurred\n");
						break;
					}
					//
					//  Print the name of the user account.
					//
					//wprintf(L"\t-- %s\n", pTmpBuf->usri0_name);
					vecNames.push_back(std::string(CW2A(pTmpBuf->usri0_name)));

					pTmpBuf++;
					dwTotalCount++;
				}
			}
		}
		//
		// Otherwise, print the system error.
		//
		else
			fprintf(stderr, "A system error has occurred: %d\n", nStatus);
		//
		// Free the allocated buffer.
		//
		if (pBuf != NULL)
		{
			NetApiBufferFree(pBuf);
			pBuf = NULL;
		}
	}
	// Continue to call NetUserEnum while 
	//  there are more entries. 
	// 
	while (nStatus == ERROR_MORE_DATA);  // end do
	//
	// Check again for allocated memory.
	//
	if (pBuf != NULL)
		NetApiBufferFree(pBuf);
	//
	// Print the final count of users enumerated.
	//
	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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奋斗羊羊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值