blob: fdf3be9f43adb6f423b39ac1438b5b2623106ff1 (
plain)
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
|
#pragma once
#include <windows.h>
BOOL IsElevated()
{
BOOL fRet = FALSE;
HANDLE hToken = NULL;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
{
TOKEN_ELEVATION Elevation;
DWORD cbSize = sizeof(TOKEN_ELEVATION);
if (GetTokenInformation(hToken, TokenElevation, &Elevation, sizeof(Elevation), &cbSize))
fRet = Elevation.TokenIsElevated;
}
if (hToken)
CloseHandle(hToken);
return fRet;
}
BOOL SelfElevate(const char* my_path)
{
if (IsElevated())
return FALSE;
SHELLEXECUTEINFO shExecInfo = { 0 };
shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
shExecInfo.fMask = NULL;
shExecInfo.hwnd = NULL;
shExecInfo.lpVerb = "runas";
shExecInfo.lpFile = my_path;
shExecInfo.lpParameters = NULL;
shExecInfo.lpDirectory = NULL;
shExecInfo.nShow = SW_SHOW;
shExecInfo.hInstApp = NULL;
if (!ShellExecuteEx(&shExecInfo))
return FALSE;
// Hide our window and loiter in the background to make scripting easier
// ShowWindow(GetConsoleWindow(), SW_HIDE);
// WaitForSingleObject(shExecInfo.hProcess, INFINITE);
return TRUE;
}
|