restart_elevated.cpp
1 #include "pch.h" 2 #include "restart_elevated.h" 3 4 #include <common/utils/elevation.h> 5 6 enum State 7 { 8 None, 9 RestartAsElevated, 10 RestartAsElevatedOpenSettings, 11 RestartAsNonElevated, 12 RestartAsNonElevatedOpenSettings 13 }; 14 static State state = None; 15 16 void schedule_restart_as_elevated(bool openSettings) 17 { 18 state = openSettings ? RestartAsElevatedOpenSettings : RestartAsElevated; 19 } 20 21 void schedule_restart_as_non_elevated() 22 { 23 state = RestartAsNonElevated; 24 } 25 26 void schedule_restart_as_non_elevated(bool openSettings) 27 { 28 state = openSettings ? RestartAsNonElevatedOpenSettings : RestartAsNonElevated; 29 } 30 31 bool is_restart_scheduled() 32 { 33 return state != None; 34 } 35 36 bool restart_if_scheduled() 37 { 38 // Make sure we have enough room, even for the long (\\?\) paths 39 constexpr DWORD exe_path_size = 0xFFFF; 40 auto exe_path = std::make_unique<wchar_t[]>(exe_path_size); 41 GetModuleFileNameW(nullptr, exe_path.get(), exe_path_size); 42 switch (state) 43 { 44 case RestartAsElevated: 45 return run_elevated(exe_path.get(), L"--restartedElevated"); 46 case RestartAsElevatedOpenSettings: 47 return run_elevated(exe_path.get(), L"--restartedElevated --open-settings"); 48 case RestartAsNonElevatedOpenSettings: 49 return run_non_elevated(exe_path.get(), L"--open-settings", NULL); 50 case RestartAsNonElevated: 51 return run_non_elevated(exe_path.get(), L"", NULL); 52 default: 53 return false; 54 } 55 } 56 57 bool restart_same_elevation() 58 { 59 constexpr DWORD exe_path_size = 0xFFFF; 60 auto exe_path = std::make_unique<wchar_t[]>(exe_path_size); 61 GetModuleFileNameW(nullptr, exe_path.get(), exe_path_size); 62 return run_same_elevation(exe_path.get(), L"", nullptr); 63 }