[fpc-pascal] Function to know if WIN is 32 or 64
DaWorm
daworm at gmail.com
Wed Mar 16 18:23:18 CET 2011
On Wed, Mar 16, 2011 at 11:25 AM, Marcos Douglas <md at delfire.net> wrote:
>> I also had to make sure the code was
>> being ran as an administrator.
>
> That's nice. Can you share the function?
Again, don't remember where I found this, Google or MSDN probably.
Only tested in Delphi.
function IsAdmin: Boolean;
{ -------------------------------------------------------------
Returns a boolean indicating whether or not user has admin
privileges. (Call only when running under NT.)
------------------------------------------------------------- }
var
hAccessToken : tHandle;
ptgGroups : pTokenGroups;
dwInfoBufferSize : DWORD;
psidAdministrators : PSID;
int : integer; // counter
blnResult : boolean; // return flag
const
SECURITY_NT_AUTHORITY: SID_IDENTIFIER_AUTHORITY = (Value:
(0,0,0,0,0,5)); // ntifs
SECURITY_BUILTIN_DOMAIN_RID: DWORD = $00000020;
DOMAIN_ALIAS_RID_ADMINS: DWORD = $00000220;
DOMAIN_ALIAS_RID_USERS : DWORD = $00000221;
DOMAIN_ALIAS_RID_GUESTS: DWORD = $00000222;
DOMAIN_ALIAS_RID_POWER_: DWORD = $00000223;
begin
Result := False;
ptgGroups := nil;
blnResult := OpenThreadToken( GetCurrentThread, TOKEN_QUERY, True,
hAccessToken );
if ( not blnResult ) then
begin
if GetLastError = ERROR_NO_TOKEN then
blnResult := OpenProcessToken( GetCurrentProcess, TOKEN_QUERY,
hAccessToken );
end;
if ( blnResult ) then
try
GetMem(ptgGroups, 1024);
blnResult := GetTokenInformation( hAccessToken, TokenGroups, ptgGroups, 1024,
dwInfoBufferSize );
CloseHandle( hAccessToken );
if ( blnResult ) then
begin
AllocateAndInitializeSid( SECURITY_NT_AUTHORITY, 2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
psidAdministrators );
{$R-}
for int := 0 to ptgGroups.GroupCount - 1 do
if EqualSid( psidAdministrators, ptgGroups.Groups[ int ].Sid ) then
begin
Result := True;
Break;
end;
{$R+}
FreeSid( psidAdministrators );
end;
finally
FreeMem( ptgGroups );
end;
end;
More information about the fpc-pascal
mailing list