Way to detect if non AHK program will require elevation Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Way to detect if non AHK program will require elevation

25 Mar 2020, 04:55

I have my script run on several computers in the office but sometimes for some reason the program it open will give that UAC prompt somtimes it will just launch the program.

Is there a method to know if a program will prompt for elevation?
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: Way to detect if non AHK program will require elevation

25 Mar 2020, 06:45

I doubt if anybody could give you any set rules, other than the programmer has to decide on a "case by case basis". Usually you would try to see if the program can run with no admin privileges and if everything works well. However, there will be various applications that you seek to automate that require admin privileges or you can be in an environment that needs it (like an IT admin running a script to do upgrades), so it can't be helped.
User avatar
Alguimist
Posts: 428
Joined: 05 Oct 2015, 16:41
Contact:

Re: Way to detect if non AHK program will require elevation  Topic is solved

25 Mar 2020, 13:52

Code: Select all

MsgBox % IsElevationRequired("C:\Windows\System32\rstrui.exe") == 1

IsElevationRequired(FilePath) {
    Local

    hModule := DllCall("GetModuleHandle", "Str", "shell32.dll", "Ptr")
    IsElevationRequired := DllCall("GetProcAddress", "Ptr", hModule, "UInt", 865, "Ptr")
    If (!hModule || !IsElevationRequired) {
        Return -1
    }

    Return DllCall(IsElevationRequired, "WStr", FilePath)
}
There's an undocumented function called IsElevationRequired that provides this information. Another way is to inspect the application manifest, which is probably what the above function does behind the scenes. If the application requires administrative privileges at a later time, like VirtualBox installer or compiled AHK scripts (which re-runs itself) you can determine that a UAC prompt is active, or at least determine that the desktop is inaccessible, for example, by checking the return value of WinExist("A") in a timer. If the UAC prompt was aborted, the value of A_LastError is 1223. If your script requires interaction with an elevated application, you may need to resort to the UI Access enabled builds of AHK.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Spawnova, ymnaren and 157 guests