Don't disable PT Run if it fails to start(running elevated) (#11684)

This commit is contained in:
Mykhailo Pylyp 2021-06-10 14:08:56 +03:00 committed by GitHub
parent 94a6947cd6
commit 8d5f52c718
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 6 deletions

View File

@ -93,7 +93,15 @@ inline bool run_non_elevated(const std::wstring& file, const std::wstring& param
HWND hwnd = GetShellWindow(); HWND hwnd = GetShellWindow();
if (!hwnd) if (!hwnd)
{ {
Logger::error(L"GetShellWindow() failed. {}", get_last_error_or_default(GetLastError())); if (GetLastError() == ERROR_SUCCESS)
{
Logger::warn(L"GetShellWindow() returned null. Shell window is not available");
}
else
{
Logger::error(L"GetShellWindow() failed. {}", get_last_error_or_default(GetLastError()));
}
return false; return false;
} }
DWORD pid; DWORD pid;

View File

@ -60,7 +60,7 @@ private:
void init_settings(); void init_settings();
// Handle to launch and terminate the launcher // Handle to launch and terminate the launcher
HANDLE m_hProcess; HANDLE m_hProcess = nullptr;
//contains the name of the powerToys //contains the name of the powerToys
std::wstring app_name; std::wstring app_name;
@ -203,6 +203,7 @@ public:
virtual void enable() virtual void enable()
{ {
Logger::info("Microsoft_Launcher::enable()"); Logger::info("Microsoft_Launcher::enable()");
m_enabled = true;
ResetEvent(m_hEvent); ResetEvent(m_hEvent);
ResetEvent(send_telemetry_event); ResetEvent(send_telemetry_event);
@ -224,7 +225,6 @@ public:
if (ShellExecuteExW(&sei)) if (ShellExecuteExW(&sei))
{ {
m_enabled = true;
m_hProcess = sei.hProcess; m_hProcess = sei.hProcess;
Logger::trace("Started PowerToys Run. Handle {}", m_hProcess); Logger::trace("Started PowerToys Run. Handle {}", m_hProcess);
} }
@ -265,7 +265,6 @@ public:
if (run_non_elevated(action_runner_path, params, pidBuffer)) if (run_non_elevated(action_runner_path, params, pidBuffer))
{ {
Logger::trace("Started PowerToys Run Process"); Logger::trace("Started PowerToys Run Process");
m_enabled = true;
const int maxRetries = 80; const int maxRetries = 80;
for (int retry = 0; retry < maxRetries; ++retry) for (int retry = 0; retry < maxRetries; ++retry)
{ {
@ -281,7 +280,8 @@ public:
} }
else else
{ {
Logger::error("Failed to start PowerToys Run"); // We expect it to fail if the shell window is not available. It can happen on startup
Logger::warn("Failed to start PowerToys Run");
} }
} }
CloseHandle(hMapFile); CloseHandle(hMapFile);
@ -332,7 +332,11 @@ public:
// For now, hotkeyId will always be zero // For now, hotkeyId will always be zero
if (m_enabled) if (m_enabled)
{ {
if (WaitForSingleObject(m_hProcess, 0) == WAIT_OBJECT_0) if (m_hProcess == nullptr)
{
Logger::warn("PowerToys Run hasn't been started. Starting PowerToys Run");
enable();
} else if (WaitForSingleObject(m_hProcess, 0) == WAIT_OBJECT_0)
{ {
Logger::warn("PowerToys Run has exited unexpectedly, restarting PowerToys Run."); Logger::warn("PowerToys Run has exited unexpectedly, restarting PowerToys Run.");
enable(); enable();