1
const
2 SECURITY_NT_AUTHORITY: SID_IDENTIFIER_AUTHORITY = (Value: ( 0 , 0 , 0 , 0 , 0 , 5 ));
3 SECURITY_BUILTIN_DOMAIN_RID = $ 00000020 ;
4 DOMAIN_ALIAS_RID_ADMINS = $ 00000220 ;
5
6 function IsAdmin: Boolean;
7 var
8 hAccessToken: THandle;
9 ptgGroups: PTokenGroups;
10 dwInfoBufferSize: DWORD;
11 psidAdministrators: PSID;
12 x: Integer;
13 bSuccess: BOOL;
14 begin
15 Result : = False;
16 bSuccess : = OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True,
17 hAccessToken);
18 if not bSuccess then
19 begin
20 if GetLastError = ERROR_NO_TOKEN then
21 bSuccess : = OpenProcessToken(GetCurrentProcess, TOKEN_QUERY,
22 hAccessToken);
23 end ;
24 if bSuccess then
25 begin
26 GetMem(ptgGroups, 1024 );
27 bSuccess : = GetTokenInformation(hAccessToken, TokenGroups,
28 ptgGroups, 1024 , dwInfoBufferSize);
29 CloseHandle(hAccessToken);
30 if bSuccess then
31 begin
32 AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2 ,
33 SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
34 0 , 0 , 0 , 0 , 0 , 0 , psidAdministrators);
35 { $R- }
36 for x : = 0 to ptgGroups.GroupCount - 1 do
37 if EqualSid(psidAdministrators, ptgGroups.Groups[x].Sid) then
38 begin
39 Result : = True;
40 Break;
41 end ;
42 { $R+ }
43 FreeSid(psidAdministrators);
44 end ;
45 FreeMem(ptgGroups);
46 end ;
47 end ;
2 SECURITY_NT_AUTHORITY: SID_IDENTIFIER_AUTHORITY = (Value: ( 0 , 0 , 0 , 0 , 0 , 5 ));
3 SECURITY_BUILTIN_DOMAIN_RID = $ 00000020 ;
4 DOMAIN_ALIAS_RID_ADMINS = $ 00000220 ;
5
6 function IsAdmin: Boolean;
7 var
8 hAccessToken: THandle;
9 ptgGroups: PTokenGroups;
10 dwInfoBufferSize: DWORD;
11 psidAdministrators: PSID;
12 x: Integer;
13 bSuccess: BOOL;
14 begin
15 Result : = False;
16 bSuccess : = OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True,
17 hAccessToken);
18 if not bSuccess then
19 begin
20 if GetLastError = ERROR_NO_TOKEN then
21 bSuccess : = OpenProcessToken(GetCurrentProcess, TOKEN_QUERY,
22 hAccessToken);
23 end ;
24 if bSuccess then
25 begin
26 GetMem(ptgGroups, 1024 );
27 bSuccess : = GetTokenInformation(hAccessToken, TokenGroups,
28 ptgGroups, 1024 , dwInfoBufferSize);
29 CloseHandle(hAccessToken);
30 if bSuccess then
31 begin
32 AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2 ,
33 SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
34 0 , 0 , 0 , 0 , 0 , 0 , psidAdministrators);
35 { $R- }
36 for x : = 0 to ptgGroups.GroupCount - 1 do
37 if EqualSid(psidAdministrators, ptgGroups.Groups[x].Sid) then
38 begin
39 Result : = True;
40 Break;
41 end ;
42 { $R+ }
43 FreeSid(psidAdministrators);
44 end ;
45 FreeMem(ptgGroups);
46 end ;
47 end ;