Files
PowerToys/src/runner/restart_elevated.cpp
Stefan Markovic 11bb7ccf60 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>
2022-02-08 11:18:12 +01:00

57 lines
1.4 KiB
C++

#include "pch.h"
#include "restart_elevated.h"
#include <common/utils/elevation.h>
enum State
{
None,
RestartAsElevated,
RestartAsElevatedOpenSettings,
RestartAsNonElevated
};
static State state = None;
void schedule_restart_as_elevated(bool openSettings)
{
state = openSettings ? RestartAsElevatedOpenSettings : RestartAsElevated;
}
void schedule_restart_as_non_elevated()
{
state = RestartAsNonElevated;
}
bool is_restart_scheduled()
{
return state != None;
}
bool restart_if_scheduled()
{
// Make sure we have enough room, even for the long (\\?\) paths
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);
switch (state)
{
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:
return false;
}
}
bool restart_same_elevation()
{
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"", nullptr);
}