-
计算机名称
GetComputerNameEx:
https://docs.microsoft.com/en-us/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getcomputernameexa
#include <iostream>
#include<string>
#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
typedef struct _ENUM_NAME
{
COMPUTER_NAME_FORMAT eNum;
LPCTSTR lpName;
}ENUM_NAME;
#define ENTRY(x) {x,_TEXT(#x)}
ENUM_NAME arrE[] = {
ENTRY(ComputerNameNetBIOS),
ENTRY(ComputerNameDnsHostname),
ENTRY(ComputerNameDnsDomain),
ENTRY(ComputerNameDnsFullyQualified),
ENTRY(ComputerNamePhysicalNetBIOS),
ENTRY(ComputerNamePhysicalDnsHostname),
ENTRY(ComputerNamePhysicalDnsDomain),
ENTRY(ComputerNamePhysicalDnsFullyQualified)
};
DWORD dwSize = 0x1000;
LPTSTR szBuffer = new TCHAR[dwSize];
for (int i = 0; i < _countof(arrE); i++)
{
if (!szBuffer) break;
memset(szBuffer, 0, dwSize);
BOOL bRet = GetComputerNameEx(arrE[i].eNum, szBuffer,&dwSize);
if (bRet) {
//cout << szBuffer << endl;
}
else if (GetLastError() == ERROR_MORE_DATA) {
delete[]szBuffer;
szBuffer = NULL;
szBuffer = new TCHAR[dwSize];
if (szBuffer) {
bRet = GetComputerNameEx(arrE[i].eNum, szBuffer, &dwSize);
}
}
if (bRet)
{
cout << arrE[i].lpName << "\t\t\t" << szBuffer << endl;
}
}
delete[]szBuffer;
system("pause");
return 0;
}
-
用户名
GetUserNameEx
https://docs.microsoft.com/zh-cn/windows/desktop/api/secext/nf-secext-getusernameexa
#include<iostream>
#include<tchar.h>
#include<windows.h>
#include<string>
#include<winerror.h>
#define SECURITY_WIN32
#include<security.h>
#pragma comment(lib,"Secur32.lib")
using namespace std;
int main()
{
typedef struct _ENUM_NAME
{
EXTENDED_NAME_FORMAT eNum;
LPCTSTR lpName;
}ENUM_NAME;
#define ENTRY(x) {x,_TEXT(#x)}
ENUM_NAME arrE[] = {
//ENTRY(NameUnknown),
ENTRY(NameFullyQualifiedDN),
ENTRY(NameSamCompatible),
ENTRY(NameDisplay),
ENTRY(NameUniqueId),
ENTRY(NameCanonical),
ENTRY(NameUserPrincipal),
ENTRY(NameCanonicalEx),
ENTRY(NameServicePrincipal),
ENTRY(NameDnsDomain),
ENTRY(NameGivenName),
ENTRY(NameSurname)
};
DWORD dwSize = 0x1000;
LPTSTR szBuffer = new TCHAR[dwSize];
for (int i = 0; i < _countof(arrE); i++)
{
if (!szBuffer) break;
memset(szBuffer, 0, dwSize);
DWORD dwTemp = dwSize;
BOOL bRet = GetUserNameEx(arrE[i].eNum, szBuffer,&dwTemp);
if (bRet) {
//cout << szBuffer << endl;
}
else
{
DWORD iError = GetLastError();
if (iError == ERROR_MORE_DATA) {
delete[]szBuffer;
szBuffer = NULL;
dwSize = dwTemp;
szBuffer = new TCHAR[dwSize];
if (szBuffer) {
bRet = GetUserNameExA(arrE[i].eNum, szBuffer, &dwSize);
}
}
else if (iError == ERROR_NO_SUCH_DOMAIN)
{
cout << arrE[i].lpName << "\t\t" << "ERROR_NO_SUCH_DOMAIN" << endl;
}
else if (iError == ERROR_NONE_MAPPED)
{
cout << arrE[i].lpName << "\t\t" << "ERROR_NO_SUCH_DOMAIN" << endl;
}
else {
cout << iError << endl;
}
}
if (bRet)
{
cout << arrE[i].lpName << "\t\t\t" << szBuffer << endl;
}
}
delete[]szBuffer;
system("pause");
system("pause");
return 0;
}
- 枚举控制面板上列出的用户列表:
#include<iostream>
#include<tchar.h>
#include<windows.h>
#include<string>
#include<winerror.h>
#include<vector>
#include<lm.h>
#pragma comment(lib,"netapi32.lib")
using namespace std;
int GetLocalGroupUsers(vector<wstring> & vecUser)
{
LOCALGROUP_INFO_0 *pGroups = NULL;
DWORD dwGroupNum;
DWORD total;
NET_API_STATUS nStatus = NetLocalGroupEnum(NULL,
0,
(LPBYTE*)&pGroups,
MAX_PREFERRED_LENGTH,
&dwGroupNum,
&total,
NULL);
if (nStatus == NERR_Success && pGroups != NULL)
{
for (int i = 0; i < dwGroupNum; i++) {
LOCALGROUP_MEMBERS_INFO_1 *pMembers = NULL;
DWORD dwMemNum, totalentries;
nStatus = NetLocalGroupGetMembers(NULL,
pGroups[i].lgrpi0_name,
1,
(LPBYTE*)(&pMembers),
MAX_PREFERRED_LENGTH,
&dwMemNum,
&totalentries,
NULL);
if (nStatus == NERR_Success && pMembers)
{
for (int iIndex = 0; iIndex < dwMemNum; iIndex++)
{
if (pMembers[iIndex].lgrmi1_sidusage != SID_NAME_USE::SidTypeUser) continue;
if (find(vecUser.begin(), vecUser.end(), pMembers[iIndex].lgrmi1_name) == vecUser.end())
{
vecUser.push_back(pMembers[iIndex].lgrmi1_name);
}
}
if (pMembers)
{
NetApiBufferFree(pMembers);
pMembers = NULL;
}
}
else
{
cout << "NetLocalGroupGetMembers fail, Error: " << nStatus << endl;
}
}
if (pGroups)
{
NetApiBufferFree(pGroups);
pGroups = NULL;
}
}
else
{
cout << "NetLocalGroupEnum fail, Error: " << nStatus << endl;
}
return vecUser.size();
}
void EnumLocalValidUsers(vector<wstring> &localValid)
{
DWORD dwLevel = 2;
LPUSER_INFO_2 pBuf = NULL;
NET_API_STATUS nStatus;
vector<wstring> arrUser;
//int nAllNum = GetAllUser(arrUser);
int nNum = GetLocalGroupUsers(arrUser);
for (int i = 0; i < nNum; i++)
{
nStatus = NetUserGetInfo(NULL, arrUser[i].c_str(), dwLevel, (LPBYTE *)&pBuf);
if (nStatus == NERR_Success)
{
if (pBuf != NULL)
{
if (pBuf->usri2_flags & UF_ACCOUNTDISABLE)
{
continue;
}
localValid.push_back(arrUser[i]);
if (pBuf != NULL)
{
NetApiBufferFree(pBuf);
pBuf = NULL;
}
}
}
else
{
cout << "NetUserGetInfo fail, Error: " << nStatus << endl;
}
}
}
#define SHOW_VEC_STRING(SHOW, x) \
wcout << SHOW << L":" << endl; \
for (vector<wstring>::iterator it = x.begin(); it != x.end(); it++) \
{ \
wcout << *it << endl; \
}
int main()
{
_tsetlocale(LC_CTYPE, TEXT(""));
vector<wstring> vecUsers;
GetLocalGroupUsers(vecUsers);
SHOW_VEC_STRING(L"GetLocalGroupUsers",vecUsers);
vecUsers.clear();
EnumLocalValidUsers(vecUsers);
SHOW_VEC_STRING(L"EnumLocalValidUsers",vecUsers);
system("pause");
system("pause");
return 0;
}
版权声明:本文为qq_18218335原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。