procedure GetLogonSID(hToken: THandle;
var ppsid: PSID);
var dwLength: DWORD;
ptg : ^TOKEN_GROUPS;
i : integer;
begin
dwLength := 0;
ptg :=
nil;
try
// Get required buffer size and allocate the TOKEN_GROUPS buffer.
if not GetTokenInformation(hToken, TokenGroups, ptg, 0, dwLength)
then
begin
if GetLastError <> ERROR_INSUFFICIENT_BUFFER
then
begin
ShowMessage('
GetTokenInformation failed');
Exit;
end;
ptg := HeapAlloc(GetProcessHeap, HEAP_ZERO_MEMORY, dwLength);
if ptg =
nil then
begin
Exit;
end;
// Get the token group information from the access token.
if not GetTokenInformation(hToken, TokenGroups, ptg, dwLength, dwLength)
then
begin
Exit;
end;
// Loop through the groups to find the logon SID.
for i := 0
to ptg.GroupCount-1
do
begin
if ptg.Groups[i].Attributes
and SE_GROUP_LOGON_ID = SE_GROUP_LOGON_ID
then
begin
// Found the logon SID; make a copy of it.
dwLength := GetLengthSid(ptg.Groups[i].Sid);
ppsid := HeapAlloc(GetProcessHeap, HEAP_ZERO_MEMORY, dwLength);
if ppsid =
nil then
begin
Exit;
end;
if not CopySid(dwLength, ppsid, ptg.Groups[i].Sid)
then
begin
// raise exception.Create(Format('CopySid: %s', [SysErrorMessage(GetLastError)]));
HeapFree(GetProcessHeap, 0, ppsid);
Exit;
end;
Break;
end;
end;
end;
finally
// Free the buffer for the token groups.
if ptg <>
nil then
begin
HeapFree(GetProcessHeap, 0, ptg);
end;
end;
end;