Mega Code Archive

 
Categories / Delphi / System
 

Is the Current Windows user a Computer Administrator Programmatically Check from Delphi

Title: Is the Current Windows user a Computer Administrator - Programmatically Check from Delphi In Windows there are two main types of user acount types: "Computer Administrator" and "Limited". Computer Administrator accounts allow the user to change all computer settings: install programs, make system wide changes, etc. If you need to check, from your Delphi application, if the currently logged user is an administrator or a member of the Administrators group, use the "IsWindowsAdmin" function: unit WindowsUser; interface uses Windows; //returns True if the currently logged Windows user has Administrator rights function IsWindowsAdmin: Boolean; implementation const SECURITY_NT_AUTHORITY: TSIDIdentifierAuthority = (Value: (0, 0, 0, 0, 0, 5)) ; const SECURITY_BUILTIN_DOMAIN_RID = $00000020; DOMAIN_ALIAS_RID_ADMINS = $00000220; function IsWindowsAdmin: Boolean; var hAccessToken: THandle; ptgGroups: PTokenGroups; dwInfoBufferSize: DWORD; psidAdministrators: PSID; g: Integer; bSuccess: BOOL; 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(SECURITY_NT_AUTHORITY, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, psidAdministrators) ; for g := 0 to ptgGroups.GroupCount - 1 do if EqualSid(psidAdministrators, ptgGroups.Groups[g].Sid) then begin Result := True; Break; end; FreeSid(psidAdministrators) ; end; FreeMem(ptgGroups) ; end; end; end.