Check if any app is running as administrator Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
michel
Posts: 60
Joined: 18 Aug 2016, 02:47

Check if any app is running as administrator

10 Jan 2017, 04:29

Hi,

I would like to restart an application like Logitech-Setpoint with administrator privileges, using Run *RunAs. But only if it isn't already running as administrator.
But how can I find out if the application has been started as administrator?

thanks
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Check if any app is running as administrator  Topic is solved

10 Jan 2017, 05:40

[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
michel
Posts: 60
Joined: 18 Aug 2016, 02:47

Re: Check if any app is running as administrator

11 Jan 2017, 01:41

Thanks jNizm !

Though I needed to find out how to get the PID, but that didn't took long :)
I even learned 2 more things: !() and throw

Code: Select all

SetTitleMatchMode, RegEx
WinGet nPID, PID, ahk_exe i)SetPoint\.exe$
msgbox % IsProcessElevated(nPID)

IsProcessElevated(ProcessID){		;;;thanks to jNizM https://autohotkey.com/boards/viewtopic.php?p=96016
    if !(hProcess := DllCall("OpenProcess", "uint", 0x0400, "int", 0, "uint", ProcessID, "ptr"))
        throw Exception("OpenProcess failed", -1)
    if !(DllCall("advapi32\OpenProcessToken", "ptr", hProcess, "uint", 0x0008, "ptr*", hToken))
        throw Exception("OpenProcessToken failed", -1), DllCall("CloseHandle", "ptr", hProcess)
    if !(DllCall("advapi32\GetTokenInformation", "ptr", hToken, "int", 20, "uint*", IsElevated, "uint", 4, "uint*", size))
        throw Exception("GetTokenInformation failed", -1), DllCall("CloseHandle", "ptr", hToken) && DllCall("CloseHandle", "ptr", hProcess)
    return IsElevated, DllCall("CloseHandle", "ptr", hToken) && DllCall("CloseHandle", "ptr", hProcess)
}
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Check if any app is running as administrator

18 Oct 2017, 20:49

- This function is great, however, if AHK is not admin, and the window is admin, it throws an exception.
- For this reason I've created a modified version of the function that returns blanks on failure, which suggests, but doesn't guarantee, that the window is admin.
- Are there any other approaches?

Code: Select all

;e.g. tested on RegEdit
;the original IsProcessElevated function throws an exception on admin windows if AHK is not admin
;this function returns blanks instead
q:: ;process (window under cursor) - check if elevated (admin)
MouseGetPos,,, hWnd
WinGet, vPID, PID, % "ahk_id " hWnd
MsgBox, % ProcessIsElevated(vPID)
return

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

ProcessIsElevated(vPID)
{
	;PROCESS_QUERY_LIMITED_INFORMATION := 0x1000
	if !(hProc := DllCall("kernel32\OpenProcess", "UInt",0x1000, "Int",0, "UInt",vPID, "Ptr"))
		return -1
	;TOKEN_QUERY := 0x8
	hToken := 0
	if !(DllCall("advapi32\OpenProcessToken", "Ptr",hProc, "UInt",0x8, "Ptr*",hToken))
	{
		DllCall("kernel32\CloseHandle", "Ptr",hProc)
		return -1
	}
	;TokenElevation := 20
	vIsElevated := vSize := 0
	vRet := (DllCall("advapi32\GetTokenInformation", "Ptr",hToken, "Int",20, "UInt*",vIsElevated, "UInt",4, "UInt*",vSize))
	DllCall("kernel32\CloseHandle", "Ptr",hToken)
	DllCall("kernel32\CloseHandle", "Ptr",hProc)
	return vRet ? vIsElevated : -1
}

;==================================================
[EDIT:] Changed function to return -1 on failure, thus: returns 0 if non-admin window, and non-zero if admin/probably admin window (1: admin, -1: failed = probably admin).
[EDIT:] Replaced PROCESS_QUERY_INFORMATION (0x400) with PROCESS_QUERY_LIMITED_INFORMATION (0x1000). See:
How to check if active window runs as admin? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=43417
Last edited by jeeswg on 16 Sep 2019, 16:49, edited 2 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Check if any app is running as administrator

18 Oct 2017, 21:25

First line of function could be:

Code: Select all

if !A_IsAdmin
    return "Error: script needs to be running elevated to use " . A_ThisFunc
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Check if any app is running as administrator

19 Oct 2017, 15:52

Here is an attempt to recreate A_IsAdmin via DllCall, based on the AHK source code, which only checks if AHK is admin, not external processes.

Code: Select all

q:: ;check if AHK is Admin (elevated)
MsgBox, % JEE_AhkIsAdmin()
return

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

