how to identify when App was start as Aministator ?

HMG en Español

Moderator: Rathinagiri

Post Reply
User avatar
AUGE_OHR
Posts: 2114
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

how to identify when App was start as Aministator ?

Post by AUGE_OHR »

hi,

how can i identify when App was start as Administrator ?
have fun
Jimmy
User avatar
serge_girard
Posts: 3392
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

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

Post by serge_girard »

Good question.... Anybody?
There's nothing you can do that can't be done...
User avatar
danielmaximiliano
Posts: 2681
Joined: Fri Apr 09, 2010 4:53 pm
Location: Argentina
Contact:

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

Post by danielmaximiliano »

AUGE_OHR wrote: Thu May 07, 2026 6:36 pm hi,

how can i identify when App was start as Administrator ?
//=============================================================================
// Averigua si OTRA aplicación (por su PID) tiene privilegios de Administrador
// Uso: IsProcessAdmin( nPID ) -> Retorna .T. o .F.
//=============================================================================
FUNCTION IsProcessAdmin( nPID )
LOCAL lIsAdmin := .F.

IF ValType( nPID ) == "N" .AND. nPID > 0
lIsAdmin := HW_IsProcessAdmin( nPID )
ENDIF
RETURN lIsAdmin

o

LOCAL nPID := 0

// Supongamos que lanzás el ejecutable del usuario de forma asíncrona
nPID := EXECUTE FILE "MiProyectoCompilado.exe" ... // (O la función que uses para lanzar)

IF nPID > 0
// Le damos un milisegundo a Windows para que asiente el proceso
hb_idleSleep( 0.05 )

IF IsProcessAdmin( nPID )
ConsoleLog("IDE DETECT: El programa del usuario se inició con permisos de Administrador.", "WARNING")
ELSE
ConsoleLog("IDE DETECT: El programa del usuario se inició con permisos estándar.", "INFO")
ENDIF
ENDIF

#pragma BEGINDUMP

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

HB_FUNC( HW_ISPROCESSADMIN )
{
DWORD dwPID = (DWORD) hb_parnl(1);
HANDLE hProcess = NULL;
HANDLE hToken = NULL;
BOOL bIsAdmin = FALSE;

// 1. Abrimos el proceso con permisos de consulta de información
hProcess = OpenProcess( PROCESS_QUERY_LIMITED_INFORMATION, FALSE, dwPID );

if ( hProcess == NULL )
{
// Si falla, intentamos con el acceso de consulta común por si las dudas
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION, FALSE, dwPID );
}

if ( hProcess != NULL )
{
// 2. Abrimos el Token de acceso de ese proceso
if ( OpenProcessToken( hProcess, TOKEN_QUERY, &hToken ) )
{
TOKEN_ELEVATION elevation;
DWORD cbSize = sizeof( TOKEN_ELEVATION );

// 3. Le preguntamos a Windows si ese Token está "Elevado" (Admin)
if ( GetTokenInformation( hToken, TokenElevation, &elevation, sizeof(elevation), &cbSize ) )
{
bIsAdmin = elevation.TokenIsElevated;
}
CloseHandle( hToken );
}
CloseHandle( hProcess );
}

hb_retl( bIsAdmin );
}

#pragma ENDDUMP
*´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`. Harbour/HMG : It's magic !
(¸.·``··*

Saludos / Regards
DaNiElMaXiMiLiAnO

Whatsapp. := +54901169026142
Telegram Name := DaNiElMaXiMiLiAnO
User avatar
AUGE_OHR
Posts: 2114
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

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

Post by AUGE_OHR »

hi,

another Solution :

Code: Select all

#include <hmg.ch>

PROCEDURE MAIN
   
PRIVATE lIsAdmin := wapi_IsUserAnAdmin()
   
IF .Not. lIsAdmin .AND. MsgYesNo ( "The application does not run in administrator mode, do you want to run it as an administrator?", "Run As Administrator" )
   RunApiAsAdministrator()
   RETURN
ENDIF

Msgdebug ( "Does the application run in administrator mode?" , lIsAdmin )
  
/* rest of prg */

RETURN


Function RunApiAsAdministrator()
ShellExecuteEx( wapi_GetActiveWindow(), 'runas', hb_ProgName(), , , SW_SHOWNORMAL )
Return Nil

#pragma BEGINDUMP

#include "SET_COMPILE_HMG_UNICODE.ch"
#include "HMG_UNICODE.h"

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

HB_FUNC( SHELLEXECUTEEX )
{
    SHELLEXECUTEINFO SHExecInfo;
    ZeroMemory(&SHExecInfo, sizeof(SHExecInfo));

    SHExecInfo.cbSize       = sizeof(SHExecInfo);
    SHExecInfo.fMask        = SEE_MASK_NOCLOSEPROCESS;
    SHExecInfo.hwnd         = HB_ISNIL( 1 ) ? GetActiveWindow() : (HWND) HMG_parnl( 1 );
    SHExecInfo.lpVerb       = (LPCSTR) HMG_parc( 2 );
    SHExecInfo.lpFile       = (LPCSTR) HMG_parc( 3 );
    SHExecInfo.lpParameters = (LPCSTR) HMG_parc( 4 );
    SHExecInfo.lpDirectory  = (LPCSTR) HMG_parc( 5 );
    SHExecInfo.nShow        = hb_parni( 6 );
    
    if( ShellExecuteEx(&SHExecInfo) )
    {
        // hb_retnl( (LONG) SHExecInfo.hProcess );
        CloseHandle(SHExecInfo.hProcess);
    }
}
#pragma ENDDUMP
have fun
Jimmy
Post Reply