Skip to content

Instantly share code, notes, and snippets.

@tmplinshi
Created January 11, 2017 07:42
Show Gist options
  • Save tmplinshi/524e5aefb28bb22bf6c1dae9154609db to your computer and use it in GitHub Desktop.
Save tmplinshi/524e5aefb28bb22bf6c1dae9154609db to your computer and use it in GitHub Desktop.
; ===============================================================================================================================
; Check if a process is elevated
; ===============================================================================================================================
IsProcessElevated(ProcessID)
{
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)
}
; ===============================================================================================================================
MsgBox % IsProcessElevated(DllCall("GetCurrentProcessId"))
; 0 => Process is not elevated
; 1 => Process is elevated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment