diff --git a/src/common/utils/elevation.h b/src/common/utils/elevation.h index 17f891ac3f..39710c1a9d 100644 --- a/src/common/utils/elevation.h +++ b/src/common/utils/elevation.h @@ -93,7 +93,15 @@ inline bool run_non_elevated(const std::wstring& file, const std::wstring& param HWND hwnd = GetShellWindow(); 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; } DWORD pid; diff --git a/src/modules/launcher/Microsoft.Launcher/dllmain.cpp b/src/modules/launcher/Microsoft.Launcher/dllmain.cpp index cfe79c9987..75f571c355 100644 --- a/src/modules/launcher/Microsoft.Launcher/dllmain.cpp +++ b/src/modules/launcher/Microsoft.Launcher/dllmain.cpp @@ -60,7 +60,7 @@ private: void init_settings(); // Handle to launch and terminate the launcher - HANDLE m_hProcess; + HANDLE m_hProcess = nullptr; //contains the name of the powerToys std::wstring app_name; @@ -203,6 +203,7 @@ public: virtual void enable() { Logger::info("Microsoft_Launcher::enable()"); + m_enabled = true; ResetEvent(m_hEvent); ResetEvent(send_telemetry_event); @@ -224,7 +225,6 @@ public: if (ShellExecuteExW(&sei)) { - m_enabled = true; m_hProcess = sei.hProcess; Logger::trace("Started PowerToys Run. Handle {}", m_hProcess); } @@ -265,7 +265,6 @@ public: if (run_non_elevated(action_runner_path, params, pidBuffer)) { Logger::trace("Started PowerToys Run Process"); - m_enabled = true; const int maxRetries = 80; for (int retry = 0; retry < maxRetries; ++retry) { @@ -281,7 +280,8 @@ public: } 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); @@ -332,7 +332,11 @@ public: // For now, hotkeyId will always be zero 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."); enable();