• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 913
  • Last Modified:

Check for Running as administrator in Vista or 7

Hi

I have software where it is important that when using windows 7 or Vista the user is "Run as Administrator"

So what i need is delphi code that will check whether this is the case and if so then I can issue a warning and exit in an orderly fashion.

Alternatively of course if it is possible to force a running program into the "Run as Adminstrator" then that would be ok but I suspect that is may not be possible for security reasons.

thanks
0
rcdcsjdc
Asked:
rcdcsjdc
2 Solutions
 
jimyXCommented:
From this article, you may use the following function:
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;

// When you want to test
procedure TForm1.Button1Click(Sender: TObject);
begin
  if IsWindowsAdmin then
    Showmessage('The current user is Admin')
  else
    Showmessage('The current user is NOT Admin');
end;

Open in new window

0
 
Snip3rXCommented:
Hello Experts,
Maybe this is shorter function:
function IsUserAdmin : boolean;
const CAdminSia : TSidIdentifierAuthority = (value: (0, 0, 0, 0, 0, 5));
var sid : PSid;
    ctm : function (token: dword; sid: pointer; var isMember: bool) : bool; stdcall;
    b1  : bool;
begin
  result := false;
  ctm := GetProcAddress(LoadLibrary('advapi32.dll'), 'CheckTokenMembership');
  if (@ctm <> nil) and AllocateAndInitializeSid(CAdminSia, 2, $20, $220, 0, 0, 0, 0, 0, 0, sid) then begin
    result := ctm(0, sid, b1) and b1;
    FreeSid(sid);
  end;
end;

Open in new window

Sn!per X

0
 
mlmccCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now