The one-line version worked for me on Windows 7. Demonstrated below. ShellRun also worked.
Code: Select all
;RELOAD AS NON-ADMIN
MsgBox, % A_IsAdmin
q::
if !A_IsAdmin
Reload
else
{
ComObjCreate("Shell.Application").Windows.FindWindowSW(0, 0, 8, 0, 1).Document.Application.ShellExecute(Chr(34) A_ScriptFullPath Chr(34))
ExitApp
}
return
Code: Select all
;TOGGLE ADMIN/NON-ADMIN
MsgBox, % A_IsAdmin
q::
if !A_IsAdmin
{
Run *RunAs "%A_ScriptFullPath%"
ExitApp
}
else
{
ComObjCreate("Shell.Application").Windows.FindWindowSW(0, 0, 8, 0, 1).Document.Application.ShellExecute(Chr(34) A_ScriptFullPath Chr(34))
ExitApp
}
return
And here is some general test code, which was split across 3 files. Cheers.
Code: Select all
;==================================================
;z test non-admin run as admin 1 non-admin.ahk
vPath := A_ScriptDir "\z test non-admin run as admin 2 admin.ahk"
Run, *RunAs "%vPath%"
return
;==================================================
;z test non-admin run as admin 2 admin.ahk
;[ShellRun function]
;AutoHotkey-Release/ShellRun.ahk at master · Lexikos/AutoHotkey-Release · GitHub
;https://github.com/Lexikos/AutoHotkey-Release/blob/master/installer/source/Lib/ShellRun.ahk
MsgBox, % A_ScriptFullPath "`r`n" A_IsAdmin
vPath := A_ScriptDir "\z test non-admin run as admin 3 non-admin.ahk"
vPathExe := A_AhkPath
vDQ := Chr(34)
;approach 1
if (vPathExe = "")
ShellRun(vDQ vPath vDQ)
else
ShellRun(vDQ vPathExe vDQ, vDQ vPath vDQ)
;approach 2
;SWC_DESKTOP := 0x8
if (vPathExe = "")
ComObjCreate("Shell.Application").Windows.FindWindowSW(0, 0, 8, 0, 1).Document.Application.ShellExecute(vDQ vPath vDQ)
else
ComObjCreate("Shell.Application").Windows.FindWindowSW(0, 0, 8, 0, 1).Document.Application.ShellExecute(vDQ vPathExe vDQ, vDQ vPath vDQ)
return
;==================================================
;z test non-admin run as admin 3 non-admin.ahk
#SingleInstance off
MsgBox, % A_ScriptFullPath "`r`n" A_IsAdmin
return
;==================================================