//ganze Unit siehe Anhang
Function CheckForAdmin: Boolean;
Const SECURITY_NT_AUTHORITY: TSIDIdentifierAuthority = (Value: (0, 0, 0, 0, 0, 5));
SECURITY_BUILTIN_DOMAIN_RID = $00000020;
DOMAIN_ALIAS_RID_ADMINS = $00000220;
Var TokenHandle: THandle;
ReturnLength: LongWord;
TokenInformation: PTokenGroups;
AdminSid: PSID;
Loop: Integer;
AdminCheckForm: TAdminCheckForm;
Begin
Result := False;
TokenHandle := 0;
TokenInformation :=
nil;
If OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, TokenHandle)
Then
Try
GetTokenInformation(TokenHandle, TokenGroups,
nil, 0, ReturnLength);
TokenInformation := GetMemory(ReturnLength);
If (TokenInformation <>
nil)
and GetTokenInformation(TokenHandle,
TokenGroups, TokenInformation, ReturnLength, ReturnLength)
Then Begin
AdminSid :=
nil;
AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2, SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, AdminSid);
For Loop := 0
to TokenInformation^.GroupCount - 1
do
If ((TokenInformation^.Groups[Loop].Attributes
and SE_GROUP_ENABLED) = SE_GROUP_ENABLED)
and EqualSid(TokenInformation^.Groups[Loop].Sid, AdminSid)
Then Begin
Result := True;
Break;
End;
FreeSid(AdminSid);
End;
Finally
FreeMemory(TokenInformation);
CloseHandle(TokenHandle);
End;
If not Result
Then Begin
AdminCheckForm := TAdminCheckForm.Create(
nil);
AdminCheckForm.ShowModal;
AdminCheckForm.Free;
End;
End;