mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
Fallback to run as admin after install, if running PT as user not possible (#16089)
* Fallback to run as admin, if running PT as user not possible * Update condition - address PR comment Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com> * Update condition #2 - address PR comment Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com> * Update condition #3 - address PR comment Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com> * Revert method name & unify var namings Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com>
This commit is contained in:
@@ -386,15 +386,17 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
|
|||||||
apply_general_settings(general_settings, false);
|
apply_general_settings(general_settings, false);
|
||||||
int rvalue = 0;
|
int rvalue = 0;
|
||||||
const bool elevated = is_process_elevated();
|
const bool elevated = is_process_elevated();
|
||||||
|
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 && cmdLine.find("--dont-elevate") != std::string::npos &&
|
if (elevated && with_dont_elevate_arg && !run_elevated_setting)
|
||||||
general_settings.GetNamedBoolean(L"run_elevated", false) == false) {
|
|
||||||
|
{
|
||||||
schedule_restart_as_non_elevated();
|
schedule_restart_as_non_elevated();
|
||||||
result = 0;
|
result = 0;
|
||||||
}
|
}
|
||||||
else if ((elevated ||
|
else if (elevated || !run_elevated_setting || with_dont_elevate_arg)
|
||||||
general_settings.GetNamedBoolean(L"run_elevated", false) == false ||
|
|
||||||
cmdLine.find("--dont-elevate") != std::string::npos))
|
|
||||||
{
|
{
|
||||||
result = runner(elevated, open_settings, settings_window, openOobe);
|
result = runner(elevated, open_settings, settings_window, openOobe);
|
||||||
|
|
||||||
@@ -423,14 +425,11 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
|
|||||||
|
|
||||||
if (is_restart_scheduled())
|
if (is_restart_scheduled())
|
||||||
{
|
{
|
||||||
if (restart_if_scheduled() == false)
|
if (!restart_if_scheduled())
|
||||||
{
|
|
||||||
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);
|
|
||||||
|
|
||||||
|
{
|
||||||
|
Logger::warn("Scheduled restart failed. Trying restart as admin as fallback...");
|
||||||
restart_same_elevation();
|
restart_same_elevation();
|
||||||
result = -1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stop_tray_icon();
|
stop_tray_icon();
|
||||||
|
|||||||
@@ -52,5 +52,5 @@ bool restart_same_elevation()
|
|||||||
constexpr DWORD exe_path_size = 0xFFFF;
|
constexpr DWORD exe_path_size = 0xFFFF;
|
||||||
auto exe_path = std::make_unique<wchar_t[]>(exe_path_size);
|
auto exe_path = std::make_unique<wchar_t[]>(exe_path_size);
|
||||||
GetModuleFileNameW(nullptr, exe_path.get(), 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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user