6,871
社区成员
发帖
与我相关
我的任务
分享
// win32_GetuserSID.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <sddl.h>
#include <windows.h>
#include <LMACCESS.H>
#include <LMERR.H>
#include <LMAPIBUF.H>
#include <Ntsecapi.h>
#include <stdlib.h>
#include <atlconv.h>
#pragma comment(lib,"Netapi32.lib")
#pragma comment(lib,"Advapi32.lib")
int _tmain(int argc,_TCHAR* argv[])
{
USES_CONVERSION;
USER_INFO_0* pUserInfo = NULL;
DWORD totalentries = 0;
DWORD entriesread = 0;
DWORD prefmaxlen = MAX_PREFERRED_LENGTH;
DWORD ret = NetUserEnum(
NULL,
0,
0,
(LPBYTE*)&pUserInfo,
prefmaxlen,
&entriesread,
&totalentries,
NULL
); //获取用户名信息结构体数组
for(int i = 0; i < entriesread; i++)
{
DWORD need1 = 0;
DWORD need2 = 0;
SID_NAME_USE snu;
//LPSTR SSID=(LPSTR)snu;
_tprintf(TEXT("UserName : %s\n"),(pUserInfo + i)->usri0_name); //枚举打印主机上所有的用户名
LookupAccountName(
NULL,
W2A((pUserInfo + i)->usri0_name),
NULL,&need1,
NULL,&need2,
NULL
);
LPBYTE pSid = (LPBYTE)calloc(need1,sizeof(BYTE));
LPWSTR pNoUser = (LPWSTR)calloc(need2,sizeof(TCHAR));
LPSTR LNoUser = (LPSTR)pNoUser;
LookupAccountName(
NULL,
W2A((pUserInfo + i)->usri0_name),
pSid,&need1,
LNoUser,&need2,//我试来试去都发现这两个不能省,虽然我没有使用他们
&snu //我试来试去都发现这两个不能省,虽然我没有使用他
);
LPTSTR lpszSID = NULL;
ConvertSidToStringSid((PSID)pSid,&lpszSID);
_tprintf(TEXT("\tSID is %s\n"),lpszSID);
LocalFree(lpszSID);
free(pSid);
free(pNoUser);
}
NetApiBufferFree(pUserInfo); //释放系统申请的内存空间
getchar();
return 0;
}