[Runner] Open settings when restarted as admin (#9463)

* open settings from runner

* Update src/runner/main.cpp

* Update src/runner/main.cpp

Co-authored-by: Enrico Giordani <enricogior@users.noreply.github.com>
This commit is contained in:
Davide Giacometti 2021-02-10 14:48:43 +01:00 committed by GitHub
parent d92ff6d45d
commit 3715b8c5f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 8 deletions

View file

@ -77,7 +77,7 @@ void open_menu_from_another_instance()
PostMessageW(hwnd_main, WM_COMMAND, ID_SETTINGS_MENU_COMMAND, 0);
}
int runner(bool isProcessElevated)
int runner(bool isProcessElevated, bool openSettings)
{
std::filesystem::path logFilePath(PTSettingsHelper::get_root_save_folder_location());
logFilePath.append(LogSettings::runnerLogPath);
@ -156,6 +156,11 @@ int runner(bool isProcessElevated)
Trace::EventLaunch(get_product_version(), isProcessElevated);
if (openSettings)
{
open_settings_window();
}
result = run_message_loop();
}
catch (std::runtime_error& err)
@ -404,6 +409,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
modules();
auto general_settings = load_general_settings();
const bool openSettings = std::string(lpCmdLine).find("--open-settings") != std::string::npos;
// Apply the general settings but don't save it as the modules() variable has not been loaded yet
apply_general_settings(general_settings, false);
@ -411,13 +417,14 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
const bool elevated = is_process_elevated();
if ((elevated ||
general_settings.GetNamedBoolean(L"run_elevated", false) == false ||
strcmp(lpCmdLine, "--dont-elevate") == 0))
std::string(lpCmdLine).find("--dont-elevate") != std::string::npos))
{
result = runner(elevated);
result = runner(elevated, openSettings);
}
else
{
schedule_restart_as_elevated();
schedule_restart_as_elevated(openSettings);
result = 0;
}
}

View file

@ -7,15 +7,17 @@ enum State
{
None,
RestartAsElevated,
RestartAsElevatedOpenSettings,
RestartAsNonElevated
};
static State state = None;
void schedule_restart_as_elevated()
void schedule_restart_as_elevated(bool openSettings)
{
state = RestartAsElevated;
state = openSettings ? RestartAsElevatedOpenSettings : RestartAsElevated;
}
void schedule_restart_as_non_elevated()
{
state = RestartAsNonElevated;
@ -36,6 +38,8 @@ bool restart_if_scheduled()
{
case RestartAsElevated:
return run_elevated(exe_path.get(), {});
case RestartAsElevatedOpenSettings:
return run_elevated(exe_path.get(), L"--open-settings");
case RestartAsNonElevated:
return run_non_elevated(exe_path.get(), L"--dont-elevate", NULL);
default:

View file

@ -1,5 +1,5 @@
#pragma once
void schedule_restart_as_elevated();
void schedule_restart_as_elevated(bool openSettings);
void schedule_restart_as_non_elevated();
bool is_restart_scheduled();
bool restart_if_scheduled();

View file

@ -79,7 +79,7 @@ std::optional<std::wstring> dispatch_json_action_to_module(const json::JsonObjec
}
else
{
schedule_restart_as_elevated();
schedule_restart_as_elevated(true);
PostQuitMessage(0);
}
}