windows下C++获取用户相关信息-创新互联
主要流程如下:
1、获取所有用户
2、调用api获取用户相关信息
bool GetUserInfo(WCHAR* username)
{//用户信息
LPUSER_INFO_2 pBuf = NULL;
LPUSER_INFO_2 pBuf2 = NULL;
LPUSER_INFO_4 pBuf4 = NULL;
//获取用户信息
if (NetUserGetInfo(NULL, username, 4, (LPBYTE*)&pBuf4) == NERR_Success ||
NetUserGetInfo(NULL, username, 2, (LPBYTE*)&pBuf2) == NERR_Success) {if (pBuf4 != NULL)
pBuf = (LPUSER_INFO_2)pBuf4;
else
pBuf = pBuf2;
if (pBuf == NULL) { return FALSE;
}
if (pBuf != NULL)
NetApiBufferFree(pBuf);
}
return TRUE;
}
bool GetUserGroup(WCHAR* username)
{LPLOCALGROUP_USERS_INFO_0 plgus0 = NULL;
LPLOCALGROUP_USERS_INFO_0 plgus1 = NULL;
LPGROUP_USERS_INFO_0 pgus0 = NULL;
LPGROUP_USERS_INFO_0 pgus1 = NULL;
DWORD group_entriesread = 0;
DWORD group_totalentries = 0;
//获取用户组信息
if (NetUserGetLocalGroups(NULL, username, 0, LG_INCLUDE_INDIRECT, (LPBYTE*)&plgus0, MAX_PREFERRED_LENGTH, &group_entriesread, &group_totalentries) == NERR_Success)
{if ((plgus1 = plgus0) != NULL)
{ for (int j = 0; j< (int)group_entriesread; j++)
{ //用户组信息
plgus1++;
}
NetApiBufferFree(plgus0);
}
}
// 获取全局组成员
pgus0 = NULL;
if (NetUserGetGroups(NULL, username, 0, (LPBYTE*)&pgus0, MAX_PREFERRED_LENGTH, &group_entriesread, &group_totalentries) == NERR_Success)
{if ((pgus1 = pgus0) != NULL)
{ for (int j = 0; j< (int)group_entriesread; j++)
{ //用户组信息
pgus1++;
}
NetApiBufferFree(pgus0);
}
}
return TRUE;
}
int user_enum()
{LPUSER_INFO_0 pBuf = NULL;
LPUSER_INFO_0 pTmpBuf;
DWORD dwLevel = 0;
LPTSTR pszServerName = NULL;
DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
DWORD dwEntriesRead = 0;
DWORD dwTotalEntries = 0;
DWORD dwResumeHandle = 0;
DWORD i = 0;
CHAR szUserUtf8[260] = {0 };
CHAR szUtf8[512] = {0 };
DWORD len = 0;
NET_API_STATUS nStatus;
int arrayIndex1 = 1;
//获取所有用户信息
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;
}
WideToUtf8_L(pTmpBuf->usri0_name, szUserUtf8, 260);
if (match != NULL)
{if (wildcardcmp(match, szUserUtf8) == 0)
{pTmpBuf++;
dwTotalCount++;
continue;
}
}
UserInfo* info = (UserInfo*)malloc(sizeof(UserInfo));
memset(info, 0, sizeof(UserInfo));
pTmpBuf++;
}
}
}
if (pBuf != NULL) {NetApiBufferFree(pBuf);
pBuf = NULL;
}
return 1;
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
网页标题:windows下C++获取用户相关信息-创新互联
文章路径:http://scyanting.com/article/deigoj.html