From 16116bad0f0a9760fe7fd88b0725536124ab13ab Mon Sep 17 00:00:00 2001 From: Arjun Date: Wed, 10 Jun 2020 16:50:19 -0700 Subject: [PATCH 1/2] Tweaked OS Detection project structure and added check in Launcher --- src/common/common.vcxproj | 2 + src/common/common.vcxproj.filters | 6 + .../launcher/Microsoft.Launcher/dllmain.cpp | 452 +++++++++--------- src/runner/runner.vcxproj | 3 - src/runner/settings_window.cpp | 2 +- 5 files changed, 248 insertions(+), 217 deletions(-) diff --git a/src/common/common.vcxproj b/src/common/common.vcxproj index 72778a00d89..4ba2491e82f 100644 --- a/src/common/common.vcxproj +++ b/src/common/common.vcxproj @@ -125,6 +125,7 @@ + @@ -158,6 +159,7 @@ + Create diff --git a/src/common/common.vcxproj.filters b/src/common/common.vcxproj.filters index 941683584fc..b833aeb0e65 100644 --- a/src/common/common.vcxproj.filters +++ b/src/common/common.vcxproj.filters @@ -111,6 +111,9 @@ Header Files + + Header Files + @@ -180,6 +183,9 @@ Source Files + + Source Files + diff --git a/src/modules/launcher/Microsoft.Launcher/dllmain.cpp b/src/modules/launcher/Microsoft.Launcher/dllmain.cpp index 42e0db209f0..2cda1985f85 100644 --- a/src/modules/launcher/Microsoft.Launcher/dllmain.cpp +++ b/src/modules/launcher/Microsoft.Launcher/dllmain.cpp @@ -6,244 +6,270 @@ #include #include "trace.h" #include "resource.h" +#include extern "C" IMAGE_DOS_HEADER __ImageBase; -BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { - switch (ul_reason_for_call) { - case DLL_PROCESS_ATTACH: - Trace::RegisterProvider(); - break; - case DLL_THREAD_ATTACH: - case DLL_THREAD_DETACH: - break; - case DLL_PROCESS_DETACH: - Trace::UnregisterProvider(); - break; - } - return TRUE; +BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) +{ + switch (ul_reason_for_call) + { + case DLL_PROCESS_ATTACH: + Trace::RegisterProvider(); + break; + case DLL_THREAD_ATTACH: + case DLL_THREAD_DETACH: + break; + case DLL_PROCESS_DETACH: + Trace::UnregisterProvider(); + break; + } + return TRUE; } - // These are the properties shown in the Settings page. -struct ModuleSettings { +struct ModuleSettings +{ } g_settings; // Implement the PowerToy Module Interface and all the required methods. -class Microsoft_Launcher : public PowertoyModuleIface { +class Microsoft_Launcher : public PowertoyModuleIface +{ private: - // The PowerToy state. - bool m_enabled = false; + // The PowerToy state. + bool m_enabled = false; - // Load initial settings from the persisted values. - void init_settings(); + // Load initial settings from the persisted values. + void init_settings(); - // Handle to launch and terminate the launcher - HANDLE m_hProcess; + // Handle to launch and terminate the launcher + HANDLE m_hProcess; - //contains the name of the powerToys - std::wstring app_name; + //contains the name of the powerToys + std::wstring app_name; public: - // Constructor - Microsoft_Launcher() { - app_name = GET_RESOURCE_STRING(IDS_LAUNCHER_NAME); - init_settings(); - }; - - ~Microsoft_Launcher() { - if (m_enabled) - { - TerminateProcess(m_hProcess, 1); - } - m_enabled = false; - } - - // Destroy the powertoy and free memory - virtual void destroy() override { - delete this; - } - - // Return the display name of the powertoy, this will be cached by the runner - virtual const wchar_t* get_name() override { - return app_name.c_str(); - } - - // Return array of the names of all events that this powertoy listens for, with - // nullptr as the last element of the array. Nullptr can also be returned for empty - // list. - virtual const wchar_t** get_events() override { - static const wchar_t* events[] = { nullptr }; - // Available events: - // - ll_keyboard - // - win_hook_event - // - // static const wchar_t* events[] = { ll_keyboard, - // win_hook_event, - // nullptr }; - - return events; - } - - // Return JSON with the configuration options. - virtual bool get_config(wchar_t* buffer, int* buffer_size) override { - HINSTANCE hinstance = reinterpret_cast(&__ImageBase); - - // Create a Settings object. - PowerToysSettings::Settings settings(hinstance, get_name()); - settings.set_description(GET_RESOURCE_STRING(IDS_LAUNCHER_SETTINGS_DESC)); - settings.set_overview_link(L"https://github.com/microsoft/PowerToys/blob/master/src/modules/launcher/README.md"); - - return settings.serialize_to_buffer(buffer, buffer_size); - } - - // Signal from the Settings editor to call a custom action. - // This can be used to spawn more complex editors. - virtual void call_custom_action(const wchar_t* action) override { - static UINT custom_action_num_calls = 0; - try { - // Parse the action values, including name. - PowerToysSettings::CustomActionObject action_object = - PowerToysSettings::CustomActionObject::from_json_string(action); + // Constructor + Microsoft_Launcher() + { + app_name = GET_RESOURCE_STRING(IDS_LAUNCHER_NAME); + init_settings(); + }; + + ~Microsoft_Launcher() + { + if (m_enabled) + { + TerminateProcess(m_hProcess, 1); + } + m_enabled = false; + } + + // Destroy the powertoy and free memory + virtual void destroy() override + { + delete this; } - catch (std::exception ex) { - // Improper JSON. + + // Return the display name of the powertoy, this will be cached by the runner + virtual const wchar_t* get_name() override + { + return app_name.c_str(); } - } - - // Called by the runner to pass the updated settings values as a serialized JSON. - virtual void set_config(const wchar_t* config) override { - try { - // Parse the input JSON string. - PowerToysSettings::PowerToyValues values = - PowerToysSettings::PowerToyValues::from_json_string(config); - - // If you don't need to do any custom processing of the settings, proceed - // to persists the values calling: - values.save_to_settings_file(); - // Otherwise call a custom function to process the settings before saving them to disk: - // save_settings(); + + // Return array of the names of all events that this powertoy listens for, with + // nullptr as the last element of the array. Nullptr can also be returned for empty + // list. + virtual const wchar_t** get_events() override + { + static const wchar_t* events[] = { nullptr }; + // Available events: + // - ll_keyboard + // - win_hook_event + // + // static const wchar_t* events[] = { ll_keyboard, + // win_hook_event, + // nullptr }; + + return events; } - catch (std::exception ex) { - // Improper JSON. + + // Return JSON with the configuration options. + virtual bool get_config(wchar_t* buffer, int* buffer_size) override + { + HINSTANCE hinstance = reinterpret_cast(&__ImageBase); + + // Create a Settings object. + PowerToysSettings::Settings settings(hinstance, get_name()); + settings.set_description(GET_RESOURCE_STRING(IDS_LAUNCHER_SETTINGS_DESC)); + settings.set_overview_link(L"https://github.com/microsoft/PowerToys/blob/master/src/modules/launcher/README.md"); + + return settings.serialize_to_buffer(buffer, buffer_size); } - } - - // Enable the powertoy - virtual void enable() - { - unsigned long powertoys_pid = GetCurrentProcessId(); - - if (!is_process_elevated(false)) - { - std::wstring executable_args = L""; - executable_args.append(std::to_wstring(powertoys_pid)); - - 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(); - ShellExecuteExW(&sei); - - m_hProcess = sei.hProcess; - } - else - { - 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" " + std::to_wstring(powertoys_pid) + L" "; - - 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) - { - PDWORD pidBuffer = reinterpret_cast(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)) - { - const int maxRetries = 80; - for (int retry = 0; retry < maxRetries; ++retry) - { - Sleep(50); - DWORD pid = *pidBuffer; - if (pid) - { - m_hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid); - break; - } - } - } - } - CloseHandle(hMapFile); - } - } - - m_enabled = true; - } - - // Disable the powertoy - virtual void disable() - { - if (m_enabled) - { - TerminateProcess(m_hProcess, 1); - } - - m_enabled = false; - } - - // Returns if the powertoys is enabled - virtual bool is_enabled() override { - return m_enabled; - } - - // Handle incoming event, data is event-specific - virtual intptr_t signal_event(const wchar_t* name, intptr_t data) override { - if (wcscmp(name, ll_keyboard) == 0) { - auto& event = *(reinterpret_cast(data)); - // Return 1 if the keypress is to be suppressed (not forwarded to Windows), - // otherwise return 0. - return 0; + + // Signal from the Settings editor to call a custom action. + // This can be used to spawn more complex editors. + virtual void call_custom_action(const wchar_t* action) override + { + static UINT custom_action_num_calls = 0; + try + { + // Parse the action values, including name. + PowerToysSettings::CustomActionObject action_object = + PowerToysSettings::CustomActionObject::from_json_string(action); + } + catch (std::exception ex) + { + // Improper JSON. + } } - else if (wcscmp(name, win_hook_event) == 0) { - auto& event = *(reinterpret_cast(data)); - // Return value is ignored - return 0; + + // Called by the runner to pass the updated settings values as a serialized JSON. + virtual void set_config(const wchar_t* config) override + { + try + { + // Parse the input JSON string. + PowerToysSettings::PowerToyValues values = + PowerToysSettings::PowerToyValues::from_json_string(config); + + // If you don't need to do any custom processing of the settings, proceed + // to persists the values calling: + values.save_to_settings_file(); + // Otherwise call a custom function to process the settings before saving them to disk: + // save_settings(); + } + catch (std::exception ex) + { + // Improper JSON. + } } - return 0; - } - /* Register helper class to handle system menu items related actions. */ - virtual void register_system_menu_helper(PowertoySystemMenuIface* helper) {} - /* Handle action on system menu item. */ - virtual void signal_system_menu_action(const wchar_t* name) {} + // Enable the powertoy + virtual void enable() + { + // Start PowerLauncher.exe only if the OS is 19H1 or higher + if (UseNewSettings()) + { + unsigned long powertoys_pid = GetCurrentProcessId(); + + if (!is_process_elevated(false)) + { + std::wstring executable_args = L""; + executable_args.append(std::to_wstring(powertoys_pid)); + + 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(); + ShellExecuteExW(&sei); + + m_hProcess = sei.hProcess; + } + else + { + 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" " + std::to_wstring(powertoys_pid) + L" "; + + 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) + { + PDWORD pidBuffer = reinterpret_cast(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)) + { + const int maxRetries = 80; + for (int retry = 0; retry < maxRetries; ++retry) + { + Sleep(50); + DWORD pid = *pidBuffer; + if (pid) + { + m_hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid); + break; + } + } + } + } + CloseHandle(hMapFile); + } + } + } + + m_enabled = true; + } + + // Disable the powertoy + virtual void disable() + { + if (m_enabled) + { + TerminateProcess(m_hProcess, 1); + } + + m_enabled = false; + } + + // Returns if the powertoys is enabled + virtual bool is_enabled() override + { + return m_enabled; + } + + // Handle incoming event, data is event-specific + virtual intptr_t signal_event(const wchar_t* name, intptr_t data) override + { + if (wcscmp(name, ll_keyboard) == 0) + { + auto& event = *(reinterpret_cast(data)); + // Return 1 if the keypress is to be suppressed (not forwarded to Windows), + // otherwise return 0. + return 0; + } + else if (wcscmp(name, win_hook_event) == 0) + { + auto& event = *(reinterpret_cast(data)); + // Return value is ignored + return 0; + } + return 0; + } + + /* Register helper class to handle system menu items related actions. */ + virtual void register_system_menu_helper(PowertoySystemMenuIface* helper) {} + /* Handle action on system menu item. */ + virtual void signal_system_menu_action(const wchar_t* name) {} }; // Load the settings file. -void Microsoft_Launcher::init_settings() { - try { - // Load and parse the settings file for this PowerToy. - PowerToysSettings::PowerToyValues settings = - PowerToysSettings::PowerToyValues::load_from_settings_file(get_name()); - - } - catch (std::exception ex) { - // Error while loading from the settings file. Let default values stay as they are. - } +void Microsoft_Launcher::init_settings() +{ + try + { + // Load and parse the settings file for this PowerToy. + PowerToysSettings::PowerToyValues settings = + PowerToysSettings::PowerToyValues::load_from_settings_file(get_name()); + } + catch (std::exception ex) + { + // Error while loading from the settings file. Let default values stay as they are. + } } - -extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create() { - return new Microsoft_Launcher(); +extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create() +{ + return new Microsoft_Launcher(); } diff --git a/src/runner/runner.vcxproj b/src/runner/runner.vcxproj index f32c8959cba..00a01f5a4d0 100644 --- a/src/runner/runner.vcxproj +++ b/src/runner/runner.vcxproj @@ -238,9 +238,6 @@ {74485049-c722-400f-abe5-86ac52d929b3} - - {e6410bfc-b341-498c-8c67-312c20cdd8d5} - {17da04df-e393-4397-9cf0-84dabe11032e} diff --git a/src/runner/settings_window.cpp b/src/runner/settings_window.cpp index f0afac1afe8..918796d482c 100644 --- a/src/runner/settings_window.cpp +++ b/src/runner/settings_window.cpp @@ -14,7 +14,7 @@ #include #include -#include +#include #define BUFSIZE 1024 From 0cc91113a7d45f4c03d451e67842bcdbce6c7dbd Mon Sep 17 00:00:00 2001 From: Arjun Date: Wed, 10 Jun 2020 17:11:35 -0700 Subject: [PATCH 2/2] Reverted formatter change to minimize diff --- .../launcher/Microsoft.Launcher/dllmain.cpp | 455 +++++++++--------- 1 file changed, 217 insertions(+), 238 deletions(-) diff --git a/src/modules/launcher/Microsoft.Launcher/dllmain.cpp b/src/modules/launcher/Microsoft.Launcher/dllmain.cpp index 2cda1985f85..0b6b82d1745 100644 --- a/src/modules/launcher/Microsoft.Launcher/dllmain.cpp +++ b/src/modules/launcher/Microsoft.Launcher/dllmain.cpp @@ -10,266 +10,245 @@ extern "C" IMAGE_DOS_HEADER __ImageBase; -BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) -{ - switch (ul_reason_for_call) - { - case DLL_PROCESS_ATTACH: - Trace::RegisterProvider(); - break; - case DLL_THREAD_ATTACH: - case DLL_THREAD_DETACH: - break; - case DLL_PROCESS_DETACH: - Trace::UnregisterProvider(); - break; - } - return TRUE; +BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { + switch (ul_reason_for_call) { + case DLL_PROCESS_ATTACH: + Trace::RegisterProvider(); + break; + case DLL_THREAD_ATTACH: + case DLL_THREAD_DETACH: + break; + case DLL_PROCESS_DETACH: + Trace::UnregisterProvider(); + break; + } + return TRUE; } + // These are the properties shown in the Settings page. -struct ModuleSettings -{ +struct ModuleSettings { } g_settings; // Implement the PowerToy Module Interface and all the required methods. -class Microsoft_Launcher : public PowertoyModuleIface -{ +class Microsoft_Launcher : public PowertoyModuleIface { private: - // The PowerToy state. - bool m_enabled = false; + // The PowerToy state. + bool m_enabled = false; - // Load initial settings from the persisted values. - void init_settings(); + // Load initial settings from the persisted values. + void init_settings(); - // Handle to launch and terminate the launcher - HANDLE m_hProcess; + // Handle to launch and terminate the launcher + HANDLE m_hProcess; - //contains the name of the powerToys - std::wstring app_name; + //contains the name of the powerToys + std::wstring app_name; public: - // Constructor - Microsoft_Launcher() - { - app_name = GET_RESOURCE_STRING(IDS_LAUNCHER_NAME); - init_settings(); - }; - - ~Microsoft_Launcher() - { - if (m_enabled) - { - TerminateProcess(m_hProcess, 1); - } - m_enabled = false; - } - - // Destroy the powertoy and free memory - virtual void destroy() override - { - delete this; + // Constructor + Microsoft_Launcher() { + app_name = GET_RESOURCE_STRING(IDS_LAUNCHER_NAME); + init_settings(); + }; + + ~Microsoft_Launcher() { + if (m_enabled) + { + TerminateProcess(m_hProcess, 1); + } + m_enabled = false; + } + + // Destroy the powertoy and free memory + virtual void destroy() override { + delete this; + } + + // Return the display name of the powertoy, this will be cached by the runner + virtual const wchar_t* get_name() override { + return app_name.c_str(); + } + + // Return array of the names of all events that this powertoy listens for, with + // nullptr as the last element of the array. Nullptr can also be returned for empty + // list. + virtual const wchar_t** get_events() override { + static const wchar_t* events[] = { nullptr }; + // Available events: + // - ll_keyboard + // - win_hook_event + // + // static const wchar_t* events[] = { ll_keyboard, + // win_hook_event, + // nullptr }; + + return events; + } + + // Return JSON with the configuration options. + virtual bool get_config(wchar_t* buffer, int* buffer_size) override { + HINSTANCE hinstance = reinterpret_cast(&__ImageBase); + + // Create a Settings object. + PowerToysSettings::Settings settings(hinstance, get_name()); + settings.set_description(GET_RESOURCE_STRING(IDS_LAUNCHER_SETTINGS_DESC)); + settings.set_overview_link(L"https://github.com/microsoft/PowerToys/blob/master/src/modules/launcher/README.md"); + + return settings.serialize_to_buffer(buffer, buffer_size); + } + + // Signal from the Settings editor to call a custom action. + // This can be used to spawn more complex editors. + virtual void call_custom_action(const wchar_t* action) override { + static UINT custom_action_num_calls = 0; + try { + // Parse the action values, including name. + PowerToysSettings::CustomActionObject action_object = + PowerToysSettings::CustomActionObject::from_json_string(action); } - - // Return the display name of the powertoy, this will be cached by the runner - virtual const wchar_t* get_name() override - { - return app_name.c_str(); + catch (std::exception ex) { + // Improper JSON. } - - // Return array of the names of all events that this powertoy listens for, with - // nullptr as the last element of the array. Nullptr can also be returned for empty - // list. - virtual const wchar_t** get_events() override - { - static const wchar_t* events[] = { nullptr }; - // Available events: - // - ll_keyboard - // - win_hook_event - // - // static const wchar_t* events[] = { ll_keyboard, - // win_hook_event, - // nullptr }; - - return events; + } + + // Called by the runner to pass the updated settings values as a serialized JSON. + virtual void set_config(const wchar_t* config) override { + try { + // Parse the input JSON string. + PowerToysSettings::PowerToyValues values = + PowerToysSettings::PowerToyValues::from_json_string(config); + + // If you don't need to do any custom processing of the settings, proceed + // to persists the values calling: + values.save_to_settings_file(); + // Otherwise call a custom function to process the settings before saving them to disk: + // save_settings(); } - - // Return JSON with the configuration options. - virtual bool get_config(wchar_t* buffer, int* buffer_size) override - { - HINSTANCE hinstance = reinterpret_cast(&__ImageBase); - - // Create a Settings object. - PowerToysSettings::Settings settings(hinstance, get_name()); - settings.set_description(GET_RESOURCE_STRING(IDS_LAUNCHER_SETTINGS_DESC)); - settings.set_overview_link(L"https://github.com/microsoft/PowerToys/blob/master/src/modules/launcher/README.md"); - - return settings.serialize_to_buffer(buffer, buffer_size); + catch (std::exception ex) { + // Improper JSON. } - - // Signal from the Settings editor to call a custom action. - // This can be used to spawn more complex editors. - virtual void call_custom_action(const wchar_t* action) override - { - static UINT custom_action_num_calls = 0; - try - { - // Parse the action values, including name. - PowerToysSettings::CustomActionObject action_object = - PowerToysSettings::CustomActionObject::from_json_string(action); - } - catch (std::exception ex) - { - // Improper JSON. - } + } + + // Enable the powertoy + virtual void enable() + { + // Start PowerLauncher.exe only if the OS is 19H1 or higher + if (UseNewSettings()) + { + unsigned long powertoys_pid = GetCurrentProcessId(); + + if (!is_process_elevated(false)) + { + std::wstring executable_args = L""; + executable_args.append(std::to_wstring(powertoys_pid)); + + 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(); + ShellExecuteExW(&sei); + + m_hProcess = sei.hProcess; + } + else + { + 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" " + std::to_wstring(powertoys_pid) + L" "; + + 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) + { + PDWORD pidBuffer = reinterpret_cast(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)) + { + const int maxRetries = 80; + for (int retry = 0; retry < maxRetries; ++retry) + { + Sleep(50); + DWORD pid = *pidBuffer; + if (pid) + { + m_hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid); + break; + } + } + } + } + CloseHandle(hMapFile); + } + } + } + + m_enabled = true; + } + + // Disable the powertoy + virtual void disable() + { + if (m_enabled) + { + TerminateProcess(m_hProcess, 1); + } + + m_enabled = false; + } + + // Returns if the powertoys is enabled + virtual bool is_enabled() override { + return m_enabled; + } + + // Handle incoming event, data is event-specific + virtual intptr_t signal_event(const wchar_t* name, intptr_t data) override { + if (wcscmp(name, ll_keyboard) == 0) { + auto& event = *(reinterpret_cast(data)); + // Return 1 if the keypress is to be suppressed (not forwarded to Windows), + // otherwise return 0. + return 0; } - - // Called by the runner to pass the updated settings values as a serialized JSON. - virtual void set_config(const wchar_t* config) override - { - try - { - // Parse the input JSON string. - PowerToysSettings::PowerToyValues values = - PowerToysSettings::PowerToyValues::from_json_string(config); - - // If you don't need to do any custom processing of the settings, proceed - // to persists the values calling: - values.save_to_settings_file(); - // Otherwise call a custom function to process the settings before saving them to disk: - // save_settings(); - } - catch (std::exception ex) - { - // Improper JSON. - } + else if (wcscmp(name, win_hook_event) == 0) { + auto& event = *(reinterpret_cast(data)); + // Return value is ignored + return 0; } + return 0; + } - // Enable the powertoy - virtual void enable() - { - // Start PowerLauncher.exe only if the OS is 19H1 or higher - if (UseNewSettings()) - { - unsigned long powertoys_pid = GetCurrentProcessId(); - - if (!is_process_elevated(false)) - { - std::wstring executable_args = L""; - executable_args.append(std::to_wstring(powertoys_pid)); - - 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(); - ShellExecuteExW(&sei); - - m_hProcess = sei.hProcess; - } - else - { - 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" " + std::to_wstring(powertoys_pid) + L" "; - - 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) - { - PDWORD pidBuffer = reinterpret_cast(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)) - { - const int maxRetries = 80; - for (int retry = 0; retry < maxRetries; ++retry) - { - Sleep(50); - DWORD pid = *pidBuffer; - if (pid) - { - m_hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid); - break; - } - } - } - } - CloseHandle(hMapFile); - } - } - } - - m_enabled = true; - } - - // Disable the powertoy - virtual void disable() - { - if (m_enabled) - { - TerminateProcess(m_hProcess, 1); - } - - m_enabled = false; - } - - // Returns if the powertoys is enabled - virtual bool is_enabled() override - { - return m_enabled; - } - - // Handle incoming event, data is event-specific - virtual intptr_t signal_event(const wchar_t* name, intptr_t data) override - { - if (wcscmp(name, ll_keyboard) == 0) - { - auto& event = *(reinterpret_cast(data)); - // Return 1 if the keypress is to be suppressed (not forwarded to Windows), - // otherwise return 0. - return 0; - } - else if (wcscmp(name, win_hook_event) == 0) - { - auto& event = *(reinterpret_cast(data)); - // Return value is ignored - return 0; - } - return 0; - } - - /* Register helper class to handle system menu items related actions. */ - virtual void register_system_menu_helper(PowertoySystemMenuIface* helper) {} - /* Handle action on system menu item. */ - virtual void signal_system_menu_action(const wchar_t* name) {} + /* Register helper class to handle system menu items related actions. */ + virtual void register_system_menu_helper(PowertoySystemMenuIface* helper) {} + /* Handle action on system menu item. */ + virtual void signal_system_menu_action(const wchar_t* name) {} }; // Load the settings file. -void Microsoft_Launcher::init_settings() -{ - try - { - // Load and parse the settings file for this PowerToy. - PowerToysSettings::PowerToyValues settings = - PowerToysSettings::PowerToyValues::load_from_settings_file(get_name()); - } - catch (std::exception ex) - { - // Error while loading from the settings file. Let default values stay as they are. - } +void Microsoft_Launcher::init_settings() { + try { + // Load and parse the settings file for this PowerToy. + PowerToysSettings::PowerToyValues settings = + PowerToysSettings::PowerToyValues::load_from_settings_file(get_name()); + + } + catch (std::exception ex) { + // Error while loading from the settings file. Let default values stay as they are. + } } -extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create() -{ - return new Microsoft_Launcher(); + +extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create() { + return new Microsoft_Launcher(); }