how to identify when App was start as Administrator ?

Post Reply
User avatar
Jimmy
Posts: 1799
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany

how to identify when App was start as Administrator ?

Post by Jimmy »

hi,

how can i identify when App was start as Administrator ?

greeting,
Jimmy
User avatar
Antonio Linares
Site Admin
Posts: 44515
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: how to identify when App was start as Administrator ?

Post by Antonio Linares »

Dear Jimmy,

Not in FWH yet (included for next build). Two distinct things:

  • Is admin = user belongs to Administrators group

  • Is elevated = process token currently has admin rights (UAC split token)

    Most apps want elevated. Cleanest: GetTokenInformation with TokenElevation (Vista+).

    Quick PRG check using shell32.IsUserAnAdmin (deprecated but works XP→Win11):

Code: Select all | Expand

  DLL32 FUNCTION IsUserAnAdmin() AS BOOL PASCAL FROM "IsUserAnAdmin" LIB "shell32.dll"

  function Main()
     if IsUserAnAdmin()
        MsgInfo( "Running elevated" )
     else
        MsgInfo( "Not elevated" )
     endif
  return nil

Proper way (TokenElevation, Vista+, no deprecation): C helper.

Code: Select all | Expand

  #include <hbapi.h>
  #include <windows.h>

  HB_FUNC( ISELEVATED )
  {
     HANDLE hToken = NULL;
     TOKEN_ELEVATION te;
     DWORD dwSize = 0;
     BOOL bElevated = FALSE;

 if( OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &hToken ) )
 {
    if( GetTokenInformation( hToken, TokenElevation, &te, sizeof( te ), &dwSize ) )
       bElevated = te.TokenIsElevated;
    CloseHandle( hToken );
 }
 hb_retl( bElevated );
  }

Run elevated programmatically if user clicks "run as admin" button:

ShellExecute( 0, "runas", "myapp.exe", "", "", 1 ) // UAC prompt

regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Jimmy
Posts: 1799
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany

Re: how to identify when App was start as Administrator ?

Post by Jimmy »

hi Antonio,

THX for the Solutions.

greeting,
Jimmy
Post Reply