-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIsProcessElevated.ahk
53 lines (45 loc) · 2.17 KB
/
IsProcessElevated.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
; https://github.com/jNizM/ahk-scripts-v2/blob/main/src/ProcessThreadModule/IsProcessElevated.ahk
; https://www.autohotkey.com/boards/viewtopic.php?p=540898#p540898
; =============================================================================================================================================================
; Author ........: jNizM
; Released ......: 2017-01-10
; Modified ......: 2023-01-16
; Tested with....: AutoHotkey v2.0.2 (x64)
; Tested on .....: Windows 11 - 22H2 (x64)
; Function ......: IsProcessElevated()
;
; Parameter(s)...: ProcessID - The process identifier of the process.
;
; Return ........: Retrieves whether a token has elevated privileges.
; =============================================================================================================================================================
#Requires AutoHotkey v2.0
IsProcessElevated(ProcessID)
{
static INVALID_HANDLE_VALUE := -1
static PROCESS_QUERY_INFORMATION := 0x0400
static PROCESS_QUERY_LIMITED_INFORMATION := 0x1000
static TOKEN_QUERY := 0x0008
static TOKEN_QUERY_SOURCE := 0x0010
static TokenElevation := 20
hProcess := DllCall("OpenProcess", "UInt", PROCESS_QUERY_INFORMATION, "Int", False, "UInt", ProcessID, "Ptr")
if (!hProcess) || (hProcess = INVALID_HANDLE_VALUE)
{
hProcess := DllCall("OpenProcess", "UInt", PROCESS_QUERY_LIMITED_INFORMATION, "Int", False, "UInt", ProcessID, "Ptr")
if (!hProcess) || (hProcess = INVALID_HANDLE_VALUE)
throw OSError()
}
if !(DllCall("advapi32\OpenProcessToken", "Ptr", hProcess, "UInt", TOKEN_QUERY | TOKEN_QUERY_SOURCE, "Ptr*", &hToken := 0))
{
DllCall("CloseHandle", "Ptr", hProcess)
throw OSError()
}
if !(DllCall("advapi32\GetTokenInformation", "Ptr", hToken, "Int", TokenElevation, "UInt*", &IsElevated := 0, "UInt", 4, "UInt*", &Size := 0))
{
DllCall("CloseHandle", "Ptr", hToken)
DllCall("CloseHandle", "Ptr", hProcess)
throw OSError()
}
DllCall("CloseHandle", "Ptr", hToken)
DllCall("CloseHandle", "Ptr", hProcess)
return IsElevated
}