JEE_AhkIsAdmin()
{
	;see AHK source code: os_version.h
	;see also:
	;Operating System Version (Windows)
	;https://msdn.microsoft.com/en-gb/library/windows/desktop/ms724832(v=vs.85).aspx
	;Using the Windows Headers (Windows)
	;https://msdn.microsoft.com/en-us/library/windows/desktop/aa383745(v=vs.85).aspx
	vVersion := DllCall("kernel32\GetVersion", "UInt")
	if (vVersion & 0xFF < 5)
		return 1

	;SC_MANAGER_LOCK := 0x8
	hSC := DllCall("advapi32\OpenSCManager", "Ptr",0, "Ptr",0, "UInt",0x8, "Ptr")
	vRet := 0
	if hSC
	{
		if (vLock := DllCall("advapi32\LockServiceDatabase", "Ptr",hSC, "Ptr"))
		{
			DllCall("advapi32\UnlockServiceDatabase", "Ptr",vLock)
			vRet := 1
		}
		else
		{
			vLastError := DllCall("kernel32\GetLastError", "UInt")
			;ERROR_SERVICE_DATABASE_LOCKED := 1055
			if (vLastError = 1055)
				vRet := 1
		}
		DllCall("advapi32\CloseServiceHandle", "Ptr",hSC)
	}
	return vRet
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
IMTheNachoMan
Posts: 59
Joined: 01 Mar 2022, 17:07
Contact:

Re: Check if any app is running as administrator

24 Sep 2023, 23:20

jeeswg wrote:
18 Oct 2017, 20:49
- This function is great, however, if AHK is not admin, and the window is admin, it throws an exception.
- For this reason I've created a modified version of the function that returns blanks on failure, which suggests, but doesn't guarantee, that the window is admin.
- Are there any other approaches?

Code: Select all

;e.g. tested on RegEdit
;the original IsProcessElevated function throws an exception on admin windows if AHK is not admin
;this function returns blanks instead
q:: ;process (window under cursor) - check if elevated (admin)
MouseGetPos,,, hWnd
WinGet, vPID, PID, % "ahk_id " hWnd
MsgBox, % ProcessIsElevated(vPID)
return

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

ProcessIsElevated(vPID)
{
	;PROCESS_QUERY_LIMITED_INFORMATION := 0x1000
	if !(hProc := DllCall("kernel32\OpenProcess", "UInt",0x1000, "Int",0, "UInt",vPID, "Ptr"))
		return -1
	;TOKEN_QUERY := 0x8
	hToken := 0
	if !(DllCall("advapi32\OpenProcessToken", "Ptr",hProc, "UInt",0x8, "Ptr*",hToken))
	{
		DllCall("kernel32\CloseHandle", "Ptr",hProc)
		return -1
	}
	;TokenElevation := 20
	vIsElevated := vSize := 0
	vRet := (DllCall("advapi32\GetTokenInformation", "Ptr",hToken, "Int",20, "UInt*",vIsElevated, "UInt",4, "UInt*",vSize))
	DllCall("kernel32\CloseHandle", "Ptr",hToken)
	DllCall("kernel32\CloseHandle", "Ptr",hProc)
	return vRet ? vIsElevated : -1
}

;==================================================
[EDIT:] Changed function to return -1 on failure, thus: returns 0 if non-admin window, and non-zero if admin/probably admin window (1: admin, -1: failed = probably admin).
[EDIT:] Replaced PROCESS_QUERY_INFORMATION (0x400) with PROCESS_QUERY_LIMITED_INFORMATION (0x1000). See:
How to check if active window runs as admin? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=43417
Do you happen to have an AHK 2.0 version of this? I can't figure out how to get it to work. It works on v1 but I need v2.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Check if any app is running as administrator

27 Sep 2023, 00:50

IMTheNachoMan wrote:
24 Sep 2023, 23:20
Do you happen to have an AHK 2.0 version of this? I can't figure out how to get it to work. It works on v1 but I need v2.
v2 -> https://github.com/jNizM/ahk-scripts-v2/blob/main/src/ProcessThreadModule/IsProcessElevated.ahk
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
IMTheNachoMan
Posts: 59
Joined: 01 Mar 2022, 17:07
Contact:

Re: Check if any app is running as administrator

27 Sep 2023, 09:20

jNizM wrote:
27 Sep 2023, 00:50
IMTheNachoMan wrote:
24 Sep 2023, 23:20
Do you happen to have an AHK 2.0 version of this? I can't figure out how to get it to work. It works on v1 but I need v2.
v2 -> https://github.com/jNizM/ahk-scripts-v2/blob/main/src/ProcessThreadModule/IsProcessElevated.ahk
Thank you so much!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Rohwedder, Tvlao and 121 guests