2019-12-17 01:36:52 +08:00
|
|
|
#include "pch.h"
|
|
|
|
#include "restart_elevated.h"
|
|
|
|
#include "common/common.h"
|
|
|
|
|
2019-12-27 00:26:11 +08:00
|
|
|
enum State
|
|
|
|
{
|
|
|
|
None,
|
|
|
|
RestartAsElevated,
|
|
|
|
RestartAsNonElevated
|
2019-12-17 01:36:52 +08:00
|
|
|
};
|
|
|
|
static State state = None;
|
|
|
|
|
2019-12-27 00:26:11 +08:00
|
|
|
void schedule_restart_as_elevated()
|
|
|
|
{
|
|
|
|
state = RestartAsElevated;
|
2019-12-17 01:36:52 +08:00
|
|
|
}
|
|
|
|
|
2019-12-27 00:26:11 +08:00
|
|
|
void schedule_restart_as_non_elevated()
|
|
|
|
{
|
|
|
|
state = RestartAsNonElevated;
|
2019-12-17 01:36:52 +08:00
|
|
|
}
|
|
|
|
|
2019-12-27 00:26:11 +08:00
|
|
|
bool is_restart_scheduled()
|
|
|
|
{
|
|
|
|
return state != None;
|
2019-12-17 01:36:52 +08:00
|
|
|
}
|
|
|
|
|
2019-12-27 00:26:11 +08:00
|
|
|
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;
|
|
|
|
}
|
2019-12-17 01:36:52 +08:00
|
|
|
}
|
2020-01-10 01:17:42 +08:00
|
|
|
|
|
|
|
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(), {});
|
|
|
|
}
|