Skip to content

Commit fc0fbd0

Browse files
bzozudit3333
authored andcommitted
runner: show message box when restarting with different elevation fails (microsoft#1061)
Also make the message box appear on top of the settings window.
1 parent ef548ed commit fc0fbd0

File tree

6 files changed

+46
-5
lines changed

6 files changed

+46
-5
lines changed

src/common/common.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,33 @@ bool run_non_elevated(const std::wstring& file, const std::wstring& params) {
310310
return succedded;
311311
}
312312

313+
bool run_same_elevation(const std::wstring& file, const std::wstring& params) {
314+
auto executable_args = file;
315+
if (!params.empty()) {
316+
executable_args += L" " + params;
317+
}
318+
STARTUPINFO si = { 0 };
319+
PROCESS_INFORMATION pi = { 0 };
320+
auto succedded = CreateProcessW(file.c_str(),
321+
const_cast<LPWSTR>(executable_args.c_str()),
322+
nullptr,
323+
nullptr,
324+
FALSE,
325+
0,
326+
nullptr,
327+
nullptr,
328+
&si,
329+
&pi);
330+
if (pi.hProcess) {
331+
CloseHandle(pi.hProcess);
332+
}
333+
if (pi.hThread) {
334+
CloseHandle(pi.hThread);
335+
}
336+
return succedded;
337+
}
338+
339+
313340
std::wstring get_process_path(HWND window) noexcept {
314341
const static std::wstring app_frame_host = L"ApplicationFrameHost.exe";
315342
DWORD pid{};

src/common/common.h

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ bool run_elevated(const std::wstring& file, const std::wstring& params);
5858
// Run command as non-elevated user, returns true if succeeded
5959
bool run_non_elevated(const std::wstring& file, const std::wstring& params);
6060

61+
// Run command with the same elevation, returns true if succedded
62+
bool run_same_elevation(const std::wstring& file, const std::wstring& params);
63+
6164
// Get the executable path or module name for modern apps
6265
std::wstring get_process_path(DWORD pid) noexcept;
6366
// Get the executable path or module name for modern apps

src/runner/main.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ int runner()
8080
catch (std::runtime_error& err)
8181
{
8282
std::string err_what = err.what();
83-
MessageBoxW(NULL, std::wstring(err_what.begin(), err_what.end()).c_str(), L"Error", MB_OK | MB_ICONERROR);
83+
MessageBoxW(nullptr, std::wstring(err_what.begin(), err_what.end()).c_str(), GET_RESOURCE_STRING(IDS_ERROR).c_str(), MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
8484
result = -1;
8585
}
8686
Trace::UnregisterProvider();
@@ -92,8 +92,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
9292
WCHAR username[UNLEN + 1];
9393
DWORD username_length = UNLEN + 1;
9494
GetUserNameW(username, &username_length);
95-
auto runner_mutex = CreateMutexW(NULL, TRUE, (std::wstring(L"Local\\PowerToyRunMutex") + username).c_str());
96-
if (runner_mutex == NULL || GetLastError() == ERROR_ALREADY_EXISTS)
95+
auto runner_mutex = CreateMutexW(nullptr, TRUE, (std::wstring(L"Local\\PowerToyRunMutex") + username).c_str());
96+
if (runner_mutex == nullptr || GetLastError() == ERROR_ALREADY_EXISTS)
9797
{
9898
// The app is already running
9999
return 0;
@@ -124,7 +124,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
124124
catch (std::runtime_error& err)
125125
{
126126
std::string err_what = err.what();
127-
MessageBoxW(NULL, std::wstring(err_what.begin(), err_what.end()).c_str(), GET_RESOURCE_STRING(IDS_ERROR).c_str(), MB_OK | MB_ICONERROR);
127+
MessageBoxW(nullptr, std::wstring(err_what.begin(), err_what.end()).c_str(), GET_RESOURCE_STRING(IDS_ERROR).c_str(), MB_OK | MB_ICONERROR);
128128
result = -1;
129129
}
130130
ReleaseMutex(runner_mutex);
@@ -135,7 +135,9 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
135135
{
136136
auto text = is_process_elevated() ? GET_RESOURCE_STRING(IDS_COULDNOT_RESTART_NONELEVATED) :
137137
GET_RESOURCE_STRING(IDS_COULDNOT_RESTART_ELEVATED);
138-
MessageBoxW(NULL, text.c_str(), GET_RESOURCE_STRING(IDS_ERROR).c_str(), MB_OK | MB_ICONERROR);
138+
MessageBoxW(nullptr, text.c_str(), GET_RESOURCE_STRING(IDS_ERROR).c_str(), MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
139+
140+
restart_same_elevation();
139141
result = -1;
140142
}
141143
}

src/runner/restart_elevated.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,11 @@ bool restart_if_scheduled()
4141
return false;
4242
}
4343
}
44+
45+
bool restart_same_elevation()
46+
{
47+
constexpr DWORD exe_path_size = 0xFFFF;
48+
auto exe_path = std::make_unique<wchar_t[]>(exe_path_size);
49+
GetModuleFileNameW(nullptr, exe_path.get(), exe_path_size);
50+
return run_same_elevation(exe_path.get(), {});
51+
}

src/runner/restart_elevated.h

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ void schedule_restart_as_elevated();
33
void schedule_restart_as_non_elevated();
44
bool is_restart_scheduled();
55
bool restart_if_scheduled();
6+
bool restart_same_elevation();

src/runner/runner.rc

-26 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)