2020-12-15 15:16:09 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
|
#include <Windows.h>
|
|
|
|
|
#include <shellapi.h>
|
|
|
|
|
#include <sddl.h>
|
2021-06-22 15:24:03 +03:00
|
|
|
#include <shldisp.h>
|
|
|
|
|
#include <shlobj.h>
|
|
|
|
|
#include <exdisp.h>
|
|
|
|
|
#include <atlbase.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <comdef.h>
|
2020-12-15 15:16:09 +03:00
|
|
|
|
2021-06-14 12:55:59 +03:00
|
|
|
#include <winrt/base.h>
|
|
|
|
|
#include <winrt/Windows.Foundation.Collections.h>
|
|
|
|
|
|
2020-12-15 15:16:09 +03:00
|
|
|
#include <string>
|
2022-04-19 22:00:28 +02:00
|
|
|
#include <filesystem>
|
|
|
|
|
|
2021-05-21 17:24:33 +03:00
|
|
|
#include <common/logger/logger.h>
|
|
|
|
|
#include <common/utils/winapi_error.h>
|
2022-04-19 22:00:28 +02:00
|
|
|
#include <common/utils/process_path.h>
|
|
|
|
|
#include <common/utils/processApi.h>
|
2020-12-15 15:16:09 +03:00
|
|
|
|
2022-04-19 22:00:28 +02:00
|
|
|
namespace
|
2021-06-22 15:24:03 +03:00
|
|
|
{
|
|
|
|
|
inline std::wstring GetErrorString(HRESULT handle)
|
|
|
|
|
{
|
2022-04-19 22:00:28 +02:00
|
|
|
_com_error err(handle);
|
|
|
|
|
return err.ErrorMessage();
|
2021-06-22 15:24:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline bool FindDesktopFolderView(REFIID riid, void** ppv)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IShellWindows> spShellWindows;
|
|
|
|
|
auto result = spShellWindows.CoCreateInstance(CLSID_ShellWindows);
|
2022-12-20 19:29:24 +00:00
|
|
|
if (result != S_OK || spShellWindows == nullptr)
|
2021-06-22 15:24:03 +03:00
|
|
|
{
|
|
|
|
|
Logger::warn(L"Failed to create instance. {}", GetErrorString(result));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CComVariant vtLoc(CSIDL_DESKTOP);
|
|
|
|
|
CComVariant vtEmpty;
|
|
|
|
|
long lhwnd;
|
|
|
|
|
CComPtr<IDispatch> spdisp;
|
|
|
|
|
result = spShellWindows->FindWindowSW(
|
|
|
|
|
&vtLoc, &vtEmpty, SWC_DESKTOP, &lhwnd, SWFO_NEEDDISPATCH, &spdisp);
|
|
|
|
|
|
2022-12-20 19:29:24 +00:00
|
|
|
if (result != S_OK || spdisp == nullptr)
|
2021-06-22 15:24:03 +03:00
|
|
|
{
|
|
|
|
|
Logger::warn(L"Failed to find the window. {}", GetErrorString(result));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CComPtr<IShellBrowser> spBrowser;
|
|
|
|
|
result = CComQIPtr<IServiceProvider>(spdisp)->QueryService(SID_STopLevelBrowser,
|
2022-04-19 22:00:28 +02:00
|
|
|
IID_PPV_ARGS(&spBrowser));
|
2022-12-20 19:29:24 +00:00
|
|
|
if (result != S_OK || spBrowser == nullptr)
|
2021-06-22 15:24:03 +03:00
|
|
|
{
|
|
|
|
|
Logger::warn(L"Failed to query service. {}", GetErrorString(result));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CComPtr<IShellView> spView;
|
|
|
|
|
result = spBrowser->QueryActiveShellView(&spView);
|
2022-12-20 19:29:24 +00:00
|
|
|
if (result != S_OK || spView == nullptr)
|
2021-06-22 15:24:03 +03:00
|
|
|
{
|
|
|
|
|
Logger::warn(L"Failed to query active shell window. {}", GetErrorString(result));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = spView->QueryInterface(riid, ppv);
|
[KBM]Launch apps / URI with keyboard shortcuts, support chords (#30121)
* Working UI update with just runProgram Path and isRunProgram
* First working, basic. no args or path, or setting change detections.
* Revert and fixed.
* Some clean up, working with config file monitor
* Args and Start-in should be working.
* File monitor, quotes, xaml screens one
* Fixed enable/disable toogle from XAML
* Code cleanup.
* Betting logging.
* Cleanup, start of RunProgramDescriptor and usage of run_non_elevated/run_elevated
* Code moved to KeyboardEventHandlers, but not enabled since it won't build as is, needs elevation.h. Other testing..
* Key chords working, pretty much
* Added gui for elevation level, need to refresh on change...
* f: include shellapi.h and reference wil in KBMEL
* run_elevated/run_non_elevated sorted out. Working!
* Removed lots of old temp code.
* Fix some speling errors.
* Cleanup before trying to add a UI for the chord
* Added "DifferentUser" option
* Closer on UI for chords.
* Better UI, lots working.
* Clean up
* Text for “Allow chords” – needs to look better…
* Bugs and clean-up
* Cleanup
* Refactor and clean up.
* More clean up
* Some localization.
* Don’t show “Allow chords“ to the “to” shortcut
* Maybe better foreground after opening new app
* Better chord matching.
* Runprogram fix for stealing existing shortcut.
* Better runProgram stuff
* Temp commit
* Working well
* Toast test
* More toast
* Added File and Folder picker UI
* Pre-check on run program file exists.
* Refactor to SetupRunProgramControls
* Open URI UI is going.
* Open URI working well
* Open URI stuff working well
* Allowed AppSpecific shortcut and fixed backup/restore shortcut dups
* Fixed settings screen
* Start of code to find by name...
* UI fixed
* Small fixes
* Some single edit code working.
* UI getting better.
* Fixes
* Fixed and merge from main
* UI updates
* UI updates.
* UI stuff
* Fixed crash from move ui item locations.
* Fixed crash from move ui item locations.
* Added delete confirm
* Basic sound working.
* Localized some stuff
* Added sounds
* Better experiance when shortcut is in use.
* UI tweaks
* Fixed KBM ui for unicode shortcut not having ","
* Some clean up
* Cleanup
* Cleanup
* Fixed applyXamlStyling
* Added back stuff lost in merge
* applyXamlStyling, again
* Fixed crash on change from non shortcut to shortcut
* Update src/modules/keyboardmanager/KeyboardManagerEngineTest/KeyboardManagerEngineTest.vcxproj
* Fixed some spelling type issues.
* ImplementationLibrary 231216
* Comment bump to see if the Microsoft.Windows.ImplementationLibrary version thing gets picked up
* Correct, Microsoft.Windows.ImplementationLibrary, finally?
* Fixed two test that failed because we now allow key-chords.
* Removed shortcut sounds.
* use original behavior when "allow chords" is off in shortcut window
* fix crash when editing a shortcut that has apps specified for it
* split KBM chords with comma on dashboard page
* Fix some spelling items.
* More "spelling"
* Fix XAML styling
* align TextBlock and ToggleSwitch
* fix cutoff issue at the top
* increase ComboBox width
* Added *Unsupported* for backwards compat on config of KBM
* fix spellcheck
* Fix crash on Remap key screen
* Fixed Remap Keys ComboBox width too short.
* Removed KBM Single Edit mode, fixed crash.
* Fix Xaml with xaml cops
* Fix crash on setting "target app" for some types of shortcuts.
* Space to toggle chord, combobox back
* fix spellcheck
* fix some code nits
* Code review updates.
* Add exclusions to the bug report tool
* Code review and kill CloseAndEndTask
* Fix alignment / 3 comboboxes per row
* Fix daily telemetry events to exclude start app and open URI
* Add chords and remove app start and open uri from config telemetry
* comma instead of plus in human readable shortcut telemetry data
* Code review, restore default-old state when new row added in KBM
* Code review, restore default-old state when new row added in KBM, part 2
* Still show target app on Settings
* Only allow enabling chords for origin shortcuts
---------
Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com>
Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
2024-02-27 18:12:05 -05:00
|
|
|
if (result != S_OK || ppv == nullptr || *ppv == nullptr)
|
2021-06-22 15:24:03 +03:00
|
|
|
{
|
|
|
|
|
Logger::warn(L"Failed to query interface. {}", GetErrorString(result));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline bool GetDesktopAutomationObject(REFIID riid, void** ppv)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IShellView> spsv;
|
[KBM]Launch apps / URI with keyboard shortcuts, support chords (#30121)
* Working UI update with just runProgram Path and isRunProgram
* First working, basic. no args or path, or setting change detections.
* Revert and fixed.
* Some clean up, working with config file monitor
* Args and Start-in should be working.
* File monitor, quotes, xaml screens one
* Fixed enable/disable toogle from XAML
* Code cleanup.
* Betting logging.
* Cleanup, start of RunProgramDescriptor and usage of run_non_elevated/run_elevated
* Code moved to KeyboardEventHandlers, but not enabled since it won't build as is, needs elevation.h. Other testing..
* Key chords working, pretty much
* Added gui for elevation level, need to refresh on change...
* f: include shellapi.h and reference wil in KBMEL
* run_elevated/run_non_elevated sorted out. Working!
* Removed lots of old temp code.
* Fix some speling errors.
* Cleanup before trying to add a UI for the chord
* Added "DifferentUser" option
* Closer on UI for chords.
* Better UI, lots working.
* Clean up
* Text for “Allow chords” – needs to look better…
* Bugs and clean-up
* Cleanup
* Refactor and clean up.
* More clean up
* Some localization.
* Don’t show “Allow chords“ to the “to” shortcut
* Maybe better foreground after opening new app
* Better chord matching.
* Runprogram fix for stealing existing shortcut.
* Better runProgram stuff
* Temp commit
* Working well
* Toast test
* More toast
* Added File and Folder picker UI
* Pre-check on run program file exists.
* Refactor to SetupRunProgramControls
* Open URI UI is going.
* Open URI working well
* Open URI stuff working well
* Allowed AppSpecific shortcut and fixed backup/restore shortcut dups
* Fixed settings screen
* Start of code to find by name...
* UI fixed
* Small fixes
* Some single edit code working.
* UI getting better.
* Fixes
* Fixed and merge from main
* UI updates
* UI updates.
* UI stuff
* Fixed crash from move ui item locations.
* Fixed crash from move ui item locations.
* Added delete confirm
* Basic sound working.
* Localized some stuff
* Added sounds
* Better experiance when shortcut is in use.
* UI tweaks
* Fixed KBM ui for unicode shortcut not having ","
* Some clean up
* Cleanup
* Cleanup
* Fixed applyXamlStyling
* Added back stuff lost in merge
* applyXamlStyling, again
* Fixed crash on change from non shortcut to shortcut
* Update src/modules/keyboardmanager/KeyboardManagerEngineTest/KeyboardManagerEngineTest.vcxproj
* Fixed some spelling type issues.
* ImplementationLibrary 231216
* Comment bump to see if the Microsoft.Windows.ImplementationLibrary version thing gets picked up
* Correct, Microsoft.Windows.ImplementationLibrary, finally?
* Fixed two test that failed because we now allow key-chords.
* Removed shortcut sounds.
* use original behavior when "allow chords" is off in shortcut window
* fix crash when editing a shortcut that has apps specified for it
* split KBM chords with comma on dashboard page
* Fix some spelling items.
* More "spelling"
* Fix XAML styling
* align TextBlock and ToggleSwitch
* fix cutoff issue at the top
* increase ComboBox width
* Added *Unsupported* for backwards compat on config of KBM
* fix spellcheck
* Fix crash on Remap key screen
* Fixed Remap Keys ComboBox width too short.
* Removed KBM Single Edit mode, fixed crash.
* Fix Xaml with xaml cops
* Fix crash on setting "target app" for some types of shortcuts.
* Space to toggle chord, combobox back
* fix spellcheck
* fix some code nits
* Code review updates.
* Add exclusions to the bug report tool
* Code review and kill CloseAndEndTask
* Fix alignment / 3 comboboxes per row
* Fix daily telemetry events to exclude start app and open URI
* Add chords and remove app start and open uri from config telemetry
* comma instead of plus in human readable shortcut telemetry data
* Code review, restore default-old state when new row added in KBM
* Code review, restore default-old state when new row added in KBM, part 2
* Still show target app on Settings
* Only allow enabling chords for origin shortcuts
---------
Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com>
Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
2024-02-27 18:12:05 -05:00
|
|
|
|
2022-04-27 18:10:08 +02:00
|
|
|
// Desktop may not be available on startup
|
|
|
|
|
auto attempts = 5;
|
|
|
|
|
for (auto i = 1; i <= attempts; i++)
|
2021-06-22 15:24:03 +03:00
|
|
|
{
|
2022-04-27 18:10:08 +02:00
|
|
|
if (FindDesktopFolderView(IID_PPV_ARGS(&spsv)))
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger::warn(L"FindDesktopFolderView() failed attempt {}", i);
|
|
|
|
|
|
|
|
|
|
if (i == attempts)
|
|
|
|
|
{
|
|
|
|
|
Logger::warn(L"FindDesktopFolderView() max attempts reached");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Sleep(3000);
|
2021-06-22 15:24:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CComPtr<IDispatch> spdispView;
|
|
|
|
|
auto result = spsv->GetItemObject(SVGIO_BACKGROUND, IID_PPV_ARGS(&spdispView));
|
|
|
|
|
if (result != S_OK)
|
|
|
|
|
{
|
|
|
|
|
Logger::warn(L"GetItemObject() failed. {}", GetErrorString(result));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = spdispView->QueryInterface(riid, ppv);
|
|
|
|
|
if (result != S_OK)
|
|
|
|
|
{
|
|
|
|
|
Logger::warn(L"QueryInterface() failed. {}", GetErrorString(result));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline bool ShellExecuteFromExplorer(
|
|
|
|
|
PCWSTR pszFile,
|
2022-04-19 22:00:28 +02:00
|
|
|
PCWSTR pszParameters = nullptr,
|
|
|
|
|
PCWSTR workingDir = L"")
|
2021-06-22 15:24:03 +03:00
|
|
|
{
|
|
|
|
|
CComPtr<IShellFolderViewDual> spFolderView;
|
|
|
|
|
if (!GetDesktopAutomationObject(IID_PPV_ARGS(&spFolderView)))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CComPtr<IDispatch> spdispShell;
|
|
|
|
|
auto result = spFolderView->get_Application(&spdispShell);
|
|
|
|
|
if (result != S_OK)
|
|
|
|
|
{
|
|
|
|
|
Logger::warn(L"get_Application() failed. {}", GetErrorString(result));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CComQIPtr<IShellDispatch2>(spdispShell)
|
2022-04-19 22:00:28 +02:00
|
|
|
->ShellExecuteW(CComBSTR(pszFile),
|
|
|
|
|
CComVariant(pszParameters ? pszParameters : L""),
|
|
|
|
|
CComVariant(workingDir),
|
|
|
|
|
CComVariant(L""),
|
|
|
|
|
CComVariant(SW_SHOWNORMAL));
|
2021-06-22 15:24:03 +03:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-15 15:16:09 +03:00
|
|
|
// Returns true if the current process is running with elevated privileges
|
|
|
|
|
inline bool is_process_elevated(const bool use_cached_value = true)
|
|
|
|
|
{
|
|
|
|
|
auto detection_func = []() {
|
|
|
|
|
HANDLE token = nullptr;
|
|
|
|
|
bool elevated = false;
|
|
|
|
|
|
|
|
|
|
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token))
|
|
|
|
|
{
|
|
|
|
|
TOKEN_ELEVATION elevation;
|
|
|
|
|
DWORD size;
|
|
|
|
|
if (GetTokenInformation(token, TokenElevation, &elevation, sizeof(elevation), &size))
|
|
|
|
|
{
|
|
|
|
|
elevated = (elevation.TokenIsElevated != 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (token)
|
|
|
|
|
{
|
|
|
|
|
CloseHandle(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return elevated;
|
|
|
|
|
};
|
|
|
|
|
static const bool cached_value = detection_func();
|
|
|
|
|
return use_cached_value ? cached_value : detection_func();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Drops the elevated privileges if present
|
|
|
|
|
inline bool drop_elevated_privileges()
|
|
|
|
|
{
|
|
|
|
|
HANDLE token = nullptr;
|
|
|
|
|
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_DEFAULT | WRITE_OWNER, &token))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PSID medium_sid = NULL;
|
|
|
|
|
if (!::ConvertStringSidToSid(SDDL_ML_MEDIUM, &medium_sid))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TOKEN_MANDATORY_LABEL label = { 0 };
|
|
|
|
|
label.Label.Attributes = SE_GROUP_INTEGRITY;
|
|
|
|
|
label.Label.Sid = medium_sid;
|
2023-02-08 11:00:19 +00:00
|
|
|
DWORD size = static_cast<DWORD>(sizeof(TOKEN_MANDATORY_LABEL) + ::GetLengthSid(medium_sid));
|
2020-12-15 15:16:09 +03:00
|
|
|
|
|
|
|
|
BOOL result = SetTokenInformation(token, TokenIntegrityLevel, &label, size);
|
|
|
|
|
LocalFree(medium_sid);
|
|
|
|
|
CloseHandle(token);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
[KBM]Launch apps / URI with keyboard shortcuts, support chords (#30121)
* Working UI update with just runProgram Path and isRunProgram
* First working, basic. no args or path, or setting change detections.
* Revert and fixed.
* Some clean up, working with config file monitor
* Args and Start-in should be working.
* File monitor, quotes, xaml screens one
* Fixed enable/disable toogle from XAML
* Code cleanup.
* Betting logging.
* Cleanup, start of RunProgramDescriptor and usage of run_non_elevated/run_elevated
* Code moved to KeyboardEventHandlers, but not enabled since it won't build as is, needs elevation.h. Other testing..
* Key chords working, pretty much
* Added gui for elevation level, need to refresh on change...
* f: include shellapi.h and reference wil in KBMEL
* run_elevated/run_non_elevated sorted out. Working!
* Removed lots of old temp code.
* Fix some speling errors.
* Cleanup before trying to add a UI for the chord
* Added "DifferentUser" option
* Closer on UI for chords.
* Better UI, lots working.
* Clean up
* Text for “Allow chords” – needs to look better…
* Bugs and clean-up
* Cleanup
* Refactor and clean up.
* More clean up
* Some localization.
* Don’t show “Allow chords“ to the “to” shortcut
* Maybe better foreground after opening new app
* Better chord matching.
* Runprogram fix for stealing existing shortcut.
* Better runProgram stuff
* Temp commit
* Working well
* Toast test
* More toast
* Added File and Folder picker UI
* Pre-check on run program file exists.
* Refactor to SetupRunProgramControls
* Open URI UI is going.
* Open URI working well
* Open URI stuff working well
* Allowed AppSpecific shortcut and fixed backup/restore shortcut dups
* Fixed settings screen
* Start of code to find by name...
* UI fixed
* Small fixes
* Some single edit code working.
* UI getting better.
* Fixes
* Fixed and merge from main
* UI updates
* UI updates.
* UI stuff
* Fixed crash from move ui item locations.
* Fixed crash from move ui item locations.
* Added delete confirm
* Basic sound working.
* Localized some stuff
* Added sounds
* Better experiance when shortcut is in use.
* UI tweaks
* Fixed KBM ui for unicode shortcut not having ","
* Some clean up
* Cleanup
* Cleanup
* Fixed applyXamlStyling
* Added back stuff lost in merge
* applyXamlStyling, again
* Fixed crash on change from non shortcut to shortcut
* Update src/modules/keyboardmanager/KeyboardManagerEngineTest/KeyboardManagerEngineTest.vcxproj
* Fixed some spelling type issues.
* ImplementationLibrary 231216
* Comment bump to see if the Microsoft.Windows.ImplementationLibrary version thing gets picked up
* Correct, Microsoft.Windows.ImplementationLibrary, finally?
* Fixed two test that failed because we now allow key-chords.
* Removed shortcut sounds.
* use original behavior when "allow chords" is off in shortcut window
* fix crash when editing a shortcut that has apps specified for it
* split KBM chords with comma on dashboard page
* Fix some spelling items.
* More "spelling"
* Fix XAML styling
* align TextBlock and ToggleSwitch
* fix cutoff issue at the top
* increase ComboBox width
* Added *Unsupported* for backwards compat on config of KBM
* fix spellcheck
* Fix crash on Remap key screen
* Fixed Remap Keys ComboBox width too short.
* Removed KBM Single Edit mode, fixed crash.
* Fix Xaml with xaml cops
* Fix crash on setting "target app" for some types of shortcuts.
* Space to toggle chord, combobox back
* fix spellcheck
* fix some code nits
* Code review updates.
* Add exclusions to the bug report tool
* Code review and kill CloseAndEndTask
* Fix alignment / 3 comboboxes per row
* Fix daily telemetry events to exclude start app and open URI
* Add chords and remove app start and open uri from config telemetry
* comma instead of plus in human readable shortcut telemetry data
* Code review, restore default-old state when new row added in KBM
* Code review, restore default-old state when new row added in KBM, part 2
* Still show target app on Settings
* Only allow enabling chords for origin shortcuts
---------
Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com>
Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
2024-02-27 18:12:05 -05:00
|
|
|
// Run command as different user, returns true if succeeded
|
|
|
|
|
inline HANDLE run_as_different_user(const std::wstring& file, const std::wstring& params, const wchar_t* workingDir = nullptr, const bool showWindow = true)
|
|
|
|
|
{
|
|
|
|
|
Logger::info(L"run_elevated with params={}", params);
|
|
|
|
|
SHELLEXECUTEINFOW exec_info = { 0 };
|
|
|
|
|
exec_info.cbSize = sizeof(SHELLEXECUTEINFOW);
|
|
|
|
|
exec_info.lpVerb = L"runAsUser";
|
|
|
|
|
exec_info.lpFile = file.c_str();
|
|
|
|
|
exec_info.lpParameters = params.c_str();
|
|
|
|
|
exec_info.hwnd = 0;
|
|
|
|
|
exec_info.fMask = SEE_MASK_NOCLOSEPROCESS;
|
|
|
|
|
exec_info.lpDirectory = workingDir;
|
|
|
|
|
exec_info.hInstApp = 0;
|
|
|
|
|
if (showWindow)
|
|
|
|
|
{
|
|
|
|
|
exec_info.nShow = SW_SHOWDEFAULT;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// might have limited success, but only option with ShellExecuteExW
|
|
|
|
|
exec_info.nShow = SW_HIDE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ShellExecuteExW(&exec_info) ? exec_info.hProcess : nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-15 15:16:09 +03:00
|
|
|
// Run command as elevated user, returns true if succeeded
|
[KBM]Launch apps / URI with keyboard shortcuts, support chords (#30121)
* Working UI update with just runProgram Path and isRunProgram
* First working, basic. no args or path, or setting change detections.
* Revert and fixed.
* Some clean up, working with config file monitor
* Args and Start-in should be working.
* File monitor, quotes, xaml screens one
* Fixed enable/disable toogle from XAML
* Code cleanup.
* Betting logging.
* Cleanup, start of RunProgramDescriptor and usage of run_non_elevated/run_elevated
* Code moved to KeyboardEventHandlers, but not enabled since it won't build as is, needs elevation.h. Other testing..
* Key chords working, pretty much
* Added gui for elevation level, need to refresh on change...
* f: include shellapi.h and reference wil in KBMEL
* run_elevated/run_non_elevated sorted out. Working!
* Removed lots of old temp code.
* Fix some speling errors.
* Cleanup before trying to add a UI for the chord
* Added "DifferentUser" option
* Closer on UI for chords.
* Better UI, lots working.
* Clean up
* Text for “Allow chords” – needs to look better…
* Bugs and clean-up
* Cleanup
* Refactor and clean up.
* More clean up
* Some localization.
* Don’t show “Allow chords“ to the “to” shortcut
* Maybe better foreground after opening new app
* Better chord matching.
* Runprogram fix for stealing existing shortcut.
* Better runProgram stuff
* Temp commit
* Working well
* Toast test
* More toast
* Added File and Folder picker UI
* Pre-check on run program file exists.
* Refactor to SetupRunProgramControls
* Open URI UI is going.
* Open URI working well
* Open URI stuff working well
* Allowed AppSpecific shortcut and fixed backup/restore shortcut dups
* Fixed settings screen
* Start of code to find by name...
* UI fixed
* Small fixes
* Some single edit code working.
* UI getting better.
* Fixes
* Fixed and merge from main
* UI updates
* UI updates.
* UI stuff
* Fixed crash from move ui item locations.
* Fixed crash from move ui item locations.
* Added delete confirm
* Basic sound working.
* Localized some stuff
* Added sounds
* Better experiance when shortcut is in use.
* UI tweaks
* Fixed KBM ui for unicode shortcut not having ","
* Some clean up
* Cleanup
* Cleanup
* Fixed applyXamlStyling
* Added back stuff lost in merge
* applyXamlStyling, again
* Fixed crash on change from non shortcut to shortcut
* Update src/modules/keyboardmanager/KeyboardManagerEngineTest/KeyboardManagerEngineTest.vcxproj
* Fixed some spelling type issues.
* ImplementationLibrary 231216
* Comment bump to see if the Microsoft.Windows.ImplementationLibrary version thing gets picked up
* Correct, Microsoft.Windows.ImplementationLibrary, finally?
* Fixed two test that failed because we now allow key-chords.
* Removed shortcut sounds.
* use original behavior when "allow chords" is off in shortcut window
* fix crash when editing a shortcut that has apps specified for it
* split KBM chords with comma on dashboard page
* Fix some spelling items.
* More "spelling"
* Fix XAML styling
* align TextBlock and ToggleSwitch
* fix cutoff issue at the top
* increase ComboBox width
* Added *Unsupported* for backwards compat on config of KBM
* fix spellcheck
* Fix crash on Remap key screen
* Fixed Remap Keys ComboBox width too short.
* Removed KBM Single Edit mode, fixed crash.
* Fix Xaml with xaml cops
* Fix crash on setting "target app" for some types of shortcuts.
* Space to toggle chord, combobox back
* fix spellcheck
* fix some code nits
* Code review updates.
* Add exclusions to the bug report tool
* Code review and kill CloseAndEndTask
* Fix alignment / 3 comboboxes per row
* Fix daily telemetry events to exclude start app and open URI
* Add chords and remove app start and open uri from config telemetry
* comma instead of plus in human readable shortcut telemetry data
* Code review, restore default-old state when new row added in KBM
* Code review, restore default-old state when new row added in KBM, part 2
* Still show target app on Settings
* Only allow enabling chords for origin shortcuts
---------
Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com>
Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
2024-02-27 18:12:05 -05:00
|
|
|
inline HANDLE run_elevated(const std::wstring& file, const std::wstring& params, const wchar_t* workingDir = nullptr, const bool showWindow = true)
|
2020-12-15 15:16:09 +03:00
|
|
|
{
|
2022-02-22 12:50:20 +01:00
|
|
|
Logger::info(L"run_elevated with params={}", params);
|
2020-12-15 15:16:09 +03:00
|
|
|
SHELLEXECUTEINFOW exec_info = { 0 };
|
|
|
|
|
exec_info.cbSize = sizeof(SHELLEXECUTEINFOW);
|
|
|
|
|
exec_info.lpVerb = L"runas";
|
|
|
|
|
exec_info.lpFile = file.c_str();
|
|
|
|
|
exec_info.lpParameters = params.c_str();
|
|
|
|
|
exec_info.hwnd = 0;
|
|
|
|
|
exec_info.fMask = SEE_MASK_NOCLOSEPROCESS;
|
[KBM]Launch apps / URI with keyboard shortcuts, support chords (#30121)
* Working UI update with just runProgram Path and isRunProgram
* First working, basic. no args or path, or setting change detections.
* Revert and fixed.
* Some clean up, working with config file monitor
* Args and Start-in should be working.
* File monitor, quotes, xaml screens one
* Fixed enable/disable toogle from XAML
* Code cleanup.
* Betting logging.
* Cleanup, start of RunProgramDescriptor and usage of run_non_elevated/run_elevated
* Code moved to KeyboardEventHandlers, but not enabled since it won't build as is, needs elevation.h. Other testing..
* Key chords working, pretty much
* Added gui for elevation level, need to refresh on change...
* f: include shellapi.h and reference wil in KBMEL
* run_elevated/run_non_elevated sorted out. Working!
* Removed lots of old temp code.
* Fix some speling errors.
* Cleanup before trying to add a UI for the chord
* Added "DifferentUser" option
* Closer on UI for chords.
* Better UI, lots working.
* Clean up
* Text for “Allow chords” – needs to look better…
* Bugs and clean-up
* Cleanup
* Refactor and clean up.
* More clean up
* Some localization.
* Don’t show “Allow chords“ to the “to” shortcut
* Maybe better foreground after opening new app
* Better chord matching.
* Runprogram fix for stealing existing shortcut.
* Better runProgram stuff
* Temp commit
* Working well
* Toast test
* More toast
* Added File and Folder picker UI
* Pre-check on run program file exists.
* Refactor to SetupRunProgramControls
* Open URI UI is going.
* Open URI working well
* Open URI stuff working well
* Allowed AppSpecific shortcut and fixed backup/restore shortcut dups
* Fixed settings screen
* Start of code to find by name...
* UI fixed
* Small fixes
* Some single edit code working.
* UI getting better.
* Fixes
* Fixed and merge from main
* UI updates
* UI updates.
* UI stuff
* Fixed crash from move ui item locations.
* Fixed crash from move ui item locations.
* Added delete confirm
* Basic sound working.
* Localized some stuff
* Added sounds
* Better experiance when shortcut is in use.
* UI tweaks
* Fixed KBM ui for unicode shortcut not having ","
* Some clean up
* Cleanup
* Cleanup
* Fixed applyXamlStyling
* Added back stuff lost in merge
* applyXamlStyling, again
* Fixed crash on change from non shortcut to shortcut
* Update src/modules/keyboardmanager/KeyboardManagerEngineTest/KeyboardManagerEngineTest.vcxproj
* Fixed some spelling type issues.
* ImplementationLibrary 231216
* Comment bump to see if the Microsoft.Windows.ImplementationLibrary version thing gets picked up
* Correct, Microsoft.Windows.ImplementationLibrary, finally?
* Fixed two test that failed because we now allow key-chords.
* Removed shortcut sounds.
* use original behavior when "allow chords" is off in shortcut window
* fix crash when editing a shortcut that has apps specified for it
* split KBM chords with comma on dashboard page
* Fix some spelling items.
* More "spelling"
* Fix XAML styling
* align TextBlock and ToggleSwitch
* fix cutoff issue at the top
* increase ComboBox width
* Added *Unsupported* for backwards compat on config of KBM
* fix spellcheck
* Fix crash on Remap key screen
* Fixed Remap Keys ComboBox width too short.
* Removed KBM Single Edit mode, fixed crash.
* Fix Xaml with xaml cops
* Fix crash on setting "target app" for some types of shortcuts.
* Space to toggle chord, combobox back
* fix spellcheck
* fix some code nits
* Code review updates.
* Add exclusions to the bug report tool
* Code review and kill CloseAndEndTask
* Fix alignment / 3 comboboxes per row
* Fix daily telemetry events to exclude start app and open URI
* Add chords and remove app start and open uri from config telemetry
* comma instead of plus in human readable shortcut telemetry data
* Code review, restore default-old state when new row added in KBM
* Code review, restore default-old state when new row added in KBM, part 2
* Still show target app on Settings
* Only allow enabling chords for origin shortcuts
---------
Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com>
Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
2024-02-27 18:12:05 -05:00
|
|
|
exec_info.lpDirectory = workingDir;
|
2020-12-15 15:16:09 +03:00
|
|
|
exec_info.hInstApp = 0;
|
[KBM]Launch apps / URI with keyboard shortcuts, support chords (#30121)
* Working UI update with just runProgram Path and isRunProgram
* First working, basic. no args or path, or setting change detections.
* Revert and fixed.
* Some clean up, working with config file monitor
* Args and Start-in should be working.
* File monitor, quotes, xaml screens one
* Fixed enable/disable toogle from XAML
* Code cleanup.
* Betting logging.
* Cleanup, start of RunProgramDescriptor and usage of run_non_elevated/run_elevated
* Code moved to KeyboardEventHandlers, but not enabled since it won't build as is, needs elevation.h. Other testing..
* Key chords working, pretty much
* Added gui for elevation level, need to refresh on change...
* f: include shellapi.h and reference wil in KBMEL
* run_elevated/run_non_elevated sorted out. Working!
* Removed lots of old temp code.
* Fix some speling errors.
* Cleanup before trying to add a UI for the chord
* Added "DifferentUser" option
* Closer on UI for chords.
* Better UI, lots working.
* Clean up
* Text for “Allow chords” – needs to look better…
* Bugs and clean-up
* Cleanup
* Refactor and clean up.
* More clean up
* Some localization.
* Don’t show “Allow chords“ to the “to” shortcut
* Maybe better foreground after opening new app
* Better chord matching.
* Runprogram fix for stealing existing shortcut.
* Better runProgram stuff
* Temp commit
* Working well
* Toast test
* More toast
* Added File and Folder picker UI
* Pre-check on run program file exists.
* Refactor to SetupRunProgramControls
* Open URI UI is going.
* Open URI working well
* Open URI stuff working well
* Allowed AppSpecific shortcut and fixed backup/restore shortcut dups
* Fixed settings screen
* Start of code to find by name...
* UI fixed
* Small fixes
* Some single edit code working.
* UI getting better.
* Fixes
* Fixed and merge from main
* UI updates
* UI updates.
* UI stuff
* Fixed crash from move ui item locations.
* Fixed crash from move ui item locations.
* Added delete confirm
* Basic sound working.
* Localized some stuff
* Added sounds
* Better experiance when shortcut is in use.
* UI tweaks
* Fixed KBM ui for unicode shortcut not having ","
* Some clean up
* Cleanup
* Cleanup
* Fixed applyXamlStyling
* Added back stuff lost in merge
* applyXamlStyling, again
* Fixed crash on change from non shortcut to shortcut
* Update src/modules/keyboardmanager/KeyboardManagerEngineTest/KeyboardManagerEngineTest.vcxproj
* Fixed some spelling type issues.
* ImplementationLibrary 231216
* Comment bump to see if the Microsoft.Windows.ImplementationLibrary version thing gets picked up
* Correct, Microsoft.Windows.ImplementationLibrary, finally?
* Fixed two test that failed because we now allow key-chords.
* Removed shortcut sounds.
* use original behavior when "allow chords" is off in shortcut window
* fix crash when editing a shortcut that has apps specified for it
* split KBM chords with comma on dashboard page
* Fix some spelling items.
* More "spelling"
* Fix XAML styling
* align TextBlock and ToggleSwitch
* fix cutoff issue at the top
* increase ComboBox width
* Added *Unsupported* for backwards compat on config of KBM
* fix spellcheck
* Fix crash on Remap key screen
* Fixed Remap Keys ComboBox width too short.
* Removed KBM Single Edit mode, fixed crash.
* Fix Xaml with xaml cops
* Fix crash on setting "target app" for some types of shortcuts.
* Space to toggle chord, combobox back
* fix spellcheck
* fix some code nits
* Code review updates.
* Add exclusions to the bug report tool
* Code review and kill CloseAndEndTask
* Fix alignment / 3 comboboxes per row
* Fix daily telemetry events to exclude start app and open URI
* Add chords and remove app start and open uri from config telemetry
* comma instead of plus in human readable shortcut telemetry data
* Code review, restore default-old state when new row added in KBM
* Code review, restore default-old state when new row added in KBM, part 2
* Still show target app on Settings
* Only allow enabling chords for origin shortcuts
---------
Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com>
Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
2024-02-27 18:12:05 -05:00
|
|
|
|
|
|
|
|
if (showWindow)
|
|
|
|
|
{
|
|
|
|
|
exec_info.nShow = SW_SHOWDEFAULT;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// might have limited success, but only option with ShellExecuteExW
|
|
|
|
|
exec_info.nShow = SW_HIDE;
|
|
|
|
|
}
|
2020-12-15 15:16:09 +03:00
|
|
|
|
[New Module] Light Switch (#41987)
<!-- Enter a brief description/summary of your PR here. What does it
fix/what does it change/how was it tested (even manually, if necessary)?
-->
## Summary of the Pull Request
This pull request introduces a new module called "Light Switch" which
allows users to automatically switch between light and dark mode on a
timer.

<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist
- [x] Closes: #1331
- [x] **Communication:** I've discussed this with core contributors
already. If the work hasn't been agreed, this work might be rejected
- [x] **Tests:** Added/updated and all pass
- [x] **Localization:** All end-user-facing strings can be localized
- [x] **Dev docs:** Added/updated
- [x] **New binaries:** Added on the required places
- [x] [JSON for
signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json)
for new binaries
- [x] [WXS for
installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs)
for new binaries and localization folder
- [x] **Documentation updated:** If checked, please file a pull request
on [our docs
repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys)
and link it here:
[#5867](https://github.com/MicrosoftDocs/windows-dev-docs-pr/pull/5867)
<!-- Provide a more detailed description of the PR, other things fixed,
or any additional comments/features here -->
## Detailed Description of the Pull Request / Additional comments
### Known bugs:
- Default settings not saving correctly when switching modes
- Issue: Sometimes when you switch from one mode to another, they are
supposed to update with new defaults but sometimes this fails for the
second variable. Potentially has to do with accessing the settings file
while another chunk of code is still updating.
- Sometimes the system looks "glitched" when switching themes
### To do:
- [x] OOBE page and assets
- [x] Logic to disable the chart when no location has been selected
- [x] Localization
### How to and what to test
Grab the latest installer from the pipeline below for your architecture
and install PowerToys from there.
- Toggle theme shortcutSystem only, Apps only, Both system and apps
selected
- Does changing the values on the settings page update the settings
file? %LOCALAPPDATA%/Microsoft/PowerToys/LightSwitch/settings.json
- Manual mode: System only, Apps only, Both system and apps selected
- Sunrise modes: Are the times accurate?
- If you manage to let this run through sunset/rise does the theme
change?
- Set your theme to change within the next minute using manual mode and
set your device to sleepOpen your device and login once the time you set
has passed. --> Do your settings resync once the next minute ticks after
logging back into your device?
- Disable the service and ensure the tasks actually ends.
- While the module is disabled:
- Make sure the shortcut no longer works
- Make sure the last time you set doesn't trigger a theme change
- Bonus: Toggle GPO Configuration and make sure you are unable to enable
the module
---------
Co-authored-by: Niels Laute <niels.laute@live.nl>
Co-authored-by: Gordon Lam (SH) <yeelam@microsoft.com>
2025-10-06 16:44:07 -04:00
|
|
|
BOOL result = ShellExecuteExW(&exec_info);
|
|
|
|
|
|
|
|
|
|
return result ? exec_info.hProcess : nullptr;
|
2020-12-15 15:16:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run command as non-elevated user, returns true if succeeded, puts the process id into returnPid if returnPid != NULL
|
[KBM]Launch apps / URI with keyboard shortcuts, support chords (#30121)
* Working UI update with just runProgram Path and isRunProgram
* First working, basic. no args or path, or setting change detections.
* Revert and fixed.
* Some clean up, working with config file monitor
* Args and Start-in should be working.
* File monitor, quotes, xaml screens one
* Fixed enable/disable toogle from XAML
* Code cleanup.
* Betting logging.
* Cleanup, start of RunProgramDescriptor and usage of run_non_elevated/run_elevated
* Code moved to KeyboardEventHandlers, but not enabled since it won't build as is, needs elevation.h. Other testing..
* Key chords working, pretty much
* Added gui for elevation level, need to refresh on change...
* f: include shellapi.h and reference wil in KBMEL
* run_elevated/run_non_elevated sorted out. Working!
* Removed lots of old temp code.
* Fix some speling errors.
* Cleanup before trying to add a UI for the chord
* Added "DifferentUser" option
* Closer on UI for chords.
* Better UI, lots working.
* Clean up
* Text for “Allow chords” – needs to look better…
* Bugs and clean-up
* Cleanup
* Refactor and clean up.
* More clean up
* Some localization.
* Don’t show “Allow chords“ to the “to” shortcut
* Maybe better foreground after opening new app
* Better chord matching.
* Runprogram fix for stealing existing shortcut.
* Better runProgram stuff
* Temp commit
* Working well
* Toast test
* More toast
* Added File and Folder picker UI
* Pre-check on run program file exists.
* Refactor to SetupRunProgramControls
* Open URI UI is going.
* Open URI working well
* Open URI stuff working well
* Allowed AppSpecific shortcut and fixed backup/restore shortcut dups
* Fixed settings screen
* Start of code to find by name...
* UI fixed
* Small fixes
* Some single edit code working.
* UI getting better.
* Fixes
* Fixed and merge from main
* UI updates
* UI updates.
* UI stuff
* Fixed crash from move ui item locations.
* Fixed crash from move ui item locations.
* Added delete confirm
* Basic sound working.
* Localized some stuff
* Added sounds
* Better experiance when shortcut is in use.
* UI tweaks
* Fixed KBM ui for unicode shortcut not having ","
* Some clean up
* Cleanup
* Cleanup
* Fixed applyXamlStyling
* Added back stuff lost in merge
* applyXamlStyling, again
* Fixed crash on change from non shortcut to shortcut
* Update src/modules/keyboardmanager/KeyboardManagerEngineTest/KeyboardManagerEngineTest.vcxproj
* Fixed some spelling type issues.
* ImplementationLibrary 231216
* Comment bump to see if the Microsoft.Windows.ImplementationLibrary version thing gets picked up
* Correct, Microsoft.Windows.ImplementationLibrary, finally?
* Fixed two test that failed because we now allow key-chords.
* Removed shortcut sounds.
* use original behavior when "allow chords" is off in shortcut window
* fix crash when editing a shortcut that has apps specified for it
* split KBM chords with comma on dashboard page
* Fix some spelling items.
* More "spelling"
* Fix XAML styling
* align TextBlock and ToggleSwitch
* fix cutoff issue at the top
* increase ComboBox width
* Added *Unsupported* for backwards compat on config of KBM
* fix spellcheck
* Fix crash on Remap key screen
* Fixed Remap Keys ComboBox width too short.
* Removed KBM Single Edit mode, fixed crash.
* Fix Xaml with xaml cops
* Fix crash on setting "target app" for some types of shortcuts.
* Space to toggle chord, combobox back
* fix spellcheck
* fix some code nits
* Code review updates.
* Add exclusions to the bug report tool
* Code review and kill CloseAndEndTask
* Fix alignment / 3 comboboxes per row
* Fix daily telemetry events to exclude start app and open URI
* Add chords and remove app start and open uri from config telemetry
* comma instead of plus in human readable shortcut telemetry data
* Code review, restore default-old state when new row added in KBM
* Code review, restore default-old state when new row added in KBM, part 2
* Still show target app on Settings
* Only allow enabling chords for origin shortcuts
---------
Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com>
Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
2024-02-27 18:12:05 -05:00
|
|
|
inline bool run_non_elevated(const std::wstring& file, const std::wstring& params, DWORD* returnPid, const wchar_t* workingDir = nullptr, const bool showWindow = true)
|
2020-12-15 15:16:09 +03:00
|
|
|
{
|
2022-02-22 12:50:20 +01:00
|
|
|
Logger::info(L"run_non_elevated with params={}", params);
|
2020-12-15 15:16:09 +03:00
|
|
|
auto executable_args = L"\"" + file + L"\"";
|
|
|
|
|
if (!params.empty())
|
|
|
|
|
{
|
|
|
|
|
executable_args += L" " + params;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HWND hwnd = GetShellWindow();
|
|
|
|
|
if (!hwnd)
|
|
|
|
|
{
|
2021-06-10 14:08:56 +03:00
|
|
|
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()));
|
|
|
|
|
}
|
2022-04-19 22:00:28 +02:00
|
|
|
|
2020-12-15 15:16:09 +03:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
DWORD pid;
|
|
|
|
|
GetWindowThreadProcessId(hwnd, &pid);
|
|
|
|
|
|
|
|
|
|
winrt::handle process{ OpenProcess(PROCESS_CREATE_PROCESS, FALSE, pid) };
|
|
|
|
|
if (!process)
|
|
|
|
|
{
|
2021-05-21 17:24:33 +03:00
|
|
|
Logger::error(L"OpenProcess() failed. {}", get_last_error_or_default(GetLastError()));
|
2020-12-15 15:16:09 +03:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SIZE_T size = 0;
|
|
|
|
|
|
|
|
|
|
InitializeProcThreadAttributeList(nullptr, 1, 0, &size);
|
|
|
|
|
auto pproc_buffer = std::make_unique<char[]>(size);
|
|
|
|
|
auto pptal = reinterpret_cast<PPROC_THREAD_ATTRIBUTE_LIST>(pproc_buffer.get());
|
2021-05-21 17:24:33 +03:00
|
|
|
if (!pptal)
|
|
|
|
|
{
|
|
|
|
|
Logger::error(L"pptal failed to initialize. {}", get_last_error_or_default(GetLastError()));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-12-15 15:16:09 +03:00
|
|
|
|
|
|
|
|
if (!InitializeProcThreadAttributeList(pptal, 1, 0, &size))
|
|
|
|
|
{
|
2021-05-21 17:24:33 +03:00
|
|
|
Logger::error(L"InitializeProcThreadAttributeList() failed. {}", get_last_error_or_default(GetLastError()));
|
2020-12-15 15:16:09 +03:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HANDLE process_handle = process.get();
|
2021-05-21 17:24:33 +03:00
|
|
|
if (!UpdateProcThreadAttribute(pptal,
|
|
|
|
|
0,
|
|
|
|
|
PROC_THREAD_ATTRIBUTE_PARENT_PROCESS,
|
|
|
|
|
&process_handle,
|
|
|
|
|
sizeof(process_handle),
|
|
|
|
|
nullptr,
|
|
|
|
|
nullptr))
|
2020-12-15 15:16:09 +03:00
|
|
|
{
|
2021-05-21 17:24:33 +03:00
|
|
|
Logger::error(L"UpdateProcThreadAttribute() failed. {}", get_last_error_or_default(GetLastError()));
|
2020-12-15 15:16:09 +03:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
STARTUPINFOEX siex = { 0 };
|
|
|
|
|
siex.lpAttributeList = pptal;
|
|
|
|
|
siex.StartupInfo.cb = sizeof(siex);
|
|
|
|
|
PROCESS_INFORMATION pi = { 0 };
|
[KBM]Launch apps / URI with keyboard shortcuts, support chords (#30121)
* Working UI update with just runProgram Path and isRunProgram
* First working, basic. no args or path, or setting change detections.
* Revert and fixed.
* Some clean up, working with config file monitor
* Args and Start-in should be working.
* File monitor, quotes, xaml screens one
* Fixed enable/disable toogle from XAML
* Code cleanup.
* Betting logging.
* Cleanup, start of RunProgramDescriptor and usage of run_non_elevated/run_elevated
* Code moved to KeyboardEventHandlers, but not enabled since it won't build as is, needs elevation.h. Other testing..
* Key chords working, pretty much
* Added gui for elevation level, need to refresh on change...
* f: include shellapi.h and reference wil in KBMEL
* run_elevated/run_non_elevated sorted out. Working!
* Removed lots of old temp code.
* Fix some speling errors.
* Cleanup before trying to add a UI for the chord
* Added "DifferentUser" option
* Closer on UI for chords.
* Better UI, lots working.
* Clean up
* Text for “Allow chords” – needs to look better…
* Bugs and clean-up
* Cleanup
* Refactor and clean up.
* More clean up
* Some localization.
* Don’t show “Allow chords“ to the “to” shortcut
* Maybe better foreground after opening new app
* Better chord matching.
* Runprogram fix for stealing existing shortcut.
* Better runProgram stuff
* Temp commit
* Working well
* Toast test
* More toast
* Added File and Folder picker UI
* Pre-check on run program file exists.
* Refactor to SetupRunProgramControls
* Open URI UI is going.
* Open URI working well
* Open URI stuff working well
* Allowed AppSpecific shortcut and fixed backup/restore shortcut dups
* Fixed settings screen
* Start of code to find by name...
* UI fixed
* Small fixes
* Some single edit code working.
* UI getting better.
* Fixes
* Fixed and merge from main
* UI updates
* UI updates.
* UI stuff
* Fixed crash from move ui item locations.
* Fixed crash from move ui item locations.
* Added delete confirm
* Basic sound working.
* Localized some stuff
* Added sounds
* Better experiance when shortcut is in use.
* UI tweaks
* Fixed KBM ui for unicode shortcut not having ","
* Some clean up
* Cleanup
* Cleanup
* Fixed applyXamlStyling
* Added back stuff lost in merge
* applyXamlStyling, again
* Fixed crash on change from non shortcut to shortcut
* Update src/modules/keyboardmanager/KeyboardManagerEngineTest/KeyboardManagerEngineTest.vcxproj
* Fixed some spelling type issues.
* ImplementationLibrary 231216
* Comment bump to see if the Microsoft.Windows.ImplementationLibrary version thing gets picked up
* Correct, Microsoft.Windows.ImplementationLibrary, finally?
* Fixed two test that failed because we now allow key-chords.
* Removed shortcut sounds.
* use original behavior when "allow chords" is off in shortcut window
* fix crash when editing a shortcut that has apps specified for it
* split KBM chords with comma on dashboard page
* Fix some spelling items.
* More "spelling"
* Fix XAML styling
* align TextBlock and ToggleSwitch
* fix cutoff issue at the top
* increase ComboBox width
* Added *Unsupported* for backwards compat on config of KBM
* fix spellcheck
* Fix crash on Remap key screen
* Fixed Remap Keys ComboBox width too short.
* Removed KBM Single Edit mode, fixed crash.
* Fix Xaml with xaml cops
* Fix crash on setting "target app" for some types of shortcuts.
* Space to toggle chord, combobox back
* fix spellcheck
* fix some code nits
* Code review updates.
* Add exclusions to the bug report tool
* Code review and kill CloseAndEndTask
* Fix alignment / 3 comboboxes per row
* Fix daily telemetry events to exclude start app and open URI
* Add chords and remove app start and open uri from config telemetry
* comma instead of plus in human readable shortcut telemetry data
* Code review, restore default-old state when new row added in KBM
* Code review, restore default-old state when new row added in KBM, part 2
* Still show target app on Settings
* Only allow enabling chords for origin shortcuts
---------
Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com>
Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
2024-02-27 18:12:05 -05:00
|
|
|
auto dwCreationFlags = EXTENDED_STARTUPINFO_PRESENT;
|
|
|
|
|
|
|
|
|
|
if (!showWindow)
|
|
|
|
|
{
|
|
|
|
|
siex.StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
|
|
|
|
|
siex.StartupInfo.wShowWindow = SW_HIDE;
|
|
|
|
|
dwCreationFlags = CREATE_NO_WINDOW;
|
|
|
|
|
}
|
2023-02-08 11:54:16 +00:00
|
|
|
|
2020-12-15 15:16:09 +03:00
|
|
|
auto succeeded = CreateProcessW(file.c_str(),
|
2023-02-08 11:54:16 +00:00
|
|
|
&executable_args[0],
|
2020-12-15 15:16:09 +03:00
|
|
|
nullptr,
|
|
|
|
|
nullptr,
|
|
|
|
|
FALSE,
|
[KBM]Launch apps / URI with keyboard shortcuts, support chords (#30121)
* Working UI update with just runProgram Path and isRunProgram
* First working, basic. no args or path, or setting change detections.
* Revert and fixed.
* Some clean up, working with config file monitor
* Args and Start-in should be working.
* File monitor, quotes, xaml screens one
* Fixed enable/disable toogle from XAML
* Code cleanup.
* Betting logging.
* Cleanup, start of RunProgramDescriptor and usage of run_non_elevated/run_elevated
* Code moved to KeyboardEventHandlers, but not enabled since it won't build as is, needs elevation.h. Other testing..
* Key chords working, pretty much
* Added gui for elevation level, need to refresh on change...
* f: include shellapi.h and reference wil in KBMEL
* run_elevated/run_non_elevated sorted out. Working!
* Removed lots of old temp code.
* Fix some speling errors.
* Cleanup before trying to add a UI for the chord
* Added "DifferentUser" option
* Closer on UI for chords.
* Better UI, lots working.
* Clean up
* Text for “Allow chords” – needs to look better…
* Bugs and clean-up
* Cleanup
* Refactor and clean up.
* More clean up
* Some localization.
* Don’t show “Allow chords“ to the “to” shortcut
* Maybe better foreground after opening new app
* Better chord matching.
* Runprogram fix for stealing existing shortcut.
* Better runProgram stuff
* Temp commit
* Working well
* Toast test
* More toast
* Added File and Folder picker UI
* Pre-check on run program file exists.
* Refactor to SetupRunProgramControls
* Open URI UI is going.
* Open URI working well
* Open URI stuff working well
* Allowed AppSpecific shortcut and fixed backup/restore shortcut dups
* Fixed settings screen
* Start of code to find by name...
* UI fixed
* Small fixes
* Some single edit code working.
* UI getting better.
* Fixes
* Fixed and merge from main
* UI updates
* UI updates.
* UI stuff
* Fixed crash from move ui item locations.
* Fixed crash from move ui item locations.
* Added delete confirm
* Basic sound working.
* Localized some stuff
* Added sounds
* Better experiance when shortcut is in use.
* UI tweaks
* Fixed KBM ui for unicode shortcut not having ","
* Some clean up
* Cleanup
* Cleanup
* Fixed applyXamlStyling
* Added back stuff lost in merge
* applyXamlStyling, again
* Fixed crash on change from non shortcut to shortcut
* Update src/modules/keyboardmanager/KeyboardManagerEngineTest/KeyboardManagerEngineTest.vcxproj
* Fixed some spelling type issues.
* ImplementationLibrary 231216
* Comment bump to see if the Microsoft.Windows.ImplementationLibrary version thing gets picked up
* Correct, Microsoft.Windows.ImplementationLibrary, finally?
* Fixed two test that failed because we now allow key-chords.
* Removed shortcut sounds.
* use original behavior when "allow chords" is off in shortcut window
* fix crash when editing a shortcut that has apps specified for it
* split KBM chords with comma on dashboard page
* Fix some spelling items.
* More "spelling"
* Fix XAML styling
* align TextBlock and ToggleSwitch
* fix cutoff issue at the top
* increase ComboBox width
* Added *Unsupported* for backwards compat on config of KBM
* fix spellcheck
* Fix crash on Remap key screen
* Fixed Remap Keys ComboBox width too short.
* Removed KBM Single Edit mode, fixed crash.
* Fix Xaml with xaml cops
* Fix crash on setting "target app" for some types of shortcuts.
* Space to toggle chord, combobox back
* fix spellcheck
* fix some code nits
* Code review updates.
* Add exclusions to the bug report tool
* Code review and kill CloseAndEndTask
* Fix alignment / 3 comboboxes per row
* Fix daily telemetry events to exclude start app and open URI
* Add chords and remove app start and open uri from config telemetry
* comma instead of plus in human readable shortcut telemetry data
* Code review, restore default-old state when new row added in KBM
* Code review, restore default-old state when new row added in KBM, part 2
* Still show target app on Settings
* Only allow enabling chords for origin shortcuts
---------
Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com>
Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
2024-02-27 18:12:05 -05:00
|
|
|
dwCreationFlags,
|
2020-12-15 15:16:09 +03:00
|
|
|
nullptr,
|
2022-04-19 22:00:28 +02:00
|
|
|
workingDir,
|
2020-12-15 15:16:09 +03:00
|
|
|
&siex.StartupInfo,
|
|
|
|
|
&pi);
|
|
|
|
|
if (succeeded)
|
|
|
|
|
{
|
|
|
|
|
if (pi.hProcess)
|
|
|
|
|
{
|
|
|
|
|
if (returnPid)
|
|
|
|
|
{
|
|
|
|
|
*returnPid = GetProcessId(pi.hProcess);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CloseHandle(pi.hProcess);
|
|
|
|
|
}
|
|
|
|
|
if (pi.hThread)
|
|
|
|
|
{
|
|
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-21 17:24:33 +03:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger::error(L"CreateProcessW() failed. {}", get_last_error_or_default(GetLastError()));
|
|
|
|
|
}
|
2020-12-15 15:16:09 +03:00
|
|
|
|
|
|
|
|
return succeeded;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-19 22:00:28 +02:00
|
|
|
inline bool RunNonElevatedEx(const std::wstring& file, const std::wstring& params, const std::wstring& working_dir)
|
2021-06-22 15:24:03 +03:00
|
|
|
{
|
2023-08-03 19:44:17 +02:00
|
|
|
bool success = false;
|
|
|
|
|
HRESULT co_init = E_FAIL;
|
2021-06-22 15:24:03 +03:00
|
|
|
try
|
|
|
|
|
{
|
2023-08-03 19:44:17 +02:00
|
|
|
co_init = CoInitialize(nullptr);
|
|
|
|
|
success = ShellExecuteFromExplorer(file.c_str(), params.c_str(), working_dir.c_str());
|
2021-06-22 15:24:03 +03:00
|
|
|
}
|
2022-04-19 22:00:28 +02:00
|
|
|
catch (...)
|
2021-06-22 15:24:03 +03:00
|
|
|
{
|
|
|
|
|
}
|
[KBM]Launch apps / URI with keyboard shortcuts, support chords (#30121)
* Working UI update with just runProgram Path and isRunProgram
* First working, basic. no args or path, or setting change detections.
* Revert and fixed.
* Some clean up, working with config file monitor
* Args and Start-in should be working.
* File monitor, quotes, xaml screens one
* Fixed enable/disable toogle from XAML
* Code cleanup.
* Betting logging.
* Cleanup, start of RunProgramDescriptor and usage of run_non_elevated/run_elevated
* Code moved to KeyboardEventHandlers, but not enabled since it won't build as is, needs elevation.h. Other testing..
* Key chords working, pretty much
* Added gui for elevation level, need to refresh on change...
* f: include shellapi.h and reference wil in KBMEL
* run_elevated/run_non_elevated sorted out. Working!
* Removed lots of old temp code.
* Fix some speling errors.
* Cleanup before trying to add a UI for the chord
* Added "DifferentUser" option
* Closer on UI for chords.
* Better UI, lots working.
* Clean up
* Text for “Allow chords” – needs to look better…
* Bugs and clean-up
* Cleanup
* Refactor and clean up.
* More clean up
* Some localization.
* Don’t show “Allow chords“ to the “to” shortcut
* Maybe better foreground after opening new app
* Better chord matching.
* Runprogram fix for stealing existing shortcut.
* Better runProgram stuff
* Temp commit
* Working well
* Toast test
* More toast
* Added File and Folder picker UI
* Pre-check on run program file exists.
* Refactor to SetupRunProgramControls
* Open URI UI is going.
* Open URI working well
* Open URI stuff working well
* Allowed AppSpecific shortcut and fixed backup/restore shortcut dups
* Fixed settings screen
* Start of code to find by name...
* UI fixed
* Small fixes
* Some single edit code working.
* UI getting better.
* Fixes
* Fixed and merge from main
* UI updates
* UI updates.
* UI stuff
* Fixed crash from move ui item locations.
* Fixed crash from move ui item locations.
* Added delete confirm
* Basic sound working.
* Localized some stuff
* Added sounds
* Better experiance when shortcut is in use.
* UI tweaks
* Fixed KBM ui for unicode shortcut not having ","
* Some clean up
* Cleanup
* Cleanup
* Fixed applyXamlStyling
* Added back stuff lost in merge
* applyXamlStyling, again
* Fixed crash on change from non shortcut to shortcut
* Update src/modules/keyboardmanager/KeyboardManagerEngineTest/KeyboardManagerEngineTest.vcxproj
* Fixed some spelling type issues.
* ImplementationLibrary 231216
* Comment bump to see if the Microsoft.Windows.ImplementationLibrary version thing gets picked up
* Correct, Microsoft.Windows.ImplementationLibrary, finally?
* Fixed two test that failed because we now allow key-chords.
* Removed shortcut sounds.
* use original behavior when "allow chords" is off in shortcut window
* fix crash when editing a shortcut that has apps specified for it
* split KBM chords with comma on dashboard page
* Fix some spelling items.
* More "spelling"
* Fix XAML styling
* align TextBlock and ToggleSwitch
* fix cutoff issue at the top
* increase ComboBox width
* Added *Unsupported* for backwards compat on config of KBM
* fix spellcheck
* Fix crash on Remap key screen
* Fixed Remap Keys ComboBox width too short.
* Removed KBM Single Edit mode, fixed crash.
* Fix Xaml with xaml cops
* Fix crash on setting "target app" for some types of shortcuts.
* Space to toggle chord, combobox back
* fix spellcheck
* fix some code nits
* Code review updates.
* Add exclusions to the bug report tool
* Code review and kill CloseAndEndTask
* Fix alignment / 3 comboboxes per row
* Fix daily telemetry events to exclude start app and open URI
* Add chords and remove app start and open uri from config telemetry
* comma instead of plus in human readable shortcut telemetry data
* Code review, restore default-old state when new row added in KBM
* Code review, restore default-old state when new row added in KBM, part 2
* Still show target app on Settings
* Only allow enabling chords for origin shortcuts
---------
Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com>
Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
2024-02-27 18:12:05 -05:00
|
|
|
if (SUCCEEDED(co_init))
|
|
|
|
|
{
|
|
|
|
|
CoUninitialize();
|
|
|
|
|
}
|
2021-06-22 15:24:03 +03:00
|
|
|
|
2023-08-03 19:44:17 +02:00
|
|
|
return success;
|
2021-06-22 15:24:03 +03:00
|
|
|
}
|
|
|
|
|
|
2022-04-19 22:00:28 +02:00
|
|
|
struct ProcessInfo
|
|
|
|
|
{
|
|
|
|
|
wil::unique_process_handle processHandle;
|
|
|
|
|
DWORD processID = {};
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-25 17:39:52 +01:00
|
|
|
inline std::optional<ProcessInfo> RunNonElevatedFailsafe(const std::wstring& file, const std::wstring& params, const std::wstring& working_dir, DWORD handleAccess = 0)
|
2022-04-19 22:00:28 +02:00
|
|
|
{
|
|
|
|
|
bool launched = RunNonElevatedEx(file, params, working_dir);
|
|
|
|
|
if (!launched)
|
|
|
|
|
{
|
|
|
|
|
Logger::warn(L"RunNonElevatedEx() failed. Trying fallback");
|
|
|
|
|
std::wstring action_runner_path = get_module_folderpath() + L"\\PowerToys.ActionRunner.exe";
|
|
|
|
|
std::wstring newParams = fmt::format(L"-run-non-elevated -target \"{}\" {}", file, params);
|
|
|
|
|
launched = run_non_elevated(action_runner_path, newParams, nullptr, working_dir.c_str());
|
|
|
|
|
if (launched)
|
|
|
|
|
{
|
|
|
|
|
Logger::trace(L"Started {}", file);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger::warn(L"Failed to start {}", file);
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
[KBM]Launch apps / URI with keyboard shortcuts, support chords (#30121)
* Working UI update with just runProgram Path and isRunProgram
* First working, basic. no args or path, or setting change detections.
* Revert and fixed.
* Some clean up, working with config file monitor
* Args and Start-in should be working.
* File monitor, quotes, xaml screens one
* Fixed enable/disable toogle from XAML
* Code cleanup.
* Betting logging.
* Cleanup, start of RunProgramDescriptor and usage of run_non_elevated/run_elevated
* Code moved to KeyboardEventHandlers, but not enabled since it won't build as is, needs elevation.h. Other testing..
* Key chords working, pretty much
* Added gui for elevation level, need to refresh on change...
* f: include shellapi.h and reference wil in KBMEL
* run_elevated/run_non_elevated sorted out. Working!
* Removed lots of old temp code.
* Fix some speling errors.
* Cleanup before trying to add a UI for the chord
* Added "DifferentUser" option
* Closer on UI for chords.
* Better UI, lots working.
* Clean up
* Text for “Allow chords” – needs to look better…
* Bugs and clean-up
* Cleanup
* Refactor and clean up.
* More clean up
* Some localization.
* Don’t show “Allow chords“ to the “to” shortcut
* Maybe better foreground after opening new app
* Better chord matching.
* Runprogram fix for stealing existing shortcut.
* Better runProgram stuff
* Temp commit
* Working well
* Toast test
* More toast
* Added File and Folder picker UI
* Pre-check on run program file exists.
* Refactor to SetupRunProgramControls
* Open URI UI is going.
* Open URI working well
* Open URI stuff working well
* Allowed AppSpecific shortcut and fixed backup/restore shortcut dups
* Fixed settings screen
* Start of code to find by name...
* UI fixed
* Small fixes
* Some single edit code working.
* UI getting better.
* Fixes
* Fixed and merge from main
* UI updates
* UI updates.
* UI stuff
* Fixed crash from move ui item locations.
* Fixed crash from move ui item locations.
* Added delete confirm
* Basic sound working.
* Localized some stuff
* Added sounds
* Better experiance when shortcut is in use.
* UI tweaks
* Fixed KBM ui for unicode shortcut not having ","
* Some clean up
* Cleanup
* Cleanup
* Fixed applyXamlStyling
* Added back stuff lost in merge
* applyXamlStyling, again
* Fixed crash on change from non shortcut to shortcut
* Update src/modules/keyboardmanager/KeyboardManagerEngineTest/KeyboardManagerEngineTest.vcxproj
* Fixed some spelling type issues.
* ImplementationLibrary 231216
* Comment bump to see if the Microsoft.Windows.ImplementationLibrary version thing gets picked up
* Correct, Microsoft.Windows.ImplementationLibrary, finally?
* Fixed two test that failed because we now allow key-chords.
* Removed shortcut sounds.
* use original behavior when "allow chords" is off in shortcut window
* fix crash when editing a shortcut that has apps specified for it
* split KBM chords with comma on dashboard page
* Fix some spelling items.
* More "spelling"
* Fix XAML styling
* align TextBlock and ToggleSwitch
* fix cutoff issue at the top
* increase ComboBox width
* Added *Unsupported* for backwards compat on config of KBM
* fix spellcheck
* Fix crash on Remap key screen
* Fixed Remap Keys ComboBox width too short.
* Removed KBM Single Edit mode, fixed crash.
* Fix Xaml with xaml cops
* Fix crash on setting "target app" for some types of shortcuts.
* Space to toggle chord, combobox back
* fix spellcheck
* fix some code nits
* Code review updates.
* Add exclusions to the bug report tool
* Code review and kill CloseAndEndTask
* Fix alignment / 3 comboboxes per row
* Fix daily telemetry events to exclude start app and open URI
* Add chords and remove app start and open uri from config telemetry
* comma instead of plus in human readable shortcut telemetry data
* Code review, restore default-old state when new row added in KBM
* Code review, restore default-old state when new row added in KBM, part 2
* Still show target app on Settings
* Only allow enabling chords for origin shortcuts
---------
Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com>
Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
2024-02-27 18:12:05 -05:00
|
|
|
auto handles = getProcessHandlesByName(std::filesystem::path{ file }.filename().wstring(), PROCESS_QUERY_INFORMATION | SYNCHRONIZE | handleAccess);
|
2022-04-19 22:00:28 +02:00
|
|
|
|
|
|
|
|
if (handles.empty())
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
|
|
ProcessInfo result;
|
|
|
|
|
result.processID = GetProcessId(handles[0].get());
|
|
|
|
|
result.processHandle = std::move(handles[0]);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-15 15:16:09 +03:00
|
|
|
// Run command with the same elevation, returns true if succeeded
|
[KBM]Launch apps / URI with keyboard shortcuts, support chords (#30121)
* Working UI update with just runProgram Path and isRunProgram
* First working, basic. no args or path, or setting change detections.
* Revert and fixed.
* Some clean up, working with config file monitor
* Args and Start-in should be working.
* File monitor, quotes, xaml screens one
* Fixed enable/disable toogle from XAML
* Code cleanup.
* Betting logging.
* Cleanup, start of RunProgramDescriptor and usage of run_non_elevated/run_elevated
* Code moved to KeyboardEventHandlers, but not enabled since it won't build as is, needs elevation.h. Other testing..
* Key chords working, pretty much
* Added gui for elevation level, need to refresh on change...
* f: include shellapi.h and reference wil in KBMEL
* run_elevated/run_non_elevated sorted out. Working!
* Removed lots of old temp code.
* Fix some speling errors.
* Cleanup before trying to add a UI for the chord
* Added "DifferentUser" option
* Closer on UI for chords.
* Better UI, lots working.
* Clean up
* Text for “Allow chords” – needs to look better…
* Bugs and clean-up
* Cleanup
* Refactor and clean up.
* More clean up
* Some localization.
* Don’t show “Allow chords“ to the “to” shortcut
* Maybe better foreground after opening new app
* Better chord matching.
* Runprogram fix for stealing existing shortcut.
* Better runProgram stuff
* Temp commit
* Working well
* Toast test
* More toast
* Added File and Folder picker UI
* Pre-check on run program file exists.
* Refactor to SetupRunProgramControls
* Open URI UI is going.
* Open URI working well
* Open URI stuff working well
* Allowed AppSpecific shortcut and fixed backup/restore shortcut dups
* Fixed settings screen
* Start of code to find by name...
* UI fixed
* Small fixes
* Some single edit code working.
* UI getting better.
* Fixes
* Fixed and merge from main
* UI updates
* UI updates.
* UI stuff
* Fixed crash from move ui item locations.
* Fixed crash from move ui item locations.
* Added delete confirm
* Basic sound working.
* Localized some stuff
* Added sounds
* Better experiance when shortcut is in use.
* UI tweaks
* Fixed KBM ui for unicode shortcut not having ","
* Some clean up
* Cleanup
* Cleanup
* Fixed applyXamlStyling
* Added back stuff lost in merge
* applyXamlStyling, again
* Fixed crash on change from non shortcut to shortcut
* Update src/modules/keyboardmanager/KeyboardManagerEngineTest/KeyboardManagerEngineTest.vcxproj
* Fixed some spelling type issues.
* ImplementationLibrary 231216
* Comment bump to see if the Microsoft.Windows.ImplementationLibrary version thing gets picked up
* Correct, Microsoft.Windows.ImplementationLibrary, finally?
* Fixed two test that failed because we now allow key-chords.
* Removed shortcut sounds.
* use original behavior when "allow chords" is off in shortcut window
* fix crash when editing a shortcut that has apps specified for it
* split KBM chords with comma on dashboard page
* Fix some spelling items.
* More "spelling"
* Fix XAML styling
* align TextBlock and ToggleSwitch
* fix cutoff issue at the top
* increase ComboBox width
* Added *Unsupported* for backwards compat on config of KBM
* fix spellcheck
* Fix crash on Remap key screen
* Fixed Remap Keys ComboBox width too short.
* Removed KBM Single Edit mode, fixed crash.
* Fix Xaml with xaml cops
* Fix crash on setting "target app" for some types of shortcuts.
* Space to toggle chord, combobox back
* fix spellcheck
* fix some code nits
* Code review updates.
* Add exclusions to the bug report tool
* Code review and kill CloseAndEndTask
* Fix alignment / 3 comboboxes per row
* Fix daily telemetry events to exclude start app and open URI
* Add chords and remove app start and open uri from config telemetry
* comma instead of plus in human readable shortcut telemetry data
* Code review, restore default-old state when new row added in KBM
* Code review, restore default-old state when new row added in KBM, part 2
* Still show target app on Settings
* Only allow enabling chords for origin shortcuts
---------
Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com>
Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
2024-02-27 18:12:05 -05:00
|
|
|
inline bool run_same_elevation(const std::wstring& file, const std::wstring& params, DWORD* returnPid, const wchar_t* workingDir = nullptr)
|
2020-12-15 15:16:09 +03:00
|
|
|
{
|
|
|
|
|
auto executable_args = L"\"" + file + L"\"";
|
|
|
|
|
if (!params.empty())
|
|
|
|
|
{
|
|
|
|
|
executable_args += L" " + params;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-14 15:29:12 +01:00
|
|
|
STARTUPINFO si = { sizeof(STARTUPINFO) };
|
2020-12-15 15:16:09 +03:00
|
|
|
PROCESS_INFORMATION pi = { 0 };
|
2023-02-08 11:54:16 +00:00
|
|
|
|
2020-12-15 15:16:09 +03:00
|
|
|
auto succeeded = CreateProcessW(file.c_str(),
|
2023-02-08 11:54:16 +00:00
|
|
|
&executable_args[0],
|
2020-12-15 15:16:09 +03:00
|
|
|
nullptr,
|
|
|
|
|
nullptr,
|
|
|
|
|
FALSE,
|
|
|
|
|
0,
|
|
|
|
|
nullptr,
|
[KBM]Launch apps / URI with keyboard shortcuts, support chords (#30121)
* Working UI update with just runProgram Path and isRunProgram
* First working, basic. no args or path, or setting change detections.
* Revert and fixed.
* Some clean up, working with config file monitor
* Args and Start-in should be working.
* File monitor, quotes, xaml screens one
* Fixed enable/disable toogle from XAML
* Code cleanup.
* Betting logging.
* Cleanup, start of RunProgramDescriptor and usage of run_non_elevated/run_elevated
* Code moved to KeyboardEventHandlers, but not enabled since it won't build as is, needs elevation.h. Other testing..
* Key chords working, pretty much
* Added gui for elevation level, need to refresh on change...
* f: include shellapi.h and reference wil in KBMEL
* run_elevated/run_non_elevated sorted out. Working!
* Removed lots of old temp code.
* Fix some speling errors.
* Cleanup before trying to add a UI for the chord
* Added "DifferentUser" option
* Closer on UI for chords.
* Better UI, lots working.
* Clean up
* Text for “Allow chords” – needs to look better…
* Bugs and clean-up
* Cleanup
* Refactor and clean up.
* More clean up
* Some localization.
* Don’t show “Allow chords“ to the “to” shortcut
* Maybe better foreground after opening new app
* Better chord matching.
* Runprogram fix for stealing existing shortcut.
* Better runProgram stuff
* Temp commit
* Working well
* Toast test
* More toast
* Added File and Folder picker UI
* Pre-check on run program file exists.
* Refactor to SetupRunProgramControls
* Open URI UI is going.
* Open URI working well
* Open URI stuff working well
* Allowed AppSpecific shortcut and fixed backup/restore shortcut dups
* Fixed settings screen
* Start of code to find by name...
* UI fixed
* Small fixes
* Some single edit code working.
* UI getting better.
* Fixes
* Fixed and merge from main
* UI updates
* UI updates.
* UI stuff
* Fixed crash from move ui item locations.
* Fixed crash from move ui item locations.
* Added delete confirm
* Basic sound working.
* Localized some stuff
* Added sounds
* Better experiance when shortcut is in use.
* UI tweaks
* Fixed KBM ui for unicode shortcut not having ","
* Some clean up
* Cleanup
* Cleanup
* Fixed applyXamlStyling
* Added back stuff lost in merge
* applyXamlStyling, again
* Fixed crash on change from non shortcut to shortcut
* Update src/modules/keyboardmanager/KeyboardManagerEngineTest/KeyboardManagerEngineTest.vcxproj
* Fixed some spelling type issues.
* ImplementationLibrary 231216
* Comment bump to see if the Microsoft.Windows.ImplementationLibrary version thing gets picked up
* Correct, Microsoft.Windows.ImplementationLibrary, finally?
* Fixed two test that failed because we now allow key-chords.
* Removed shortcut sounds.
* use original behavior when "allow chords" is off in shortcut window
* fix crash when editing a shortcut that has apps specified for it
* split KBM chords with comma on dashboard page
* Fix some spelling items.
* More "spelling"
* Fix XAML styling
* align TextBlock and ToggleSwitch
* fix cutoff issue at the top
* increase ComboBox width
* Added *Unsupported* for backwards compat on config of KBM
* fix spellcheck
* Fix crash on Remap key screen
* Fixed Remap Keys ComboBox width too short.
* Removed KBM Single Edit mode, fixed crash.
* Fix Xaml with xaml cops
* Fix crash on setting "target app" for some types of shortcuts.
* Space to toggle chord, combobox back
* fix spellcheck
* fix some code nits
* Code review updates.
* Add exclusions to the bug report tool
* Code review and kill CloseAndEndTask
* Fix alignment / 3 comboboxes per row
* Fix daily telemetry events to exclude start app and open URI
* Add chords and remove app start and open uri from config telemetry
* comma instead of plus in human readable shortcut telemetry data
* Code review, restore default-old state when new row added in KBM
* Code review, restore default-old state when new row added in KBM, part 2
* Still show target app on Settings
* Only allow enabling chords for origin shortcuts
---------
Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com>
Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
2024-02-27 18:12:05 -05:00
|
|
|
workingDir,
|
2020-12-15 15:16:09 +03:00
|
|
|
&si,
|
|
|
|
|
&pi);
|
|
|
|
|
|
|
|
|
|
if (succeeded)
|
|
|
|
|
{
|
|
|
|
|
if (pi.hProcess)
|
|
|
|
|
{
|
|
|
|
|
if (returnPid)
|
|
|
|
|
{
|
|
|
|
|
*returnPid = GetProcessId(pi.hProcess);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CloseHandle(pi.hProcess);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pi.hThread)
|
|
|
|
|
{
|
|
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return succeeded;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns true if the current process is running from administrator account
|
|
|
|
|
// The function returns true in case of error since we want to return false
|
|
|
|
|
// only in case of a positive verification that the user is not an admin.
|
|
|
|
|
inline bool check_user_is_admin()
|
|
|
|
|
{
|
|
|
|
|
auto freeMemory = [](PSID pSID, PTOKEN_GROUPS pGroupInfo) {
|
|
|
|
|
if (pSID)
|
|
|
|
|
{
|
|
|
|
|
FreeSid(pSID);
|
|
|
|
|
}
|
|
|
|
|
if (pGroupInfo)
|
|
|
|
|
{
|
|
|
|
|
GlobalFree(pGroupInfo);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
HANDLE hToken;
|
|
|
|
|
DWORD dwSize = 0;
|
|
|
|
|
PTOKEN_GROUPS pGroupInfo;
|
|
|
|
|
SID_IDENTIFIER_AUTHORITY SIDAuth = SECURITY_NT_AUTHORITY;
|
|
|
|
|
PSID pSID = NULL;
|
|
|
|
|
|
|
|
|
|
// Open a handle to the access token for the calling process.
|
|
|
|
|
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Call GetTokenInformation to get the buffer size.
|
|
|
|
|
if (!GetTokenInformation(hToken, TokenGroups, NULL, dwSize, &dwSize))
|
|
|
|
|
{
|
|
|
|
|
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Allocate the buffer.
|
2023-02-08 11:00:19 +00:00
|
|
|
pGroupInfo = static_cast<PTOKEN_GROUPS>(GlobalAlloc(GPTR, dwSize));
|
2020-12-15 15:16:09 +03:00
|
|
|
|
|
|
|
|
// Call GetTokenInformation again to get the group information.
|
|
|
|
|
if (!GetTokenInformation(hToken, TokenGroups, pGroupInfo, dwSize, &dwSize))
|
|
|
|
|
{
|
|
|
|
|
freeMemory(pSID, pGroupInfo);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a SID for the BUILTIN\Administrators group.
|
|
|
|
|
if (!AllocateAndInitializeSid(&SIDAuth, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &pSID))
|
|
|
|
|
{
|
|
|
|
|
freeMemory(pSID, pGroupInfo);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Loop through the group SIDs looking for the administrator SID.
|
|
|
|
|
for (DWORD i = 0; i < pGroupInfo->GroupCount; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (EqualSid(pSID, pGroupInfo->Groups[i].Sid))
|
|
|
|
|
{
|
|
|
|
|
freeMemory(pSID, pGroupInfo);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
freeMemory(pSID, pGroupInfo);
|
|
|
|
|
return false;
|
2023-08-03 19:44:17 +02:00
|
|
|
}
|
2023-12-28 13:33:04 +03:00
|
|
|
|
|
|
|
|
inline bool IsProcessOfWindowElevated(HWND window)
|
|
|
|
|
{
|
|
|
|
|
DWORD pid = 0;
|
|
|
|
|
GetWindowThreadProcessId(window, &pid);
|
|
|
|
|
if (!pid)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wil::unique_handle hProcess{ OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION,
|
|
|
|
|
FALSE,
|
|
|
|
|
pid) };
|
|
|
|
|
|
|
|
|
|
wil::unique_handle token;
|
|
|
|
|
|
|
|
|
|
if (OpenProcessToken(hProcess.get(), TOKEN_QUERY, &token))
|
|
|
|
|
{
|
|
|
|
|
TOKEN_ELEVATION elevation;
|
|
|
|
|
DWORD size;
|
|
|
|
|
if (GetTokenInformation(token.get(), TokenElevation, &elevation, sizeof(elevation), &size))
|
|
|
|
|
{
|
|
|
|
|
return elevation.TokenIsElevated != 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|