Jump to content
Mark Williams

IsElevated

Recommended Posts

Is there a simple way to check if my app is running elevated?

 

I can see a few oldish posts on this on Google, but nothing recent. I was just wondering if newer Delphi has introduced a simple function for it, although I couldn't find anything in help or likely units. Or  perhaps there is a new Windows API procedure?

Share this post


Link to post
3 hours ago, Mark Williams said:

I can see a few oldish posts on this on Google, but nothing recent. I was just wondering if newer Delphi has introduced a simple function for it, although I couldn't find anything in help or likely units. Or  perhaps there is a new Windows API procedure?

The ones you found probably does something like this, so I guess that's oldish too :classic_rolleyes::

// -----------------------------------------------------------------------------
//
//              RunningAsAdmin
//
// -----------------------------------------------------------------------------
// Detect if we're running at an elevated security level.
// -----------------------------------------------------------------------------
function RunningAsAdmin: boolean;
var
  hToken, hProcess: THandle;
  pTokenInformation: pointer;
  ReturnLength: DWord;
  TokenInformation: TTokenElevation;
begin
  Result := False;
  hProcess := GetCurrentProcess;
  try
    if OpenProcessToken(hProcess, TOKEN_QUERY, hToken) then
      try
        FillChar(TokenInformation, SizeOf(TokenInformation), 0);
        pTokenInformation := @TokenInformation;
        GetTokenInformation(hToken, TokenElevation, pTokenInformation, SizeOf(TokenInformation), ReturnLength);
        Result := (TokenInformation.TokenIsElevated <> 0);
      finally
        CloseHandle(hToken);
      end;
  except
    // Ignore error - although none of the above should throw an exception...
  end;
end;

 

  • Thanks 1

Share this post


Link to post
Guest

Here by Craig Chapman a code to Create Elevate Level as Admin

https://chapmanworld.com/2015/06/08/elevated-privileges-for-delphi-applications/

 

Here, by Alex a code to create your verification

https://stackoverflow.com/questions/923350/delphi-prompt-for-uac-elevation-when-needed

 

Here, by Ruben Bartelink same question - try this 

https://stackoverflow.com/questions/1220213/detect-if-running-as-administrator-with-or-without-elevated-privileges/4497572#4497572

Edited by Guest

Share this post


Link to post
6 minutes ago, Lars Fosdal said:

If you want it to always run elevated, you can specify that in the manifest.

No. I just need to know if user has run my app in elevated mode.  My App launches Outlook and if one is running elevated and the other isn't then you get an OLE error. I just want to be able to advise user which is doing which so they can correct.

Share this post


Link to post
On 1/14/2021 at 1:49 PM, Mark Williams said:

No. I just need to know if user has run my app in elevated mode.  My App launches Outlook and if one is running elevated and the other isn't then you get an OLE error. I just want to be able to advise user which is doing which so they can correct.

RunElevatedSupport.pas is what I use.

Call IsElevated (Boolean) thats all you need to do.

Also offers to elevate code pieces.

 

I love it.

  • Like 2

Share this post


Link to post
49 minutes ago, Mark Williams said:

Thanks. I'll check it out

You might love the ease of use aswell.

Never had any kind of problems so far.

 

Only limitation is when you execute elevated code. That depend on your code. To write files or registry data it worked flawless for me so far.

 

Enjoy it 🙂

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×