Reloading: Admin to Non-Admin

Get help with using AutoHotkey and its commands and hotkeys
b0dhimind
Posts: 7
Joined: 20 May 2015, 11:41
Facebook: AhmedIsAwesome

Reloading: Admin to Non-Admin

20 May 2015, 11:51

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?
lexikos
Posts: 6668
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Reloading: Admin to Non-Admin

20 May 2015, 20:07

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.
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Reloading: Admin to Non-Admin

28 Feb 2019, 22:19

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

;==================================================
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot] and 200 guests