Adnan M. TURKEN
Adnan M. TURKEN

Reputation: 1594

Want to learn if my application has admin rights?

for delphi I want to learn if my application has admin rights, is there a solution for this you may know ?

related question:

How to launch an application with admin rights?

Upvotes: 3

Views: 5272

Answers (3)

Craig Stuntz
Craig Stuntz

Reputation: 126547

You call the WinAPI function GetTokenInformation, passing TokenElevation. There's a C++ example here which should be easy to convert.

Note that being the administrator and being elevated are different.

Upvotes: 4

davea
davea

Reputation: 654

Just attempt to do something that requires administrative privileges:

uses
  WinSvc;

function IsAdmin(Host : string = '') : Boolean;
var
  H: SC_HANDLE;
begin
  if Win32Platform <> VER_PLATFORM_WIN32_NT then
    Result := True
  else begin
    H := OpenSCManager(PChar(Host), nil, SC_MANAGER_ALL_ACCESS);
    Result := H <> 0;
    if Result then
      CloseServiceHandle(H);
  end;
end;

Upvotes: 10

zz1433
zz1433

Reputation: 3586

As to knowing wether or not your program has admin rights, I have no code for that, but This might be an idea. Note that I just wrote it and is untested.

But the idea is if you are able to create a file at the program files folder then you probably have admin rights.

function IsRunningWithAdminPrivs: Boolean;
begin
var
  List: TStringList;
begin
  List := TStringList.Create;
  try
    try
      List.Text := 'Sample';
      // Use SHGetFolder path to retreive the program files folder
      // here is hardcoded for the sake of the example
      List.SaveToFile('C:\program files\test.txt');
      Result := True;
    except
      Result := False;
    end;
  finally
    List.Free;
    DeleteFile('C:\program files\test.txt');
  end;
end;

Upvotes: -2

Related Questions