function IsAdmin: Boolean;
const
SECURITY_BUILTIN_DOMAIN_RID=32;
DOMAIN_ALIAS_RID_ADMINS =544;
var
hAccessToken: THandle;
ptgGroups: PTokenGroups;
dwInfoBufferSize: DWORD;
sid: PSID;
x: Integer;
bSuccess: BOOL;
sia:TSIDIdentifierAuthority;
begin
Result := False;
bSuccess := OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True, hAccessToken);
if not bSuccess then
begin
if GetLastError = ERROR_NO_TOKEN then
bSuccess := OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, hAccessToken);
end;
if bSuccess then
begin
GetMem(ptgGroups, 1024);
bSuccess := GetTokenInformation(hAccessToken,TokenGroups, ptgGroups, 1024, dwInfoBufferSize);
CloseHandle(hAccessToken);
if bSuccess then
begin
AllocateAndInitializeSid(_SID_IDENTIFIER_AUTHORITY(sia), 2,
SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,0, 0, 0, 0, 0, 0, sid) ;
{$R-}
for x := 0 to ptgGroups.GroupCount-1 do begin
if EqualSid(sid, ptgGroups.Groups[x].Sid) then
begin
Result := True;
Break;
end;
end;
{$R+}
FreeSid(sid);
end;
FreeMem(ptgGroups);
end;
end;
Tests whether the current user is a member of the Administrator's group.
Syntax
BOOL IsUserAnAdmin(VOID);
Return Value
Returns TRUE if the user is a member of the Administrator's group; otherwise, FALSE.
Remarks
This function is a wrapper for CheckTokenMembership. It is recommended to call that function directly to determine Administrator group status rather than calling IsUserAnAdmin.
Note This function is available through Windows Vista. It might be altered or unavailable in subsequent versions of Microsoft Windows.
Function Information
Minimum DLL Version shell32.dll version 5.0 or later
Custom Implementation No
Header shlobj.h
Import library shell32.lib
Minimum operating systems Windows 2000