Added more logging (#10445)

This commit is contained in:
Mykhailo Pylyp
2021-03-26 14:21:29 +02:00
committed by GitHub
parent ccaa98513c
commit 1c46e6b72b

View File

@@ -192,6 +192,7 @@ public:
if (!is_process_elevated(false)) if (!is_process_elevated(false))
{ {
Logger::trace("Starting PowerToys Run from not elevated process");
std::wstring executable_args; std::wstring executable_args;
executable_args += L" -powerToysPid "; executable_args += L" -powerToysPid ";
executable_args += std::to_wstring(powertoys_pid); executable_args += std::to_wstring(powertoys_pid);
@@ -207,6 +208,7 @@ public:
{ {
m_enabled = true; m_enabled = true;
m_hProcess = sei.hProcess; m_hProcess = sei.hProcess;
Logger::trace("Started PowerToys Run. Handle {}", m_hProcess);
} }
else else
{ {
@@ -215,6 +217,7 @@ public:
} }
else else
{ {
Logger::trace("Starting PowerToys Run from elevated process");
std::wstring action_runner_path = get_module_folderpath(); std::wstring action_runner_path = get_module_folderpath();
std::wstring params; std::wstring params;
@@ -228,32 +231,42 @@ public:
action_runner_path += L"\\action_runner.exe"; action_runner_path += L"\\action_runner.exe";
// Set up the shared file from which to retrieve the PID of PowerLauncher // Set up the shared file from which to retrieve the PID of PowerLauncher
HANDLE hMapFile = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(DWORD), POWER_LAUNCHER_PID_SHARED_FILE); HANDLE hMapFile = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(DWORD), POWER_LAUNCHER_PID_SHARED_FILE);
if (hMapFile) if (!hMapFile)
{ {
PDWORD pidBuffer = reinterpret_cast<PDWORD>(MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(DWORD))); auto err = get_last_error_message(GetLastError());
if (pidBuffer) Logger::error(L"Failed to create FileMapping {}. {}", POWER_LAUNCHER_PID_SHARED_FILE, err.has_value() ? err.value() : L"");
{ return;
*pidBuffer = 0; }
m_hProcess = NULL;
if (run_non_elevated(action_runner_path, params, pidBuffer)) PDWORD pidBuffer = reinterpret_cast<PDWORD>(MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(DWORD)));
if (pidBuffer)
{
*pidBuffer = 0;
m_hProcess = NULL;
if (run_non_elevated(action_runner_path, params, pidBuffer))
{
Logger::trace("Started PowerToys Run Process. PID {}", *pidBuffer);
m_enabled = true;
const int maxRetries = 80;
for (int retry = 0; retry < maxRetries; ++retry)
{ {
m_enabled = true; Sleep(50);
const int maxRetries = 80; DWORD pid = *pidBuffer;
for (int retry = 0; retry < maxRetries; ++retry) if (pid)
{ {
Sleep(50); m_hProcess = OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION | SYNCHRONIZE, FALSE, pid);
DWORD pid = *pidBuffer; Logger::trace("Opened PowerToys Run Process. Handle {}", m_hProcess);
if (pid) break;
{
m_hProcess = OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION | SYNCHRONIZE, FALSE, pid);
break;
}
} }
} }
} }
CloseHandle(hMapFile); else
{
Logger::error("Failed to start PowerToys Run");
}
} }
CloseHandle(hMapFile);
} }
} }
} }
@@ -308,7 +321,7 @@ public:
enable(); enable();
} }
Logger::trace("Set POWER_LAUNCHER_SHARED_EVENT"); Logger::trace("Set POWER_LAUNCHER_SHARED_EVENT. Handle {}", m_hProcess);
SetEvent(m_hEvent); SetEvent(m_hEvent);
return true; return true;
} }
@@ -331,6 +344,7 @@ public:
// Terminate process by sending WM_CLOSE signal and if it fails, force terminate. // Terminate process by sending WM_CLOSE signal and if it fails, force terminate.
void terminateProcess() void terminateProcess()
{ {
Logger::trace(L"Terminating PowerToys Run process. Handle {}.", m_hProcess);
DWORD processID = GetProcessId(m_hProcess); DWORD processID = GetProcessId(m_hProcess);
if (TerminateProcess(m_hProcess, 1) == 0) if (TerminateProcess(m_hProcess, 1) == 0)
{ {