Skip to content

Commit

Permalink
Fallback to run as admin, if running PT as user not possible
Browse files Browse the repository at this point in the history
  • Loading branch information
stefansjfw committed Feb 4, 2022
1 parent f62dd69 commit 6b6edf5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
18 changes: 7 additions & 11 deletions src/runner/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,15 +386,15 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
apply_general_settings(general_settings, false);
int rvalue = 0;
const bool elevated = is_process_elevated();
const bool withDontElevateArg = cmdLine.find("--dont-elevate") != std::string::npos;
const bool runElevatedSetting = general_settings.GetNamedBoolean(L"run_elevated", false);

if (elevated && cmdLine.find("--dont-elevate") != std::string::npos &&
general_settings.GetNamedBoolean(L"run_elevated", false) == false) {
if (elevated && withDontElevateArg && runElevatedSetting == false)
{
schedule_restart_as_non_elevated();
result = 0;
}
else if ((elevated ||
general_settings.GetNamedBoolean(L"run_elevated", false) == false ||
cmdLine.find("--dont-elevate") != std::string::npos))
else if (elevated || runElevatedSetting == false || withDontElevateArg)
{
result = runner(elevated, open_settings, settings_window, openOobe);

Expand Down Expand Up @@ -425,12 +425,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
{
if (restart_if_scheduled() == false)
{
auto text = is_process_elevated() ? GET_RESOURCE_STRING(IDS_COULDNOT_RESTART_NONELEVATED) :
GET_RESOURCE_STRING(IDS_COULDNOT_RESTART_ELEVATED);
MessageBoxW(nullptr, text.c_str(), GET_RESOURCE_STRING(IDS_ERROR).c_str(), MB_OK | MB_ICONERROR | MB_SETFOREGROUND);

restart_same_elevation();
result = -1;
Logger::warn("Scheduled restart failed. Trying restart as admin as fallback...");
restart_elevated();
}
}
stop_tray_icon();
Expand Down
4 changes: 2 additions & 2 deletions src/runner/restart_elevated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ bool restart_if_scheduled()
}
}

bool restart_same_elevation()
bool restart_elevated()
{
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", nullptr);
return run_same_elevation(exe_path.get(), L"", nullptr);
}
2 changes: 1 addition & 1 deletion src/runner/restart_elevated.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ void schedule_restart_as_elevated(bool openSettings);
void schedule_restart_as_non_elevated();
bool is_restart_scheduled();
bool restart_if_scheduled();
bool restart_same_elevation();
bool restart_elevated();

0 comments on commit 6b6edf5

Please sign in to comment.