////////////////////////////////////////////////////////////////////////////////
// u_GetDomainUserGroups
////////////////////////////////////////////////////////////////////////////////
// (c) 2010 MaBuSE member of delphipraxis.net
// based on MpuNTUser.pas (c) 2005, 2006 Michael Puff (MPu)
////////////////////////////////////////////////////////////////////////////////
// there is no warranty of any kind, use at your own risk
////////////////////////////////////////////////////////////////////////////////
unit u_GetDomainUserGroups;
interface
uses
Windows, SysUtils, Classes;
// http://msdn.microsoft.com/en-us/library/aa370655%28VS.85%29.aspx
function NetUserGetGroups(servername: LPCWSTR; username: LPCWSTR; level: DWORD;
var bufptr: Pointer;
prefmaxlen: DWORD;
var entriesread: DWORD;
var totalentries: DWORD): DWORD;
stdcall;
// http://msdn.microsoft.com/en-us/library/aa370304(VS.85).aspx
function NetApiBufferFree(Buffer: Pointer): DWORD;
stdcall;
// Gibt Gruppen des Benutzers User in der Domäne Domain zurück
function GetDomainUserGroups(
const Domain, User: WideString):
string;
implementation
function NetUserGetGroups;
external '
netapi32.dll'
name '
NetUserGetGroups';
function NetApiBufferFree;
external '
netapi32.dll'
name '
NetApiBufferFree';
const
MAX_PREFERRED_LENGTH = DWORD(-1);
type
TGroupInfo0 =
record
grpi0_name: LPWSTR;
end;
PGroupInfo0 = ^TGroupInfo0;
function GetDomainUserGroups(
const Domain, User: WideString):
string;
var
gui0: Pointer;
i: Integer;
Error, EntriesRead, EntriesTotal: DWORD;
begin
gui0 :=
nil;
try
Error := NetUserGetGroups(PWideChar(Domain), PWideChar(User),
0, gui0, MAX_PREFERRED_LENGTH, EntriesRead, EntriesTotal);
if Error <> 0
then RaiseLastOSError(Error)
else
with TStringList.Create
do
try
for i := 0
to EntriesRead - 1
do
Add(PGroupInfo0(Integer(gui0)+(i*SizeOf(Pointer)))^.grpi0_name);
Sort;
Result := Text;
finally
Free;
end;
finally
NetApiBufferFree(gui0);
end;
end;
end.