Page 1 of 1

Reloading: Admin to Non-Admin

Posted: 20 May 2015, 11:51
by b0dhimind

Code: Select all

;RELOAD AS ADMIN
#+Esc::
if not A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"
   ExitApp
}
else
{
   Run RunAs "%A_ScriptFullPath%"
   ExitApp
}
return
I'm trying to get the above to act as a toggle for reloading the script as an admin/reloading as non-admin.

It says here http://ahkscript.org/docs/commands/Run.htm that *RunAs can be used to run as an admin but how can I do the reverse, namely going from admin to non-admin status?

Re: Reloading: Admin to Non-Admin

Posted: 20 May 2015, 20:07
by lexikos
The AutoHotkey installer uses ShellExecute via the desktop, in a custom function I named ShellRun. There's also a one-line version which I haven't tested.

Re: Reloading: Admin to Non-Admin

Posted: 28 Feb 2019, 22:19
by jeeswg
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

;==================================================