Skip to content

Commit

Permalink
[runner, color picker, run] remove os dection (#10648)
Browse files Browse the repository at this point in the history
  • Loading branch information
enricogior committed Apr 8, 2021
1 parent 97fdf10 commit aa5ff65
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 84 deletions.
9 changes: 2 additions & 7 deletions src/modules/colorPicker/ColorPicker/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "Generated Files/resource.h"
#include <common/logger/logger.h>
#include <common/SettingsAPI/settings_objects.h>
#include <common/utils/os-detect.h>
#include <common/utils/resources.h>

#include <colorPicker/ColorPicker/ColorPickerConstants.h>
Expand Down Expand Up @@ -227,12 +226,8 @@ class ColorPicker : public PowertoyModuleIface
{
ResetEvent(send_telemetry_event);
ResetEvent(m_hInvokeEvent);
// use only with new settings?
if (UseNewSettings())
{
launch_process();
m_enabled = true;
}
launch_process();
m_enabled = true;
};

virtual void disable()
Expand Down
138 changes: 67 additions & 71 deletions src/modules/launcher/Microsoft.Launcher/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <common/utils/elevation.h>
#include <common/utils/process_path.h>
#include <common/utils/resources.h>
#include <common/utils/os-detect.h>
#include <common/utils/winapi_error.h>

#include <filesystem>
Expand Down Expand Up @@ -185,89 +184,86 @@ class Microsoft_Launcher : public PowertoyModuleIface
Logger::info("Launcher is enabling");
ResetEvent(m_hEvent);
ResetEvent(send_telemetry_event);
// Start PowerLauncher.exe only if the OS is 19H1 or higher
if (UseNewSettings())
{
unsigned long powertoys_pid = GetCurrentProcessId();

if (!is_process_elevated(false))
unsigned long powertoys_pid = GetCurrentProcessId();

if (!is_process_elevated(false))
{
Logger::trace("Starting PowerToys Run from not elevated process");
std::wstring executable_args;
executable_args += L" -powerToysPid ";
executable_args += std::to_wstring(powertoys_pid);
executable_args += L" --centralized-kb-hook";

SHELLEXECUTEINFOW sei{ sizeof(sei) };
sei.fMask = { SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI };
sei.lpFile = L"modules\\launcher\\PowerLauncher.exe";
sei.nShow = SW_SHOWNORMAL;
sei.lpParameters = executable_args.data();

if (ShellExecuteExW(&sei))
{
Logger::trace("Starting PowerToys Run from not elevated process");
std::wstring executable_args;
executable_args += L" -powerToysPid ";
executable_args += std::to_wstring(powertoys_pid);
executable_args += L" --centralized-kb-hook";

SHELLEXECUTEINFOW sei{ sizeof(sei) };
sei.fMask = { SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI };
sei.lpFile = L"modules\\launcher\\PowerLauncher.exe";
sei.nShow = SW_SHOWNORMAL;
sei.lpParameters = executable_args.data();

if (ShellExecuteExW(&sei))
{
m_enabled = true;
m_hProcess = sei.hProcess;
Logger::trace("Started PowerToys Run. Handle {}", m_hProcess);
}
else
{
Logger::error("Launcher failed to start");
}
m_enabled = true;
m_hProcess = sei.hProcess;
Logger::trace("Started PowerToys Run. Handle {}", m_hProcess);
}
else
{
Logger::trace("Starting PowerToys Run from elevated process");
std::wstring action_runner_path = get_module_folderpath();

std::wstring params;
params += L"-run-non-elevated ";
params += L"-target modules\\launcher\\PowerLauncher.exe ";
params += L"-pidFile ";
params += POWER_LAUNCHER_PID_SHARED_FILE;
params += L" -powerToysPid " + std::to_wstring(powertoys_pid) + L" ";
params += L"--centralized-kb-hook ";

action_runner_path += L"\\action_runner.exe";
// 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);
if (!hMapFile)
{
auto err = get_last_error_message(GetLastError());
Logger::error(L"Failed to create FileMapping {}. {}", POWER_LAUNCHER_PID_SHARED_FILE, err.has_value() ? err.value() : L"");
return;
}
Logger::error("Launcher failed to start");
}
}
else
{
Logger::trace("Starting PowerToys Run from elevated process");
std::wstring action_runner_path = get_module_folderpath();

std::wstring params;
params += L"-run-non-elevated ";
params += L"-target modules\\launcher\\PowerLauncher.exe ";
params += L"-pidFile ";
params += POWER_LAUNCHER_PID_SHARED_FILE;
params += L" -powerToysPid " + std::to_wstring(powertoys_pid) + L" ";
params += L"--centralized-kb-hook ";

action_runner_path += L"\\action_runner.exe";
// 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);
if (!hMapFile)
{
auto err = get_last_error_message(GetLastError());
Logger::error(L"Failed to create FileMapping {}. {}", POWER_LAUNCHER_PID_SHARED_FILE, err.has_value() ? err.value() : L"");
return;
}

PDWORD pidBuffer = reinterpret_cast<PDWORD>(MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(DWORD)));
if (pidBuffer)
{
*pidBuffer = 0;
m_hProcess = NULL;
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))
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)
{
Logger::trace("Started PowerToys Run Process. PID {}", *pidBuffer);
m_enabled = true;
const int maxRetries = 80;
for (int retry = 0; retry < maxRetries; ++retry)
Sleep(50);
DWORD pid = *pidBuffer;
if (pid)
{
Sleep(50);
DWORD pid = *pidBuffer;
if (pid)
{
m_hProcess = OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION | SYNCHRONIZE, FALSE, pid);
Logger::trace("Opened PowerToys Run Process. Handle {}", m_hProcess);
break;
}
m_hProcess = OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION | SYNCHRONIZE, FALSE, pid);
Logger::trace("Opened PowerToys Run Process. Handle {}", m_hProcess);
break;
}
}
else
{
Logger::error("Failed to start PowerToys Run");
}
}
CloseHandle(hMapFile);
else
{
Logger::error("Failed to start PowerToys Run");
}
}
CloseHandle(hMapFile);
}
}

Expand Down
11 changes: 5 additions & 6 deletions src/runner/settings_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <common/version/helper.h>
#include <common/logger/logger.h>
#include <common/utils/elevation.h>
#include <common/utils/os-detect.h>
#include <common/utils/process_path.h>
#include <common/utils/timeutil.h>
#include <common/utils/winapi_error.h>
Expand Down Expand Up @@ -367,12 +366,12 @@ void run_settings_window(bool showOobeWindow)

BOOL process_created = false;

// Due to a bug in .NET, running the Settings process as non-elevated
// from an elevated process sometimes results in a crash.
// TODO: Revisit this after switching to .NET 5
if (is_process_elevated() && !UseNewSettings())
if (is_process_elevated())
{
process_created = run_settings_non_elevated(executable_path.c_str(), executable_args.data(), &process_info);
// TODO: Revisit this after switching to .NET 5
// Due to a bug in .NET, running the Settings process as non-elevated
// from an elevated process sometimes results in a crash.
// process_created = run_settings_non_elevated(executable_path.c_str(), executable_args.data(), &process_info);
}

if (FALSE == process_created)
Expand Down

0 comments on commit aa5ff65

Please sign in to comment.