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
Check if any app is running as administrator Topic is solved
Re: Check if any app is running as administrator Topic is solved
See here: IsProcessElevated
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Re: Check if any app is running as administrator
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
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)
}
Re: Check if any app is running as administrator
- 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?
[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
- 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:] 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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Check if any app is running as administrator
First line of function could be:
Code: Select all
if !A_IsAdmin
return "Error: script needs to be running elevated to use " . A_ThisFunc
Re: Check if any app is running as administrator
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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
-
- Posts: 59
- Joined: 01 Mar 2022, 17:07
- Contact:
Re: Check if any app is running as administrator
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.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?[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).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:] 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
Re: Check if any app is running as administrator
v2 -> https://github.com/jNizM/ahk-scripts-v2/blob/main/src/ProcessThreadModule/IsProcessElevated.ahkIMTheNachoMan wrote: ↑24 Sep 2023, 23:20Do 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.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
-
- Posts: 59
- Joined: 01 Mar 2022, 17:07
- Contact:
Re: Check if any app is running as administrator
Thank you so much!jNizM wrote: ↑27 Sep 2023, 00:50v2 -> https://github.com/jNizM/ahk-scripts-v2/blob/main/src/ProcessThreadModule/IsProcessElevated.ahkIMTheNachoMan wrote: ↑24 Sep 2023, 23:20Do 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.