获取Windows用户所属组信息

 

type
NET_API_STATUS = DWORD;

PLocalGroupUserInfo_0 = ^TLocalGroupUserInfo_0;

TLocalGroupUserInfo_0 = record
GroupName: LPWSTR;
end;


function NetUserGetLocalGroups(ServerName: LPCWSTR;UserName: LPCWSTR;Level: DWORD;Flags: DWORD;Buf: Pointer;PrefMaxLen: DWORD;var EntriesRead: DWORD;var Entries:DWORD ):NET_API_STATUS;stdCall;external 'NETAPI32.DLL';


function NetApiBufferFree(Buffer: Pointer):NET_API_STATUS;stdCall;external 'NETAPI32.DLL';

 

function TSecurity_WIN.GetUserGroups(const AUser: WideString): TStringList;
const

NERR_Success = 0;
LG_INCLUDE_INDIRECT = 1;


var
wName: WideString;
dwResult: DWORD;
P: PLocalGroupUserInfo_0;
PrefMaxLen: DWORD;
EntriesRead: DWORD;
TotalEntries: DWORD;
I: Integer;
Buf: Pointer;
wGroup: WideString;
begin
Result := TStringList.Create;
wName := AUser;
PrefMaxLen := $FFFFFFFF;
EntriesRead := 0;
TotalEntries := 0;
Buf := nil;
dwResult := NetUserGetLocalGroups(nil,PWideChar(wName),0,LG_INCLUDE_INDIRECT,@Buf,PrefMaxLen,EntriesRead,TotalEntries);

 

if dwResult = NERR_Success then
begin
if Buf <> nil then
begin
P := PLocalGroupUserInfo_0(Buf);
Result.Clear;
for I := 0 to EntriesRead - 1 do
begin
wGroup := P^.GroupName;
Result.Add(wGroup);
Inc(P);
end;
NetApiBufferFree(Buf);
end;
end;
end;

 

你可能感兴趣的:(windows,api,function,buffer,include)