Is there a way to determine if a program needs to run as administrator if you only know the filename?

Devon Nullman 21 Reputation points
2023-02-26T21:04:42.7266667+00:00

Example - I have a program called "Recuva" it is located ay C:\Program Files\Recuva\recuva64.exe - It asks for approval to run as an admin if I start it but I want to know that before I start it knowing only the filename. Same for several other programs.

Developer technologies | VB
{count} votes

2 answers

Sort by: Most helpful
  1. RLWA32 49,541 Reputation points
    2023-02-26T22:30:09.2233333+00:00

    It is possible to check if an executable requires elevated privileges as an Administrator before starting it. The most general case requires that the target executable has a manifest that contains -

    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

    and it is possible to examine the .exe to determine if requireAdministrator is present.

    However, there are several variations. For example, if the manifest contains -

    <requestedExecutionLevel level="highestAvailable" uiAccess="false" />

    then elevation will only be requested if the application is started by a member of the Administrators group. Another possibility is for the the elevation request to be contained in a shortcut that starts the executable instead of in the executable's manifest. Yet another possibility is for the application to "self-elevate" after it begins running by restarting itself using ShellExecute and the "runas" verb.

    So which of the above scenarios is applicable to your question?


  2. Castorix31 90,681 Reputation points
    2023-02-27T10:38:00.78+00:00

    A way to check is Elevation is required is with CheckElevation

    nLevel returns > 0 is Elevation is required in this test :

                Dim nFlags = 8
                Dim nLevel = 0
                Dim nReason = 0           
                Dim nReturn As UInteger = CheckElevation("C:\Test.exe", nFlags, IntPtr.Zero, nLevel, nReason)
    
        <DllImport("Kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function CheckElevation(AppName As String, ByRef dwFlags As UInteger, hToken As IntPtr, ByRef dwLevel As UInteger, ByRef dwReason As UInteger) As UInteger
        End Function
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.