function GetAccountSid(
const Server, User: WideString;
var Sid: PSID): DWORD;
function ConvertSidToStringSid(SID: PSID;
var StringSid: LPSTR): Boolean;
stdcall;
external '
advapi32.dll'
name '
ConvertSidToStringSidA';
implementation
function ExpandEnvStr(
const sInput:
string):
string;
const
MAXSIZE = 32768;
// laut PSDK sind 32k das Maximum
begin
SetLength(Result, MAXSIZE);
SetLength(Result,
ExpandEnvironmentStrings(PChar(sInput), @Result[1], Length(Result))-1);
//-1 um abschließendes #0 zu verwerfen
end;
function GetAccountSid(
const Server, User: WideString;
var Sid: PSID): DWORD;
var
dwDomainSize, dwSidSize: DWord;
R : LongBool;
wDomain : WideString;
Use : DWord;
begin
Result := 0;
SetLastError(0);
dwSidSize := 0;
dwDomainSize := 0;
R := LookupAccountNameW(PWideChar(Server), PWideChar(User),
nil, dwSidSize,
nil, dwDomainSize, Use);
if (
not R)
and (GetLastError = ERROR_INSUFFICIENT_BUFFER)
then
begin
SetLength(wDomain, dwDomainSize);
Sid := GetMemory(dwSidSize);
R := LookupAccountNameW(PWideChar(Server), PWideChar(User), Sid,
dwSidSize, PWideChar(wDomain), dwDomainSize, Use);
if not R
then
begin
FreeMemory(Sid);
Sid :=
nil;
end;
end
else
Result := GetLastError;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
EntriesRead, TotalEntries, ResumeHandle : DWORD;
UserInfo : lpUSER_INFO_0;
lpBuffer : Pointer;
i : Integer;
Res : LongWord;
sl : TStringList;
sSID : PChar;
ptrSID : PSID;
reg : TRegistry;
begin
ListBox1.Clear;
sl:= TStringList.Create;
ResumeHandle := 0;
repeat
Res:= NetUserEnum(
nil, 0, 0, lpBuffer, 0, EntriesRead, TotalEntries, ResumeHandle);
if (Res = NERR_SUCCESS)
or (Res = ERROR_MORE_DATA)
then
begin
UserInfo := lpBuffer;
for i := 0
to EntriesRead-1
do
begin
sl.Add(WideCharToString(UserInfo^.usri0_name));
ListBox1.Items.Add('
------------------------------------------');
ListBox1.Items.Add(WideCharToString(UserInfo^.usri0_name));
Inc(UserInfo);
end;
NetApiBufferFree(lpBuffer);
end;
until (Res <> ERROR_MORE_DATA);
for i:=0
to sl.Count-1
do
begin
GetAccountSid('
', sl[i], ptrSID);
if ConvertSidToStringSid(ptrSID, sSID)
then
begin
ListBox1.Items.Add(sSID);
reg:= TRegistry.Create;
reg.RootKey:= HKEY_LOCAL_MACHINE;
if reg.OpenKeyReadOnly('
\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\' + sSID)
then
ListBox1.Items.Add(ExpandEnvStr(reg.ReadString('
ProfileImagePath')));
reg.CloseKey;
reg.Free;
end;
end;
sl.Free;
end;