找到一個實體,用到ConvertSidToStringSid這個函式,參考API檔案加了頭檔案和lib,還是提示未找到識別符號,好奇怪。
uj5u.com熱心網友回復:
// 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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/20311.html
