From 6d4d50dde85fd8fbf468c54e6fc02b296326b9b7 Mon Sep 17 00:00:00 2001 From: Stefan Markovic Date: Fri, 4 Feb 2022 17:41:09 +0100 Subject: [PATCH 1/5] Fallback to run as admin, if running PT as user not possible --- src/runner/main.cpp | 18 +++++++----------- src/runner/restart_elevated.cpp | 4 ++-- src/runner/restart_elevated.h | 2 +- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/runner/main.cpp b/src/runner/main.cpp index b673fd8fc4f9..f766a361a99d 100644 --- a/src/runner/main.cpp +++ b/src/runner/main.cpp @@ -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); @@ -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(); diff --git a/src/runner/restart_elevated.cpp b/src/runner/restart_elevated.cpp index dc6346dfbb9f..8b16d949cffe 100644 --- a/src/runner/restart_elevated.cpp +++ b/src/runner/restart_elevated.cpp @@ -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(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); } diff --git a/src/runner/restart_elevated.h b/src/runner/restart_elevated.h index b341afa975f5..f2a0e4b15671 100644 --- a/src/runner/restart_elevated.h +++ b/src/runner/restart_elevated.h @@ -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(); From dbd5e57ec9a8ca4816502ec529b6696363c95f53 Mon Sep 17 00:00:00 2001 From: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com> Date: Fri, 4 Feb 2022 19:02:22 +0100 Subject: [PATCH 2/5] Update condition - address PR comment Co-authored-by: Andrey Nekrasov --- src/runner/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/runner/main.cpp b/src/runner/main.cpp index f766a361a99d..668decd7cc15 100644 --- a/src/runner/main.cpp +++ b/src/runner/main.cpp @@ -423,7 +423,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine if (is_restart_scheduled()) { - if (restart_if_scheduled() == false) + if (!restart_if_scheduled()) + { Logger::warn("Scheduled restart failed. Trying restart as admin as fallback..."); restart_elevated(); From 28bef922cbded809902b4b9f54932825552c6d4a Mon Sep 17 00:00:00 2001 From: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com> Date: Fri, 4 Feb 2022 19:02:45 +0100 Subject: [PATCH 3/5] Update condition #2 - address PR comment Co-authored-by: Andrey Nekrasov --- src/runner/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/runner/main.cpp b/src/runner/main.cpp index 668decd7cc15..8213e6cb6bb7 100644 --- a/src/runner/main.cpp +++ b/src/runner/main.cpp @@ -394,7 +394,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine schedule_restart_as_non_elevated(); result = 0; } - else if (elevated || runElevatedSetting == false || withDontElevateArg) + else if (elevated || !runElevatedSetting || withDontElevateArg) + { result = runner(elevated, open_settings, settings_window, openOobe); From 3870e04181d5948913174024e1c02645407741ac Mon Sep 17 00:00:00 2001 From: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com> Date: Fri, 4 Feb 2022 19:02:57 +0100 Subject: [PATCH 4/5] Update condition #3 - address PR comment Co-authored-by: Andrey Nekrasov --- src/runner/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/runner/main.cpp b/src/runner/main.cpp index 8213e6cb6bb7..b0ee4496f4b2 100644 --- a/src/runner/main.cpp +++ b/src/runner/main.cpp @@ -389,7 +389,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine const bool withDontElevateArg = cmdLine.find("--dont-elevate") != std::string::npos; const bool runElevatedSetting = general_settings.GetNamedBoolean(L"run_elevated", false); - if (elevated && withDontElevateArg && runElevatedSetting == false) + if (elevated && withDontElevateArg && !runElevatedSetting) + { schedule_restart_as_non_elevated(); result = 0; From db6baf9b1f08ca09aab9e8e26b2a34c5e883b22c Mon Sep 17 00:00:00 2001 From: Stefan Markovic Date: Fri, 4 Feb 2022 19:28:37 +0100 Subject: [PATCH 5/5] Revert method name & unify var namings --- src/runner/main.cpp | 10 +++++----- src/runner/restart_elevated.cpp | 2 +- src/runner/restart_elevated.h | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/runner/main.cpp b/src/runner/main.cpp index b0ee4496f4b2..f2e106bf68c9 100644 --- a/src/runner/main.cpp +++ b/src/runner/main.cpp @@ -386,16 +386,16 @@ 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); + const bool with_dont_elevate_arg = cmdLine.find("--dont-elevate") != std::string::npos; + const bool run_elevated_setting = general_settings.GetNamedBoolean(L"run_elevated", false); - if (elevated && withDontElevateArg && !runElevatedSetting) + if (elevated && with_dont_elevate_arg && !run_elevated_setting) { schedule_restart_as_non_elevated(); result = 0; } - else if (elevated || !runElevatedSetting || withDontElevateArg) + else if (elevated || !run_elevated_setting || with_dont_elevate_arg) { result = runner(elevated, open_settings, settings_window, openOobe); @@ -429,7 +429,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine { Logger::warn("Scheduled restart failed. Trying restart as admin as fallback..."); - restart_elevated(); + restart_same_elevation(); } } stop_tray_icon(); diff --git a/src/runner/restart_elevated.cpp b/src/runner/restart_elevated.cpp index 8b16d949cffe..f24425c7bbe9 100644 --- a/src/runner/restart_elevated.cpp +++ b/src/runner/restart_elevated.cpp @@ -47,7 +47,7 @@ bool restart_if_scheduled() } } -bool restart_elevated() +bool restart_same_elevation() { constexpr DWORD exe_path_size = 0xFFFF; auto exe_path = std::make_unique(exe_path_size); diff --git a/src/runner/restart_elevated.h b/src/runner/restart_elevated.h index f2a0e4b15671..b341afa975f5 100644 --- a/src/runner/restart_elevated.h +++ b/src/runner/restart_elevated.h @@ -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_elevated(); +bool restart_same_elevation();