Script breaks when Run As Admin Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
SirSocks
Posts: 360
Joined: 26 Oct 2018, 08:14

Script breaks when Run As Admin

17 Apr 2021, 09:59

Hello,
When I run a script or compiled script (.exe) as admin, the script fails to run as intended.
See the code below, the gui will display the file path, but if it's launched with Admin privilege's the gui will not display the file path.
I would like the user to be able to "Run as Admin" if they'd like to, or run normally.

How can a script function as intended when "Running as Admin" and running normally?
All help and guidance is appreciated.

Image

Code: Select all

#NoEnv 
#Singleinstance, Force

gui, add, text, center, Drag and drop file here
gui, add, text, w260 vTXT_1, File path is: %FileNameVar%
gui, show, w280 ,Admin_ahk
return

GuiDropFiles:
GuiControl,, TXT_1, % "File path is: " A_GuiEvent
return
JBensimon
Posts: 118
Joined: 19 Nov 2017, 11:19

Re: Script breaks when Run As Admin  Topic is solved

17 Apr 2021, 10:50

Most AHK scripts do run as intended even when elevated. The issue with your particular script is that File Explorer (which is attempting to perform the drag/drop operation) always runs at the Medium integrity level (i.e. non-elevated) whereas your target window (the AHK script's) runs at the High integrity level (i.e. elevated) when it's being run "as admin", and Windows does not by default allow a process (in your case Explorer) to send window messages (including drop messages) to processes running at a higher integrity level (like your script, when it's running "as admin").

There is however a solution available via the Windows API: your script can explicitly "declare" that it will accept certain types of messages from lower integrity processes. Insert the following lines after the gui show command, before return:

Code: Select all

DllCall("ChangeWindowMessageFilter", uint, 0x49, uint, 1)	; WM_COPYGLOBALDATA
DllCall("ChangeWindowMessageFilter", uint, 0x233, uint, 1)	; WM_DROPFILES
JB
User avatar
SirSocks
Posts: 360
Joined: 26 Oct 2018, 08:14

Re: Script breaks when Run As Admin

17 Apr 2021, 11:27

@JBensimon That's a very good explanation, very educational, thank you! :salute:
@mikeyww Thanks for the links, they are helpful.
JBensimon
Posts: 118
Joined: 19 Nov 2017, 11:19

Re: Script breaks when Run As Admin

17 Apr 2021, 11:43

Q: are you Chris Hoffman https://twitter.com/chrisbhoffman or did you just swipe his photo? ;)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: haomingchen1998, xask1 and 238 guests