forked from microsoft/PowerToys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restart_elevated.cpp
51 lines (45 loc) · 1.2 KB
/
restart_elevated.cpp
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
#include "pch.h"
#include "restart_elevated.h"
#include "common/common.h"
enum State
{
None,
RestartAsElevated,
RestartAsNonElevated
};
static State state = None;
void schedule_restart_as_elevated()
{
state = RestartAsElevated;
}
void schedule_restart_as_non_elevated()
{
state = RestartAsNonElevated;
}
bool is_restart_scheduled()
{
return state != None;
}
bool restart_if_scheduled()
{
// Make sure we have enough room, even for the long (\\?\) paths
constexpr DWORD exe_path_size = 0xFFFF;
auto exe_path = std::make_unique<wchar_t[]>(exe_path_size);
GetModuleFileNameW(nullptr, exe_path.get(), exe_path_size);
switch (state)
{
case RestartAsElevated:
return run_elevated(exe_path.get(), {});
case RestartAsNonElevated:
return run_non_elevated(exe_path.get(), L"--dont-elevate");
default:
return false;
}
}
bool restart_same_elevation()
{
constexpr DWORD exe_path_size = 0xFFFF;
auto exe_path = std::make_unique<wchar_t[]>(exe_path_size);
GetModuleFileNameW(nullptr, exe_path.get(), exe_path_size);
return run_same_elevation(exe_path.get(), L"--dont-elevate");
}