From 969abe015c37b76321b7a21344a835d52c89e621 Mon Sep 17 00:00:00 2001 From: Alekhya Kommuru Date: Tue, 14 Jan 2020 15:00:05 -0800 Subject: [PATCH 01/15] added the helper functions --- src/common/common.cpp | 19 +++++++++++++++++++ src/common/common.h | 14 +++++++++++++- src/modules/powerrename/dll/dllmain.cpp | 12 ++++++++++-- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/common/common.cpp b/src/common/common.cpp index 73b3cef189..1a5677366f 100644 --- a/src/common/common.cpp +++ b/src/common/common.cpp @@ -385,6 +385,25 @@ std::wstring get_resource_string(UINT resource_id, HINSTANCE instance, const wch } } +// function to return the string as a to a wchar_t* (to enable localization in cases where the return type did not accept wstring) +wchar_t* get_resource_string_wchar(UINT resource_id, HINSTANCE instance) +{ + wchar_t* text_ptr; + unsigned int length = LoadStringW(instance, resource_id, reinterpret_cast(&text_ptr), 0); + std::wstring res_string = { *reinterpret_cast(&text_ptr), length }; + length++; + + if (length > 1) + { + wchar_t* tmp_res_ptr; + tmp_res_ptr = new wchar_t[length]; + wmemcpy(tmp_res_ptr, res_string.c_str(), length); + return tmp_res_ptr; + } + + return (wchar_t*)L"test"; +} + std::wstring get_module_filename(HMODULE mod) { wchar_t buffer[MAX_PATH + 1]; diff --git a/src/common/common.h b/src/common/common.h index 81525f9361..5aaa215c50 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -77,4 +77,16 @@ std::wstring get_resource_string(UINT resource_id, HINSTANCE instance, const wch // Requires that // extern "C" IMAGE_DOS_HEADER __ImageBase; // is added to the .cpp file. -#define GET_RESOURCE_STRING(resource_id) get_resource_string(resource_id, reinterpret_cast(&__ImageBase), L#resource_id) \ No newline at end of file +#define GET_RESOURCE_STRING(resource_id) get_resource_string(resource_id, reinterpret_cast(&__ImageBase), L#resource_id) + +// Function which takes a pointer ptr and allocates space before populating it with the string from the resource table +// using this approach as wstring cannot be cast to PCWSTR without allocating space +// hence, we have to malloc and free the pointer after each use +wchar_t* get_resource_string_wchar(UINT resource_id, HINSTANCE instance); + +// Wrapper for getting a string from the resource file. +// Requires that +// extern "C" IMAGE_DOS_HEADER __ImageBase; +// is added to the .cpp file. +// used when the return type must be a wchar_t* instead of a wstring. +#define GET_RES_STRING_WCHAR(resource_id) get_resource_string_wchar(resource_id, reinterpret_cast(&__ImageBase)) diff --git a/src/modules/powerrename/dll/dllmain.cpp b/src/modules/powerrename/dll/dllmain.cpp index 4d1f82a298..71ba578286 100644 --- a/src/modules/powerrename/dll/dllmain.cpp +++ b/src/modules/powerrename/dll/dllmain.cpp @@ -4,7 +4,8 @@ #include #include #include - +#include +#include "resource.h" #include std::atomic g_dwModuleRefCount = 0; @@ -160,12 +161,14 @@ class PowerRenameModule : public PowertoyModuleIface private: // Enabled by default bool m_enabled = true; + wchar_t* app_name; public: // Return the display name of the powertoy, this will be cached virtual PCWSTR get_name() override { - return L"PowerRename"; + app_name = GET_RES_STRING_WCHAR(IDS_POWERRENAME); + return app_name; } // Enable the powertoy @@ -299,6 +302,11 @@ public: { init_settings(); } + + ~PowerRenameModule() + { + delete app_name; + } }; extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create() From 0b1232b65d9b345bebd10df7e3699497ecf8ea79 Mon Sep 17 00:00:00 2001 From: Alekhya Kommuru Date: Tue, 14 Jan 2020 15:17:08 -0800 Subject: [PATCH 02/15] localized dllmain powerrename --- src/modules/powerrename/dll/PowerRenameExt.rc | 15 +++++++- src/modules/powerrename/dll/dllmain.cpp | 36 +++++++++---------- src/modules/powerrename/dll/resource.h | 17 +++++++-- 3 files changed, 47 insertions(+), 21 deletions(-) diff --git a/src/modules/powerrename/dll/PowerRenameExt.rc b/src/modules/powerrename/dll/PowerRenameExt.rc index 1f6fd72364..e85b27e058 100644 --- a/src/modules/powerrename/dll/PowerRenameExt.rc +++ b/src/modules/powerrename/dll/PowerRenameExt.rc @@ -91,7 +91,20 @@ STRINGTABLE BEGIN - IDS_POWERRENAME "PowerRename" + IDS_POWERRENAME "PowerRename" + IDS_SETTINGS_DESCRIPTION L"A Windows Shell Extension for more advanced bulk renaming using search and replace or regular expressions." + IDS_SETTINGS_ICON L"pt-power-rename" + IDS_OVERVIEW_LINK L"https://github.com/microsoft/PowerToys/tree/master/src/modules/powerrename" + IDS_BOOL_PERSIST L"bool_persist_input" + IDS_RESTORE_SEARCH L"Restore search, replace and flags values on launch from previous run." + IDS_MRU_ENABLED L"bool_mru_enabled" + IDS_ENABLE_AUTO L"Enable autocomplete and autosuggest of recently used inputs for search and replace values." + IDS_MAX_MRU_SIZE L"int_max_mru_size" + IDS_MAX_ITEMS L"Maximum number of items to show in recently used list for autocomplete dropdown." + IDS_SHOW_ICON L"bool_show_icon_on_menu" + IDS_ICON_CONTEXT_MENU L"Show icon on context menu." + IDS_EXTENDED_MENU L"bool_show_extended_menu" L"Only show the PowerRename menu item on the extended context menu (SHIFT + Right-click)." + IDS_EXTENDED_MENU_INFO L"Only show the PowerRename menu item on the extended context menu (SHIFT + Right-click)." END #endif // English (United States) resources diff --git a/src/modules/powerrename/dll/dllmain.cpp b/src/modules/powerrename/dll/dllmain.cpp index 71ba578286..562751c8c6 100644 --- a/src/modules/powerrename/dll/dllmain.cpp +++ b/src/modules/powerrename/dll/dllmain.cpp @@ -206,38 +206,38 @@ public: // Create a Settings object. PowerToysSettings::Settings settings(hinstance, get_name()); - settings.set_description(L"A Windows Shell Extension for more advanced bulk renaming using search and replace or regular expressions."); - settings.set_icon_key(L"pt-power-rename"); + settings.set_description(GET_RESOURCE_STRING(IDS_SETTINGS_DESCRIPTION)); + settings.set_icon_key(GET_RESOURCE_STRING(IDS_SETTINGS_ICON)); // Link to the GitHub PowerRename sub-page - settings.set_overview_link(L"https://github.com/microsoft/PowerToys/tree/master/src/modules/powerrename"); + settings.set_overview_link(GET_RESOURCE_STRING(IDS_OVERVIEW_LINK)); settings.add_bool_toogle( - L"bool_persist_input", - L"Restore search, replace and flags values on launch from previous run.", + GET_RESOURCE_STRING(IDS_BOOL_PERSIST), + GET_RESOURCE_STRING(IDS_RESTORE_SEARCH), CSettings::GetPersistState()); settings.add_bool_toogle( - L"bool_mru_enabled", - L"Enable autocomplete and autosuggest of recently used inputs for search and replace values.", + GET_RESOURCE_STRING(IDS_MRU_ENABLED), + GET_RESOURCE_STRING(IDS_ENABLE_AUTO), CSettings::GetMRUEnabled()); settings.add_int_spinner( - L"int_max_mru_size", - L"Maximum number of items to show in recently used list for autocomplete dropdown.", + GET_RESOURCE_STRING(IDS_MAX_MRU_SIZE), + GET_RESOURCE_STRING(IDS_MAX_ITEMS), CSettings::GetMaxMRUSize(), 0, 20, 1); settings.add_bool_toogle( - L"bool_show_icon_on_menu", - L"Show icon on context menu.", + GET_RESOURCE_STRING(IDS_SHOW_ICON), + GET_RESOURCE_STRING(IDS_ICON_CONTEXT_MENU), CSettings::GetShowIconOnMenu()); settings.add_bool_toogle( - L"bool_show_extended_menu", - L"Only show the PowerRename menu item on the extended context menu (SHIFT + Right-click).", + GET_RESOURCE_STRING(IDS_EXTENDED_MENU), + GET_RESOURCE_STRING(IDS_EXTENDED_MENU_INFO), CSettings::GetExtendedContextMenuOnly()); return settings.serialize_to_buffer(buffer, buffer_size); @@ -253,11 +253,11 @@ public: PowerToysSettings::PowerToyValues values = PowerToysSettings::PowerToyValues::from_json_string(config); - CSettings::SetPersistState(values.get_bool_value(L"bool_persist_input").value()); - CSettings::SetMRUEnabled(values.get_bool_value(L"bool_mru_enabled").value()); - CSettings::SetMaxMRUSize(values.get_int_value(L"int_max_mru_size").value()); - CSettings::SetShowIconOnMenu(values.get_bool_value(L"bool_show_icon_on_menu").value()); - CSettings::SetExtendedContextMenuOnly(values.get_bool_value(L"bool_show_extended_menu").value()); + CSettings::SetPersistState(values.get_bool_value(GET_RESOURCE_STRING(IDS_BOOL_PERSIST)).value()); + CSettings::SetMRUEnabled(values.get_bool_value(GET_RESOURCE_STRING(IDS_MRU_ENABLED)).value()); + CSettings::SetMaxMRUSize(values.get_int_value(GET_RESOURCE_STRING(IDS_MAX_MRU_SIZE)).value()); + CSettings::SetShowIconOnMenu(values.get_bool_value(GET_RESOURCE_STRING(IDS_SHOW_ICON)).value()); + CSettings::SetExtendedContextMenuOnly(values.get_bool_value(GET_RESOURCE_STRING(IDS_EXTENDED_MENU)).value()); } catch (std::exception) { diff --git a/src/modules/powerrename/dll/resource.h b/src/modules/powerrename/dll/resource.h index 8bc71ee4e1..2e48734955 100644 --- a/src/modules/powerrename/dll/resource.h +++ b/src/modules/powerrename/dll/resource.h @@ -1,5 +1,18 @@ -#define IDS_POWERRENAME 801 -#define IDI_RENAME 132 +#define IDS_POWERRENAME 99 +#define IDI_RENAME 100 +#define IDS_SETTINGS_DESCRIPTION 101 +#define IDS_SETTINGS_ICON 102 +#define IDS_OVERVIEW_LINK 103 +#define IDS_BOOL_PERSIST 104 +#define IDS_RESTORE_SEARCH 105 +#define IDS_MRU_ENABLED 106 +#define IDS_ENABLE_AUTO 107 +#define IDS_MAX_MRU_SIZE 108 +#define IDS_MAX_ITEMS 109 +#define IDS_SHOW_ICON 110 +#define IDS_ICON_CONTEXT_MENU 111 +#define IDS_EXTENDED_MENU 112 +#define IDS_EXTENDED_MENU_INFO 113 // Next default values for new objects // From 8f2b2aba1250e3a68f4b2c919d2068f8e83b9ac1 Mon Sep 17 00:00:00 2001 From: Alekhya Kommuru Date: Tue, 14 Jan 2020 15:23:21 -0800 Subject: [PATCH 03/15] localized powerRenameExt --- src/modules/powerrename/dll/PowerRenameExt.cpp | 4 +++- src/modules/powerrename/dll/PowerRenameExt.h | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/modules/powerrename/dll/PowerRenameExt.cpp b/src/modules/powerrename/dll/PowerRenameExt.cpp index a2d98c19db..6f056c7338 100644 --- a/src/modules/powerrename/dll/PowerRenameExt.cpp +++ b/src/modules/powerrename/dll/PowerRenameExt.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include "resource.h" @@ -195,7 +196,8 @@ DWORD WINAPI CPowerRenameMenu::s_PowerRenameUIThreadProc(_In_ void* pData) HRESULT __stdcall CPowerRenameMenu::GetTitle(IShellItemArray* /*psiItemArray*/, LPWSTR* ppszName) { - return SHStrDup(L"PowerRename", ppszName); + app_name = GET_RES_STRING_WCHAR(IDS_POWERRENAME); + return SHStrDup(app_name, ppszName); } HRESULT __stdcall CPowerRenameMenu::GetIcon(IShellItemArray* /*psiItemArray*/, LPWSTR* ppszIcon) diff --git a/src/modules/powerrename/dll/PowerRenameExt.h b/src/modules/powerrename/dll/PowerRenameExt.h index 8e6258b1a3..7e45f38906 100644 --- a/src/modules/powerrename/dll/PowerRenameExt.h +++ b/src/modules/powerrename/dll/PowerRenameExt.h @@ -66,9 +66,12 @@ public: static bool IsEnabled(); private: - ~CPowerRenameMenu(); + ~CPowerRenameMenu(){ + delete (app_name); + } std::atomic m_refCount = 1; HBITMAP m_hbmpIcon = nullptr; CComPtr m_spdo; + wchar_t* app_name; }; From 1e3486af94ea78ad0c2c5c6821d15c886740b171 Mon Sep 17 00:00:00 2001 From: Alekhya Kommuru Date: Tue, 14 Jan 2020 16:39:03 -0800 Subject: [PATCH 04/15] localized the settings file --- src/modules/powerrename/lib/PowerRenameLib.rc | 73 +++++++++++++++++++ src/modules/powerrename/lib/Settings.cpp | 26 ++++--- src/modules/powerrename/lib/resource.h | 28 +++++++ 3 files changed, 115 insertions(+), 12 deletions(-) create mode 100644 src/modules/powerrename/lib/PowerRenameLib.rc create mode 100644 src/modules/powerrename/lib/resource.h diff --git a/src/modules/powerrename/lib/PowerRenameLib.rc b/src/modules/powerrename/lib/PowerRenameLib.rc new file mode 100644 index 0000000000..3779e40581 --- /dev/null +++ b/src/modules/powerrename/lib/PowerRenameLib.rc @@ -0,0 +1,73 @@ +// Microsoft Visual C++ generated resource script. +// +#include "resource.h" + +#define APSTUDIO_READONLY_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 2 resource. +// +#include "winres.h" + +///////////////////////////////////////////////////////////////////////////// +#undef APSTUDIO_READONLY_SYMBOLS + +///////////////////////////////////////////////////////////////////////////// +// English (United States) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +LANGUAGE 9, 1 + +#ifdef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// TEXTINCLUDE +// + +1 TEXTINCLUDE +BEGIN + "resource.h\0" +END + +2 TEXTINCLUDE +BEGIN + "#include ""winres.h""\r\n" + "\0" +END + +3 TEXTINCLUDE +BEGIN + "\r\n" + "\0" +END + +#endif // APSTUDIO_INVOKED + +#endif // English (United States) resources +///////////////////////////////////////////////////////////////////////////// + +STRINGTABLE +BEGIN + IDS_ROOT_PATH L"Software\\Microsoft\\PowerRename" + IDS_SEARCH_PATH L"SearchMRU" + IDS_REPLACE_PATH L"ReplaceMRU" + IDS_ENABLED L"Enabled" + IDS_SHOW_ICON L"ShowIcon" + IDS_CONTEXT_MENU L"ExtendedContextMenuOnly" + IDS_PERSIST_STATE L"PersistState" + IDS_MAX_MRU_SIZE L"MaxMRUSize" + IDS_FLAGS L"Flags" + IDS_SEARCH_TEXT L"SearchText" + IDS_REPLACE_TEXT L"ReplaceText" + IDS_MRU_ENABLED L"MRUEnabled" +END + +#ifndef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 3 resource. +// + + +///////////////////////////////////////////////////////////////////////////// +#endif // not APSTUDIO_INVOKED diff --git a/src/modules/powerrename/lib/Settings.cpp b/src/modules/powerrename/lib/Settings.cpp index 7921e7f901..32b687c8f4 100644 --- a/src/modules/powerrename/lib/Settings.cpp +++ b/src/modules/powerrename/lib/Settings.cpp @@ -3,20 +3,22 @@ #include #include "Settings.h" #include "PowerRenameInterfaces.h" +#include "resource.h" +#include -const wchar_t c_rootRegPath[] = L"Software\\Microsoft\\PowerRename"; -const wchar_t c_mruSearchRegPath[] = L"SearchMRU"; -const wchar_t c_mruReplaceRegPath[] = L"ReplaceMRU"; +wchar_t* c_rootRegPath = GET_RES_STRING_WCHAR(IDS_ROOT_PATH); +wchar_t* c_mruSearchRegPath = GET_RES_STRING_WCHAR(IDS_SEARCH_PATH); +wchar_t* c_mruReplaceRegPath = GET_RES_STRING_WCHAR(IDS_REPLACE_PATH); -const wchar_t c_enabled[] = L"Enabled"; -const wchar_t c_showIconOnMenu[] = L"ShowIcon"; -const wchar_t c_extendedContextMenuOnly[] = L"ExtendedContextMenuOnly"; -const wchar_t c_persistState[] = L"PersistState"; -const wchar_t c_maxMRUSize[] = L"MaxMRUSize"; -const wchar_t c_flags[] = L"Flags"; -const wchar_t c_searchText[] = L"SearchText"; -const wchar_t c_replaceText[] = L"ReplaceText"; -const wchar_t c_mruEnabled[] = L"MRUEnabled"; +wchar_t* c_enabled = GET_RES_STRING_WCHAR(IDS_ENABLED); +wchar_t* c_showIconOnMenu = GET_RES_STRING_WCHAR(IDS_SHOW_ICON); +wchar_t* c_extendedContextMenuOnly = GET_RES_STRING_WCHAR(IDS_CONTEXT_MENU); +wchar_t* c_persistState = GET_RES_STRING_WCHAR(IDS_PERSIST_STATE); +wchar_t* c_maxMRUSize = GET_RES_STRING_WCHAR(IDS_MAX_MRU_SIZE); +wchar_t* c_flags = GET_RES_STRING_WCHAR(IDS_FLAGS); +wchar_t* c_searchText = GET_RES_STRING_WCHAR(IDS_SEARCH_TEXT); +wchar_t* c_replaceText = GET_RES_STRING_WCHAR(IDS_REPLACE_TEXT); +wchar_t* c_mruEnabled = GET_RES_STRING_WCHAR(IDS_MRU_ENABLED); const bool c_enabledDefault = true; const bool c_showIconOnMenuDefault = true; diff --git a/src/modules/powerrename/lib/resource.h b/src/modules/powerrename/lib/resource.h new file mode 100644 index 0000000000..ff2a4a1a64 --- /dev/null +++ b/src/modules/powerrename/lib/resource.h @@ -0,0 +1,28 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Visual C++ generated include file. +// Used by PowerRenameLib.rc + +#define IDS_ROOT_PATH 100 +#define IDS_SEARCH_PATH 101 +#define IDS_REPLACE_PATH 102 +#define IDS_ENABLED 103 +#define IDS_SHOW_ICON 104 +#define IDS_CONTEXT_MENU 105 +#define IDS_PERSIST_STATE 106 +#define IDS_MAX_MRU_SIZE 107 +#define IDS_FLAGS 108 +#define IDS_SEARCH_TEXT 109 +#define IDS_REPLACE_TEXT 110 +#define IDS_MRU_ENABLED 111 + + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 101 +#define _APS_NEXT_COMMAND_VALUE 40001 +#define _APS_NEXT_CONTROL_VALUE 1001 +#define _APS_NEXT_SYMED_VALUE 101 +#endif +#endif From d0648d17540b12088f00cb24e5f07322c264b930 Mon Sep 17 00:00:00 2001 From: Alekhya Kommuru Date: Tue, 14 Jan 2020 16:40:40 -0800 Subject: [PATCH 05/15] built the proj --- src/modules/powerrename/lib/PowerRenameLib.vcxproj | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/modules/powerrename/lib/PowerRenameLib.vcxproj b/src/modules/powerrename/lib/PowerRenameLib.vcxproj index 52d9243d62..8acd064065 100644 --- a/src/modules/powerrename/lib/PowerRenameLib.vcxproj +++ b/src/modules/powerrename/lib/PowerRenameLib.vcxproj @@ -147,6 +147,7 @@ + @@ -167,6 +168,9 @@ + + + From 31a01ab22722323ac65bd7087f113b07557fda24 Mon Sep 17 00:00:00 2001 From: Alekhya Kommuru Date: Wed, 15 Jan 2020 13:57:05 -0800 Subject: [PATCH 06/15] Modified resourceIDs for strings in the table --- .../powerrename/dll/PowerRenameExt.cpp | 4 +- src/modules/powerrename/dll/PowerRenameExt.h | 4 +- src/modules/powerrename/dll/dllmain.cpp | 2 +- src/modules/powerrename/dll/resource.h | 42 ++++++++++--------- src/modules/powerrename/lib/Settings.cpp | 26 ++++++------ .../powerrename/testapp/PowerRenameTest.cpp | 1 + 6 files changed, 39 insertions(+), 40 deletions(-) diff --git a/src/modules/powerrename/dll/PowerRenameExt.cpp b/src/modules/powerrename/dll/PowerRenameExt.cpp index 6f056c7338..8870adc17e 100644 --- a/src/modules/powerrename/dll/PowerRenameExt.cpp +++ b/src/modules/powerrename/dll/PowerRenameExt.cpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include #include "resource.h" @@ -270,4 +270,4 @@ HRESULT __stdcall CPowerRenameMenu::EnumSubCommands(IEnumExplorerCommand** ppEnu { *ppEnum = nullptr; return E_NOTIMPL; -} +} \ No newline at end of file diff --git a/src/modules/powerrename/dll/PowerRenameExt.h b/src/modules/powerrename/dll/PowerRenameExt.h index 7e45f38906..0b1f0c75f1 100644 --- a/src/modules/powerrename/dll/PowerRenameExt.h +++ b/src/modules/powerrename/dll/PowerRenameExt.h @@ -66,9 +66,7 @@ public: static bool IsEnabled(); private: - ~CPowerRenameMenu(){ - delete (app_name); - } + ~CPowerRenameMenu(); std::atomic m_refCount = 1; HBITMAP m_hbmpIcon = nullptr; diff --git a/src/modules/powerrename/dll/dllmain.cpp b/src/modules/powerrename/dll/dllmain.cpp index 562751c8c6..adc3970bc6 100644 --- a/src/modules/powerrename/dll/dllmain.cpp +++ b/src/modules/powerrename/dll/dllmain.cpp @@ -312,4 +312,4 @@ public: extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create() { return new PowerRenameModule(); -} +} \ No newline at end of file diff --git a/src/modules/powerrename/dll/resource.h b/src/modules/powerrename/dll/resource.h index 2e48734955..6d2c78ffb3 100644 --- a/src/modules/powerrename/dll/resource.h +++ b/src/modules/powerrename/dll/resource.h @@ -1,26 +1,28 @@ -#define IDS_POWERRENAME 99 -#define IDI_RENAME 100 -#define IDS_SETTINGS_DESCRIPTION 101 -#define IDS_SETTINGS_ICON 102 -#define IDS_OVERVIEW_LINK 103 -#define IDS_BOOL_PERSIST 104 -#define IDS_RESTORE_SEARCH 105 -#define IDS_MRU_ENABLED 106 -#define IDS_ENABLE_AUTO 107 -#define IDS_MAX_MRU_SIZE 108 -#define IDS_MAX_ITEMS 109 -#define IDS_SHOW_ICON 110 -#define IDS_ICON_CONTEXT_MENU 111 -#define IDS_EXTENDED_MENU 112 -#define IDS_EXTENDED_MENU_INFO 113 +#define IDS_POWERRENAME 801 +#define IDI_RENAME 132 +#define IDS_SETTINGS_DESCRIPTION 2101 +#define IDS_SETTINGS_ICON 2102 +#define IDS_OVERVIEW_LINK 2103 +#define IDS_BOOL_PERSIST 2104 +#define IDS_RESTORE_SEARCH 2105 +#define IDS_MRU_ENABLED 2106 +#define IDS_ENABLE_AUTO 2107 +#define IDS_MAX_MRU_SIZE 2108 +#define IDS_MAX_ITEMS 2109 +#define IDS_SHOW_ICON 2110 +#define IDS_ICON_CONTEXT_MENU 2111 +#define IDS_EXTENDED_MENU 2112 +#define IDS_EXTENDED_MENU_INFO 2113 // Next default values for new objects -// + +// + #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS -#define _APS_NEXT_RESOURCE_VALUE 102 -#define _APS_NEXT_COMMAND_VALUE 40001 -#define _APS_NEXT_CONTROL_VALUE 1001 -#define _APS_NEXT_SYMED_VALUE 101 +#define _APS_NEXT_RESOURCE_VALUE 102 +#define _APS_NEXT_COMMAND_VALUE 40001 +#define _APS_NEXT_CONTROL_VALUE 1001 +#define _APS_NEXT_SYMED_VALUE 101 #endif #endif diff --git a/src/modules/powerrename/lib/Settings.cpp b/src/modules/powerrename/lib/Settings.cpp index 32b687c8f4..531e5c5ebc 100644 --- a/src/modules/powerrename/lib/Settings.cpp +++ b/src/modules/powerrename/lib/Settings.cpp @@ -4,21 +4,19 @@ #include "Settings.h" #include "PowerRenameInterfaces.h" #include "resource.h" -#include -wchar_t* c_rootRegPath = GET_RES_STRING_WCHAR(IDS_ROOT_PATH); -wchar_t* c_mruSearchRegPath = GET_RES_STRING_WCHAR(IDS_SEARCH_PATH); -wchar_t* c_mruReplaceRegPath = GET_RES_STRING_WCHAR(IDS_REPLACE_PATH); - -wchar_t* c_enabled = GET_RES_STRING_WCHAR(IDS_ENABLED); -wchar_t* c_showIconOnMenu = GET_RES_STRING_WCHAR(IDS_SHOW_ICON); -wchar_t* c_extendedContextMenuOnly = GET_RES_STRING_WCHAR(IDS_CONTEXT_MENU); -wchar_t* c_persistState = GET_RES_STRING_WCHAR(IDS_PERSIST_STATE); -wchar_t* c_maxMRUSize = GET_RES_STRING_WCHAR(IDS_MAX_MRU_SIZE); -wchar_t* c_flags = GET_RES_STRING_WCHAR(IDS_FLAGS); -wchar_t* c_searchText = GET_RES_STRING_WCHAR(IDS_SEARCH_TEXT); -wchar_t* c_replaceText = GET_RES_STRING_WCHAR(IDS_REPLACE_TEXT); -wchar_t* c_mruEnabled = GET_RES_STRING_WCHAR(IDS_MRU_ENABLED); +const wchar_t c_rootRegPath[] = L"Software\\Microsoft\\PowerRename"; +const wchar_t c_mruSearchRegPath[] = L"SearchMRU"; +const wchar_t c_mruReplaceRegPath[] = L"ReplaceMRU"; +const wchar_t c_enabled[] = L"Enabled"; +const wchar_t c_showIconOnMenu[] = L"ShowIcon"; +const wchar_t c_extendedContextMenuOnly[] = L"ExtendedContextMenuOnly"; +const wchar_t c_persistState[] = L"PersistState"; +const wchar_t c_maxMRUSize[] = L"MaxMRUSize"; +const wchar_t c_flags[] = L"Flags"; +const wchar_t c_searchText[] = L"SearchText"; +const wchar_t c_replaceText[] = L"ReplaceText"; +const wchar_t c_mruEnabled[] = L"MRUEnabled"; const bool c_enabledDefault = true; const bool c_showIconOnMenuDefault = true; diff --git a/src/modules/powerrename/testapp/PowerRenameTest.cpp b/src/modules/powerrename/testapp/PowerRenameTest.cpp index 370795df50..529eb0c2f4 100644 --- a/src/modules/powerrename/testapp/PowerRenameTest.cpp +++ b/src/modules/powerrename/testapp/PowerRenameTest.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") From 12c4dbf0e5d2f9f49fd6022dad6d02e1b727612f Mon Sep 17 00:00:00 2001 From: Alekhya Kommuru Date: Wed, 15 Jan 2020 16:44:18 -0800 Subject: [PATCH 07/15] added common as a reference project --- src/modules/powerrename/lib/PowerRenameLib.rc | 24 +++++++++--------- src/modules/powerrename/lib/Settings.cpp | 25 ++++++++++--------- .../testapp/PowerRenameTest.vcxproj | 5 ++++ 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/modules/powerrename/lib/PowerRenameLib.rc b/src/modules/powerrename/lib/PowerRenameLib.rc index 3779e40581..6597877d64 100644 --- a/src/modules/powerrename/lib/PowerRenameLib.rc +++ b/src/modules/powerrename/lib/PowerRenameLib.rc @@ -48,18 +48,18 @@ END STRINGTABLE BEGIN - IDS_ROOT_PATH L"Software\\Microsoft\\PowerRename" - IDS_SEARCH_PATH L"SearchMRU" - IDS_REPLACE_PATH L"ReplaceMRU" - IDS_ENABLED L"Enabled" - IDS_SHOW_ICON L"ShowIcon" - IDS_CONTEXT_MENU L"ExtendedContextMenuOnly" - IDS_PERSIST_STATE L"PersistState" - IDS_MAX_MRU_SIZE L"MaxMRUSize" - IDS_FLAGS L"Flags" - IDS_SEARCH_TEXT L"SearchText" - IDS_REPLACE_TEXT L"ReplaceText" - IDS_MRU_ENABLED L"MRUEnabled" + IDS_ROOT_PATH L"Software\\Microsoft\\PowerRename" + IDS_SEARCH_PATH L"SearchMRU" + IDS_REPLACE_PATH L"ReplaceMRU" + IDS_ENABLED L"Enabled" + IDS_SHOW_ICON L"ShowIcon" + IDS_CONTEXT_MENU L"ExtendedContextMenuOnly" + IDS_PERSIST_STATE L"PersistState" + IDS_MAX_MRU_SIZE L"MaxMRUSize" + IDS_FLAGS L"Flags" + IDS_SEARCH_TEXT L"SearchText" + IDS_REPLACE_TEXT L"ReplaceText" + IDS_MRU_ENABLED L"MRUEnabled" END #ifndef APSTUDIO_INVOKED diff --git a/src/modules/powerrename/lib/Settings.cpp b/src/modules/powerrename/lib/Settings.cpp index 531e5c5ebc..830fd3ef38 100644 --- a/src/modules/powerrename/lib/Settings.cpp +++ b/src/modules/powerrename/lib/Settings.cpp @@ -4,19 +4,20 @@ #include "Settings.h" #include "PowerRenameInterfaces.h" #include "resource.h" +#include -const wchar_t c_rootRegPath[] = L"Software\\Microsoft\\PowerRename"; -const wchar_t c_mruSearchRegPath[] = L"SearchMRU"; -const wchar_t c_mruReplaceRegPath[] = L"ReplaceMRU"; -const wchar_t c_enabled[] = L"Enabled"; -const wchar_t c_showIconOnMenu[] = L"ShowIcon"; -const wchar_t c_extendedContextMenuOnly[] = L"ExtendedContextMenuOnly"; -const wchar_t c_persistState[] = L"PersistState"; -const wchar_t c_maxMRUSize[] = L"MaxMRUSize"; -const wchar_t c_flags[] = L"Flags"; -const wchar_t c_searchText[] = L"SearchText"; -const wchar_t c_replaceText[] = L"ReplaceText"; -const wchar_t c_mruEnabled[] = L"MRUEnabled"; +const wchar_t* c_rootRegPath = GET_RESOURCE_STRING(IDS_ROOT_PATH).c_str(); +const wchar_t* c_mruSearchRegPath = GET_RESOURCE_STRING(IDS_SEARCH_PATH).c_str(); +const wchar_t* c_mruReplaceRegPath = GET_RESOURCE_STRING(IDS_REPLACE_PATH).c_str(); +const wchar_t* c_enabled = GET_RESOURCE_STRING(IDS_ENABLED).c_str(); +const wchar_t* c_showIconOnMenu = GET_RESOURCE_STRING(IDS_SHOW_ICON).c_str(); +const wchar_t* c_extendedContextMenuOnly = GET_RESOURCE_STRING(IDS_CONTEXT_MENU).c_str(); +const wchar_t* c_persistState = GET_RESOURCE_STRING(IDS_PERSIST_STATE).c_str(); +const wchar_t* c_maxMRUSize = GET_RESOURCE_STRING(IDS_MAX_MRU_SIZE).c_str(); +const wchar_t* c_flags = GET_RESOURCE_STRING(IDS_FLAGS).c_str(); +const wchar_t* c_searchText = GET_RESOURCE_STRING(IDS_SEARCH_TEXT).c_str(); +const wchar_t* c_replaceText = GET_RESOURCE_STRING(IDS_REPLACE_TEXT).c_str(); +const wchar_t* c_mruEnabled = GET_RESOURCE_STRING(IDS_MRU_ENABLED).c_str(); const bool c_enabledDefault = true; const bool c_showIconOnMenuDefault = true; diff --git a/src/modules/powerrename/testapp/PowerRenameTest.vcxproj b/src/modules/powerrename/testapp/PowerRenameTest.vcxproj index 1a9bd6b990..1e06bf571d 100644 --- a/src/modules/powerrename/testapp/PowerRenameTest.vcxproj +++ b/src/modules/powerrename/testapp/PowerRenameTest.vcxproj @@ -185,6 +185,11 @@ + + + {74485049-c722-400f-abe5-86ac52d929b3} + + From df1c6b9b0b80380aa8141fe2be247aa1779ec2bd Mon Sep 17 00:00:00 2001 From: Alekhya Kommuru Date: Fri, 17 Jan 2020 11:06:57 -0800 Subject: [PATCH 08/15] Removed get_res_string_wchar and used the get_resource_string() function instead which returns a wstring typecast into wchar* --- src/common/common.cpp | 760 ++++++++++-------- src/common/common.h | 42 +- .../powerrename/dll/PowerRenameExt.cpp | 4 +- src/modules/powerrename/dll/PowerRenameExt.h | 2 +- src/modules/powerrename/dll/dllmain.cpp | 11 +- 5 files changed, 436 insertions(+), 383 deletions(-) diff --git a/src/common/common.cpp b/src/common/common.cpp index 1a5677366f..3d3d326241 100644 --- a/src/common/common.cpp +++ b/src/common/common.cpp @@ -7,401 +7,467 @@ #include #include "version.h" -std::optional get_button_pos(HWND hwnd) { - RECT button; - if (DwmGetWindowAttribute(hwnd, DWMWA_CAPTION_BUTTON_BOUNDS, &button, sizeof(RECT)) == S_OK) { - return button; - } else { - return {}; - } +std::optional get_button_pos(HWND hwnd) +{ + RECT button; + if (DwmGetWindowAttribute(hwnd, DWMWA_CAPTION_BUTTON_BOUNDS, &button, sizeof(RECT)) == S_OK) + { + return button; + } + else + { + return {}; + } } -std::optional get_window_pos(HWND hwnd) { - RECT window; - if (DwmGetWindowAttribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &window, sizeof(window)) == S_OK) { - return window; - } else { - return {}; - } +std::optional get_window_pos(HWND hwnd) +{ + RECT window; + if (DwmGetWindowAttribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &window, sizeof(window)) == S_OK) + { + return window; + } + else + { + return {}; + } } -std::optional get_mouse_pos() { - POINT point; - if (GetCursorPos(&point) == 0) { - return {}; - } else { - return point; - } +std::optional get_mouse_pos() +{ + POINT point; + if (GetCursorPos(&point) == 0) + { + return {}; + } + else + { + return point; + } } -WindowAndProcPath get_filtered_base_window_and_path(HWND window) { - return hwnd_cache.get_window_and_path(window); +WindowAndProcPath get_filtered_base_window_and_path(HWND window) +{ + return hwnd_cache.get_window_and_path(window); } -HWND get_filtered_active_window() { - return hwnd_cache.get_window(GetForegroundWindow()); +HWND get_filtered_active_window() +{ + return hwnd_cache.get_window(GetForegroundWindow()); } -int width(const RECT& rect) { - return rect.right - rect.left; +int width(const RECT& rect) +{ + return rect.right - rect.left; } -int height(const RECT& rect) { - return rect.bottom - rect.top; +int height(const RECT& rect) +{ + return rect.bottom - rect.top; } -bool operator<(const RECT& lhs, const RECT& rhs) { - auto lhs_tuple = std::make_tuple(lhs.left, lhs.right, lhs.top, lhs.bottom); - auto rhs_tuple = std::make_tuple(rhs.left, rhs.right, rhs.top, rhs.bottom); - return lhs_tuple < rhs_tuple; +bool operator<(const RECT& lhs, const RECT& rhs) +{ + auto lhs_tuple = std::make_tuple(lhs.left, lhs.right, lhs.top, lhs.bottom); + auto rhs_tuple = std::make_tuple(rhs.left, rhs.right, rhs.top, rhs.bottom); + return lhs_tuple < rhs_tuple; } -RECT keep_rect_inside_rect(const RECT& small_rect, const RECT& big_rect) { - RECT result = small_rect; - if ((result.right - result.left) > (big_rect.right - big_rect.left)) { - // small_rect is too big horizontally. resize it. - result.right = big_rect.right; - result.left = big_rect.left; - } else { - if (result.right > big_rect.right) { - // move the rect left. - result.left -= result.right-big_rect.right; - result.right -= result.right-big_rect.right; +RECT keep_rect_inside_rect(const RECT& small_rect, const RECT& big_rect) +{ + RECT result = small_rect; + if ((result.right - result.left) > (big_rect.right - big_rect.left)) + { + // small_rect is too big horizontally. resize it. + result.right = big_rect.right; + result.left = big_rect.left; + } + else + { + if (result.right > big_rect.right) + { + // move the rect left. + result.left -= result.right - big_rect.right; + result.right -= result.right - big_rect.right; + } + + if (result.left < big_rect.left) + { + // move the rect right. + result.right += big_rect.left - result.left; + result.left += big_rect.left - result.left; + } } - if (result.left < big_rect.left) { - // move the rect right. - result.right += big_rect.left-result.left; - result.left += big_rect.left-result.left; + if ((result.bottom - result.top) > (big_rect.bottom - big_rect.top)) + { + // small_rect is too big vertically. resize it. + result.bottom = big_rect.bottom; + result.top = big_rect.top; } - } + else + { + if (result.bottom > big_rect.bottom) + { + // move the rect up. + result.top -= result.bottom - big_rect.bottom; + result.bottom -= result.bottom - big_rect.bottom; + } - if ((result.bottom - result.top) > (big_rect.bottom - big_rect.top)) { - // small_rect is too big vertically. resize it. - result.bottom = big_rect.bottom; - result.top = big_rect.top; - } else { - if (result.bottom > big_rect.bottom) { - // move the rect up. - result.top -= result.bottom-big_rect.bottom; - result.bottom -= result.bottom-big_rect.bottom; + if (result.top < big_rect.top) + { + // move the rect down. + result.bottom += big_rect.top - result.top; + result.top += big_rect.top - result.top; + } } - - if (result.top < big_rect.top) { - // move the rect down. - result.bottom += big_rect.top-result.top; - result.top += big_rect.top-result.top; - } - } - return result; + return result; } -int run_message_loop() { - MSG msg; - while (GetMessage(&msg, NULL, 0, 0)) { - TranslateMessage(&msg); - DispatchMessage(&msg); - } - return static_cast(msg.wParam); -} - -void show_last_error_message(LPCWSTR lpszFunction, DWORD dw) { - // Retrieve the system error message for the error code - LPWSTR lpMsgBuf = NULL; - if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - dw, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - lpMsgBuf, - 0, NULL) > 0) { - // Display the error message and exit the process - LPWSTR lpDisplayBuf = (LPWSTR)LocalAlloc(LMEM_ZEROINIT, (lstrlenW(lpMsgBuf) + lstrlenW(lpszFunction) + 40) * sizeof(WCHAR)); - if (lpDisplayBuf != NULL) { - StringCchPrintfW(lpDisplayBuf, - LocalSize(lpDisplayBuf) / sizeof(WCHAR), - L"%s failed with error %d: %s", - lpszFunction, dw, lpMsgBuf); - MessageBoxW(NULL, (LPCTSTR)lpDisplayBuf, L"Error", MB_OK); - LocalFree(lpDisplayBuf); +int run_message_loop() +{ + MSG msg; + while (GetMessage(&msg, NULL, 0, 0)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); } - LocalFree(lpMsgBuf); - } + return static_cast(msg.wParam); } -WindowState get_window_state(HWND hwnd) { - WINDOWPLACEMENT placement; - placement.length = sizeof(WINDOWPLACEMENT); - - if (GetWindowPlacement(hwnd, &placement) == 0) { - return UNKNONW; - } - - if (placement.showCmd == SW_MINIMIZE || placement.showCmd == SW_SHOWMINIMIZED || IsIconic(hwnd)) { - return MINIMIZED; - } - - if (placement.showCmd == SW_MAXIMIZE || placement.showCmd == SW_SHOWMAXIMIZED) { - return MAXIMIZED; - } - - auto rectp = get_window_pos(hwnd); - if (!rectp) { - return UNKNONW; - } - - auto rect = *rectp; - MONITORINFO monitor; - monitor.cbSize = sizeof(MONITORINFO); - auto h_monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); - GetMonitorInfo(h_monitor, &monitor); - bool top_left = monitor.rcWork.top == rect.top && monitor.rcWork.left == rect.left; - bool bottom_left = monitor.rcWork.bottom == rect.bottom && monitor.rcWork.left == rect.left; - bool top_right = monitor.rcWork.top == rect.top && monitor.rcWork.right == rect.right; - bool bottom_right = monitor.rcWork.bottom == rect.bottom && monitor.rcWork.right == rect.right; - - if (top_left && bottom_left) return SNAPED_LEFT; - if (top_left) return SNAPED_TOP_LEFT; - if (bottom_left) return SNAPED_BOTTOM_LEFT; - if (top_right && bottom_right) return SNAPED_RIGHT; - if (top_right) return SNAPED_TOP_RIGHT; - if (bottom_right) return SNAPED_BOTTOM_RIGHT; - - return RESTORED; -} - -bool is_process_elevated() { - 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); +void show_last_error_message(LPCWSTR lpszFunction, DWORD dw) +{ + // Retrieve the system error message for the error code + LPWSTR lpMsgBuf = NULL; + if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + dw, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + lpMsgBuf, + 0, + NULL) > 0) + { + // Display the error message and exit the process + LPWSTR lpDisplayBuf = (LPWSTR)LocalAlloc(LMEM_ZEROINIT, (lstrlenW(lpMsgBuf) + lstrlenW(lpszFunction) + 40) * sizeof(WCHAR)); + if (lpDisplayBuf != NULL) + { + StringCchPrintfW(lpDisplayBuf, + LocalSize(lpDisplayBuf) / sizeof(WCHAR), + L"%s failed with error %d: %s", + lpszFunction, + dw, + lpMsgBuf); + MessageBoxW(NULL, (LPCTSTR)lpDisplayBuf, L"Error", MB_OK); + LocalFree(lpDisplayBuf); + } + LocalFree(lpMsgBuf); } - } +} - if (token) { +WindowState get_window_state(HWND hwnd) +{ + WINDOWPLACEMENT placement; + placement.length = sizeof(WINDOWPLACEMENT); + + if (GetWindowPlacement(hwnd, &placement) == 0) + { + return UNKNONW; + } + + if (placement.showCmd == SW_MINIMIZE || placement.showCmd == SW_SHOWMINIMIZED || IsIconic(hwnd)) + { + return MINIMIZED; + } + + if (placement.showCmd == SW_MAXIMIZE || placement.showCmd == SW_SHOWMAXIMIZED) + { + return MAXIMIZED; + } + + auto rectp = get_window_pos(hwnd); + if (!rectp) + { + return UNKNONW; + } + + auto rect = *rectp; + MONITORINFO monitor; + monitor.cbSize = sizeof(MONITORINFO); + auto h_monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); + GetMonitorInfo(h_monitor, &monitor); + bool top_left = monitor.rcWork.top == rect.top && monitor.rcWork.left == rect.left; + bool bottom_left = monitor.rcWork.bottom == rect.bottom && monitor.rcWork.left == rect.left; + bool top_right = monitor.rcWork.top == rect.top && monitor.rcWork.right == rect.right; + bool bottom_right = monitor.rcWork.bottom == rect.bottom && monitor.rcWork.right == rect.right; + + if (top_left && bottom_left) + return SNAPED_LEFT; + if (top_left) + return SNAPED_TOP_LEFT; + if (bottom_left) + return SNAPED_BOTTOM_LEFT; + if (top_right && bottom_right) + return SNAPED_RIGHT; + if (top_right) + return SNAPED_TOP_RIGHT; + if (bottom_right) + return SNAPED_BOTTOM_RIGHT; + + return RESTORED; +} + +bool is_process_elevated() +{ + 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; +} + +bool drop_elevated_privileges() +{ + HANDLE token = nullptr; + LPCTSTR lpszPrivilege = SE_SECURITY_NAME; + 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; + DWORD size = (DWORD)sizeof(TOKEN_MANDATORY_LABEL) + ::GetLengthSid(medium_sid); + + BOOL result = SetTokenInformation(token, TokenIntegrityLevel, &label, size); + LocalFree(medium_sid); CloseHandle(token); - } - return elevated; + return result; } -bool drop_elevated_privileges() { - HANDLE token = nullptr; - LPCTSTR lpszPrivilege = SE_SECURITY_NAME; - 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; - DWORD size = (DWORD)sizeof(TOKEN_MANDATORY_LABEL) + ::GetLengthSid(medium_sid); - - BOOL result = SetTokenInformation(token, TokenIntegrityLevel, &label, size); - LocalFree(medium_sid); - CloseHandle(token); - - return result; -} - -std::wstring get_process_path(DWORD pid) noexcept { - auto process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, TRUE, pid); - std::wstring name; - if (process != INVALID_HANDLE_VALUE) { - name.resize(MAX_PATH); - DWORD name_length = static_cast(name.length()); - if (QueryFullProcessImageNameW(process, 0, (LPWSTR)name.data(), &name_length) == 0) { - name_length = 0; +std::wstring get_process_path(DWORD pid) noexcept +{ + auto process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, TRUE, pid); + std::wstring name; + if (process != INVALID_HANDLE_VALUE) + { + name.resize(MAX_PATH); + DWORD name_length = static_cast(name.length()); + if (QueryFullProcessImageNameW(process, 0, (LPWSTR)name.data(), &name_length) == 0) + { + name_length = 0; + } + name.resize(name_length); + CloseHandle(process); } - name.resize(name_length); - CloseHandle(process); - } - return name; + return name; } -bool run_elevated(const std::wstring& file, const std::wstring& params) { - 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; - exec_info.lpDirectory = 0; - exec_info.hInstApp = 0; +bool run_elevated(const std::wstring& file, const std::wstring& params) +{ + 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; + exec_info.lpDirectory = 0; + exec_info.hInstApp = 0; - if (ShellExecuteExW(&exec_info)) { - return exec_info.hProcess != nullptr; - } else { - return false; - } -} - -bool run_non_elevated(const std::wstring& file, const std::wstring& params) { - auto executable_args = file; - if (!params.empty()) { - executable_args += L" " + params; - } - - HWND hwnd = GetShellWindow(); - if (!hwnd) { - return false; - } - DWORD pid; - GetWindowThreadProcessId(hwnd, &pid); - - winrt::handle process{ OpenProcess(PROCESS_CREATE_PROCESS, FALSE, pid) }; - if (!process) { - return false; - } - - SIZE_T size = 0; - - InitializeProcThreadAttributeList(nullptr, 1, 0, &size); - auto pproc_buffer = std::make_unique(size); - auto pptal = reinterpret_cast(pproc_buffer.get()); - - if (!InitializeProcThreadAttributeList(pptal, 1, 0, &size)) { - return false; - } - - HANDLE process_handle = process.get(); - if (!pptal || !UpdateProcThreadAttribute(pptal, - 0, - PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, - &process_handle, - sizeof(process_handle), - nullptr, - nullptr)) { - return false; - } - - STARTUPINFOEX siex = { 0 }; - siex.lpAttributeList = pptal; - siex.StartupInfo.cb = sizeof(siex); - - PROCESS_INFORMATION process_info = { 0 }; - auto succedded = CreateProcessW(file.c_str(), - const_cast(executable_args.c_str()), - nullptr, - nullptr, - FALSE, - EXTENDED_STARTUPINFO_PRESENT, - nullptr, - nullptr, - &siex.StartupInfo, - &process_info); - if (process_info.hProcess) { - CloseHandle(process_info.hProcess); - } - if (process_info.hThread) { - CloseHandle(process_info.hThread); - } - return succedded; -} - -bool run_same_elevation(const std::wstring& file, const std::wstring& params) { - auto executable_args = file; - if (!params.empty()) { - executable_args += L" " + params; - } - STARTUPINFO si = { 0 }; - PROCESS_INFORMATION pi = { 0 }; - auto succedded = CreateProcessW(file.c_str(), - const_cast(executable_args.c_str()), - nullptr, - nullptr, - FALSE, - 0, - nullptr, - nullptr, - &si, - &pi); - if (pi.hProcess) { - CloseHandle(pi.hProcess); - } - if (pi.hThread) { - CloseHandle(pi.hThread); - } - return succedded; -} - - -std::wstring get_process_path(HWND window) noexcept { - const static std::wstring app_frame_host = L"ApplicationFrameHost.exe"; - DWORD pid{}; - GetWindowThreadProcessId(window, &pid); - auto name = get_process_path(pid); - if (name.length() >= app_frame_host.length() && - name.compare(name.length() - app_frame_host.length(), app_frame_host.length(), app_frame_host) == 0) { - // It is a UWP app. We will enumarate the windows and look for one created - // by something with a different PID - DWORD new_pid = pid; - EnumChildWindows(window, [](HWND hwnd, LPARAM param) -> BOOL { - auto new_pid_ptr = reinterpret_cast(param); - DWORD pid; - GetWindowThreadProcessId(hwnd, &pid); - if (pid != *new_pid_ptr) { - *new_pid_ptr = pid; - return FALSE; - } else { - return TRUE; - } - }, reinterpret_cast(&new_pid)); - // If we have a new pid, get the new name. - if (new_pid != pid) { - return get_process_path(new_pid); + if (ShellExecuteExW(&exec_info)) + { + return exec_info.hProcess != nullptr; + } + else + { + return false; } - } - return name; } -std::wstring get_product_version() { - static std::wstring version = std::to_wstring(VERSION_MAJOR) + - L"." + std::to_wstring(VERSION_MINOR) + - L"." + std::to_wstring(VERSION_REVISION) + - L"." + std::to_wstring(VERSION_BUILD); +bool run_non_elevated(const std::wstring& file, const std::wstring& params) +{ + auto executable_args = file; + if (!params.empty()) + { + executable_args += L" " + params; + } - return version; + HWND hwnd = GetShellWindow(); + if (!hwnd) + { + return false; + } + DWORD pid; + GetWindowThreadProcessId(hwnd, &pid); + + winrt::handle process{ OpenProcess(PROCESS_CREATE_PROCESS, FALSE, pid) }; + if (!process) + { + return false; + } + + SIZE_T size = 0; + + InitializeProcThreadAttributeList(nullptr, 1, 0, &size); + auto pproc_buffer = std::make_unique(size); + auto pptal = reinterpret_cast(pproc_buffer.get()); + + if (!InitializeProcThreadAttributeList(pptal, 1, 0, &size)) + { + return false; + } + + HANDLE process_handle = process.get(); + if (!pptal || !UpdateProcThreadAttribute(pptal, + 0, + PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, + &process_handle, + sizeof(process_handle), + nullptr, + nullptr)) + { + return false; + } + + STARTUPINFOEX siex = { 0 }; + siex.lpAttributeList = pptal; + siex.StartupInfo.cb = sizeof(siex); + + PROCESS_INFORMATION process_info = { 0 }; + auto succedded = CreateProcessW(file.c_str(), + const_cast(executable_args.c_str()), + nullptr, + nullptr, + FALSE, + EXTENDED_STARTUPINFO_PRESENT, + nullptr, + nullptr, + &siex.StartupInfo, + &process_info); + if (process_info.hProcess) + { + CloseHandle(process_info.hProcess); + } + if (process_info.hThread) + { + CloseHandle(process_info.hThread); + } + return succedded; } -std::wstring get_resource_string(UINT resource_id, HINSTANCE instance, const wchar_t* fallback) { - wchar_t* text_ptr; - auto length = LoadStringW(instance, resource_id, reinterpret_cast(&text_ptr), 0); - if (length == 0) { - return fallback; - } else { - return { text_ptr, static_cast(length) }; - } +bool run_same_elevation(const std::wstring& file, const std::wstring& params) +{ + auto executable_args = file; + if (!params.empty()) + { + executable_args += L" " + params; + } + STARTUPINFO si = { 0 }; + PROCESS_INFORMATION pi = { 0 }; + auto succedded = CreateProcessW(file.c_str(), + const_cast(executable_args.c_str()), + nullptr, + nullptr, + FALSE, + 0, + nullptr, + nullptr, + &si, + &pi); + if (pi.hProcess) + { + CloseHandle(pi.hProcess); + } + if (pi.hThread) + { + CloseHandle(pi.hThread); + } + return succedded; } -// function to return the string as a to a wchar_t* (to enable localization in cases where the return type did not accept wstring) -wchar_t* get_resource_string_wchar(UINT resource_id, HINSTANCE instance) +std::wstring get_process_path(HWND window) noexcept +{ + const static std::wstring app_frame_host = L"ApplicationFrameHost.exe"; + DWORD pid{}; + GetWindowThreadProcessId(window, &pid); + auto name = get_process_path(pid); + if (name.length() >= app_frame_host.length() && + name.compare(name.length() - app_frame_host.length(), app_frame_host.length(), app_frame_host) == 0) + { + // It is a UWP app. We will enumarate the windows and look for one created + // by something with a different PID + DWORD new_pid = pid; + EnumChildWindows( + window, [](HWND hwnd, LPARAM param) -> BOOL { + auto new_pid_ptr = reinterpret_cast(param); + DWORD pid; + GetWindowThreadProcessId(hwnd, &pid); + if (pid != *new_pid_ptr) + { + *new_pid_ptr = pid; + return FALSE; + } + else + { + return TRUE; + } + }, + reinterpret_cast(&new_pid)); + // If we have a new pid, get the new name. + if (new_pid != pid) + { + return get_process_path(new_pid); + } + } + return name; +} + +std::wstring get_product_version() +{ + static std::wstring version = std::to_wstring(VERSION_MAJOR) + + L"." + std::to_wstring(VERSION_MINOR) + + L"." + std::to_wstring(VERSION_REVISION) + + L"." + std::to_wstring(VERSION_BUILD); + + return version; +} + +std::wstring get_resource_string(UINT resource_id, HINSTANCE instance, const wchar_t* fallback) { wchar_t* text_ptr; - unsigned int length = LoadStringW(instance, resource_id, reinterpret_cast(&text_ptr), 0); - std::wstring res_string = { *reinterpret_cast(&text_ptr), length }; - length++; - - if (length > 1) + auto length = LoadStringW(instance, resource_id, reinterpret_cast(&text_ptr), 0); + if (length == 0) { - wchar_t* tmp_res_ptr; - tmp_res_ptr = new wchar_t[length]; - wmemcpy(tmp_res_ptr, res_string.c_str(), length); - return tmp_res_ptr; + return fallback; + } + else + { + return { text_ptr, static_cast(length) }; } - - return (wchar_t*)L"test"; } std::wstring get_module_filename(HMODULE mod) diff --git a/src/common/common.h b/src/common/common.h index 5aaa215c50..5e17572a29 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -14,9 +14,10 @@ std::optional get_mouse_pos(); // Gets active window, filtering out all "non standard" windows like the taskbar, etc. HWND get_filtered_active_window(); // Gets window ancestor (usualy the window we want to do stuff with), filtering out all "non standard" windows like the taskbar, etc. and provide the app process path -struct WindowAndProcPath { - HWND hwnd = nullptr; - std::wstring process_path; +struct WindowAndProcPath +{ + HWND hwnd = nullptr; + std::wstring process_path; }; WindowAndProcPath get_filtered_base_window_and_path(HWND window); @@ -32,17 +33,18 @@ int run_message_loop(); void show_last_error_message(LPCWSTR lpszFunction, DWORD dw); -enum WindowState { - UNKNONW, - MINIMIZED, - MAXIMIZED, - SNAPED_TOP_LEFT, - SNAPED_LEFT, - SNAPED_BOTTOM_LEFT, - SNAPED_TOP_RIGHT, - SNAPED_RIGHT, - SNAPED_BOTTOM_RIGHT, - RESTORED +enum WindowState +{ + UNKNONW, + MINIMIZED, + MAXIMIZED, + SNAPED_TOP_LEFT, + SNAPED_LEFT, + SNAPED_BOTTOM_LEFT, + SNAPED_TOP_RIGHT, + SNAPED_RIGHT, + SNAPED_BOTTOM_RIGHT, + RESTORED }; WindowState get_window_state(HWND hwnd); @@ -78,15 +80,3 @@ std::wstring get_resource_string(UINT resource_id, HINSTANCE instance, const wch // extern "C" IMAGE_DOS_HEADER __ImageBase; // is added to the .cpp file. #define GET_RESOURCE_STRING(resource_id) get_resource_string(resource_id, reinterpret_cast(&__ImageBase), L#resource_id) - -// Function which takes a pointer ptr and allocates space before populating it with the string from the resource table -// using this approach as wstring cannot be cast to PCWSTR without allocating space -// hence, we have to malloc and free the pointer after each use -wchar_t* get_resource_string_wchar(UINT resource_id, HINSTANCE instance); - -// Wrapper for getting a string from the resource file. -// Requires that -// extern "C" IMAGE_DOS_HEADER __ImageBase; -// is added to the .cpp file. -// used when the return type must be a wchar_t* instead of a wstring. -#define GET_RES_STRING_WCHAR(resource_id) get_resource_string_wchar(resource_id, reinterpret_cast(&__ImageBase)) diff --git a/src/modules/powerrename/dll/PowerRenameExt.cpp b/src/modules/powerrename/dll/PowerRenameExt.cpp index cf9ec0d339..cc700d304b 100644 --- a/src/modules/powerrename/dll/PowerRenameExt.cpp +++ b/src/modules/powerrename/dll/PowerRenameExt.cpp @@ -197,8 +197,8 @@ DWORD WINAPI CPowerRenameMenu::s_PowerRenameUIThreadProc(_In_ void* pData) HRESULT __stdcall CPowerRenameMenu::GetTitle(IShellItemArray* /*psiItemArray*/, LPWSTR* ppszName) { - app_name = GET_RES_STRING_WCHAR(IDS_POWERRENAME); - return SHStrDup(app_name, ppszName); + app_name = GET_RESOURCE_STRING(IDS_POWERRENAME); + return SHStrDup(app_name.c_str(), ppszName); } HRESULT __stdcall CPowerRenameMenu::GetIcon(IShellItemArray* /*psiItemArray*/, LPWSTR* ppszIcon) diff --git a/src/modules/powerrename/dll/PowerRenameExt.h b/src/modules/powerrename/dll/PowerRenameExt.h index 0b1f0c75f1..01906d14ab 100644 --- a/src/modules/powerrename/dll/PowerRenameExt.h +++ b/src/modules/powerrename/dll/PowerRenameExt.h @@ -71,5 +71,5 @@ private: std::atomic m_refCount = 1; HBITMAP m_hbmpIcon = nullptr; CComPtr m_spdo; - wchar_t* app_name; + std::wstring app_name; }; diff --git a/src/modules/powerrename/dll/dllmain.cpp b/src/modules/powerrename/dll/dllmain.cpp index adc3970bc6..fc7e12521b 100644 --- a/src/modules/powerrename/dll/dllmain.cpp +++ b/src/modules/powerrename/dll/dllmain.cpp @@ -161,14 +161,14 @@ class PowerRenameModule : public PowertoyModuleIface private: // Enabled by default bool m_enabled = true; - wchar_t* app_name; + std::wstring app_name; public: // Return the display name of the powertoy, this will be cached virtual PCWSTR get_name() override { - app_name = GET_RES_STRING_WCHAR(IDS_POWERRENAME); - return app_name; + app_name = GET_RESOURCE_STRING(IDS_POWERRENAME); + return app_name.c_str(); } // Enable the powertoy @@ -303,10 +303,7 @@ public: init_settings(); } - ~PowerRenameModule() - { - delete app_name; - } + ~PowerRenameModule(){}; }; extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create() From 44ac22c0ded6ada4c7a749b17f6ad8d1640104ca Mon Sep 17 00:00:00 2001 From: Alekhya Kommuru Date: Fri, 17 Jan 2020 11:12:23 -0800 Subject: [PATCH 09/15] Added new lines to the end of the file --- src/modules/powerrename/dll/PowerRenameExt.cpp | 2 +- src/modules/powerrename/dll/dllmain.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/powerrename/dll/PowerRenameExt.cpp b/src/modules/powerrename/dll/PowerRenameExt.cpp index cc700d304b..7778afa5b2 100644 --- a/src/modules/powerrename/dll/PowerRenameExt.cpp +++ b/src/modules/powerrename/dll/PowerRenameExt.cpp @@ -271,4 +271,4 @@ HRESULT __stdcall CPowerRenameMenu::EnumSubCommands(IEnumExplorerCommand** ppEnu { *ppEnum = nullptr; return E_NOTIMPL; -} \ No newline at end of file +} diff --git a/src/modules/powerrename/dll/dllmain.cpp b/src/modules/powerrename/dll/dllmain.cpp index fc7e12521b..832ffac048 100644 --- a/src/modules/powerrename/dll/dllmain.cpp +++ b/src/modules/powerrename/dll/dllmain.cpp @@ -309,4 +309,4 @@ public: extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create() { return new PowerRenameModule(); -} \ No newline at end of file +} From a504a75166a7a8d50f33885e39a07d6f3f94870a Mon Sep 17 00:00:00 2001 From: Alekhya Kommuru Date: Fri, 17 Jan 2020 11:25:42 -0800 Subject: [PATCH 10/15] Removed string resources from the settings.cpp file --- src/modules/powerrename/lib/PowerRenameLib.rc | 73 ----------------- src/modules/powerrename/lib/Settings.cpp | 78 ++++++++++--------- src/modules/powerrename/lib/resource.h | 28 ------- 3 files changed, 43 insertions(+), 136 deletions(-) delete mode 100644 src/modules/powerrename/lib/PowerRenameLib.rc delete mode 100644 src/modules/powerrename/lib/resource.h diff --git a/src/modules/powerrename/lib/PowerRenameLib.rc b/src/modules/powerrename/lib/PowerRenameLib.rc deleted file mode 100644 index 6597877d64..0000000000 --- a/src/modules/powerrename/lib/PowerRenameLib.rc +++ /dev/null @@ -1,73 +0,0 @@ -// Microsoft Visual C++ generated resource script. -// -#include "resource.h" - -#define APSTUDIO_READONLY_SYMBOLS -///////////////////////////////////////////////////////////////////////////// -// -// Generated from the TEXTINCLUDE 2 resource. -// -#include "winres.h" - -///////////////////////////////////////////////////////////////////////////// -#undef APSTUDIO_READONLY_SYMBOLS - -///////////////////////////////////////////////////////////////////////////// -// English (United States) resources - -#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) -LANGUAGE 9, 1 - -#ifdef APSTUDIO_INVOKED -///////////////////////////////////////////////////////////////////////////// -// -// TEXTINCLUDE -// - -1 TEXTINCLUDE -BEGIN - "resource.h\0" -END - -2 TEXTINCLUDE -BEGIN - "#include ""winres.h""\r\n" - "\0" -END - -3 TEXTINCLUDE -BEGIN - "\r\n" - "\0" -END - -#endif // APSTUDIO_INVOKED - -#endif // English (United States) resources -///////////////////////////////////////////////////////////////////////////// - -STRINGTABLE -BEGIN - IDS_ROOT_PATH L"Software\\Microsoft\\PowerRename" - IDS_SEARCH_PATH L"SearchMRU" - IDS_REPLACE_PATH L"ReplaceMRU" - IDS_ENABLED L"Enabled" - IDS_SHOW_ICON L"ShowIcon" - IDS_CONTEXT_MENU L"ExtendedContextMenuOnly" - IDS_PERSIST_STATE L"PersistState" - IDS_MAX_MRU_SIZE L"MaxMRUSize" - IDS_FLAGS L"Flags" - IDS_SEARCH_TEXT L"SearchText" - IDS_REPLACE_TEXT L"ReplaceText" - IDS_MRU_ENABLED L"MRUEnabled" -END - -#ifndef APSTUDIO_INVOKED -///////////////////////////////////////////////////////////////////////////// -// -// Generated from the TEXTINCLUDE 3 resource. -// - - -///////////////////////////////////////////////////////////////////////////// -#endif // not APSTUDIO_INVOKED diff --git a/src/modules/powerrename/lib/Settings.cpp b/src/modules/powerrename/lib/Settings.cpp index 830fd3ef38..bc5bfebaea 100644 --- a/src/modules/powerrename/lib/Settings.cpp +++ b/src/modules/powerrename/lib/Settings.cpp @@ -3,21 +3,21 @@ #include #include "Settings.h" #include "PowerRenameInterfaces.h" -#include "resource.h" -#include -const wchar_t* c_rootRegPath = GET_RESOURCE_STRING(IDS_ROOT_PATH).c_str(); -const wchar_t* c_mruSearchRegPath = GET_RESOURCE_STRING(IDS_SEARCH_PATH).c_str(); -const wchar_t* c_mruReplaceRegPath = GET_RESOURCE_STRING(IDS_REPLACE_PATH).c_str(); -const wchar_t* c_enabled = GET_RESOURCE_STRING(IDS_ENABLED).c_str(); -const wchar_t* c_showIconOnMenu = GET_RESOURCE_STRING(IDS_SHOW_ICON).c_str(); -const wchar_t* c_extendedContextMenuOnly = GET_RESOURCE_STRING(IDS_CONTEXT_MENU).c_str(); -const wchar_t* c_persistState = GET_RESOURCE_STRING(IDS_PERSIST_STATE).c_str(); -const wchar_t* c_maxMRUSize = GET_RESOURCE_STRING(IDS_MAX_MRU_SIZE).c_str(); -const wchar_t* c_flags = GET_RESOURCE_STRING(IDS_FLAGS).c_str(); -const wchar_t* c_searchText = GET_RESOURCE_STRING(IDS_SEARCH_TEXT).c_str(); -const wchar_t* c_replaceText = GET_RESOURCE_STRING(IDS_REPLACE_TEXT).c_str(); -const wchar_t* c_mruEnabled = GET_RESOURCE_STRING(IDS_MRU_ENABLED).c_str(); +// Note: Not moving these strings to the resource file as these are internal and used as IDs +const wchar_t c_rootRegPath[] = L"Software\\Microsoft\\PowerRename"; +const wchar_t c_mruSearchRegPath[] = L"SearchMRU"; +const wchar_t c_mruReplaceRegPath[] = L"ReplaceMRU"; + +const wchar_t c_enabled[] = L"Enabled"; +const wchar_t c_showIconOnMenu[] = L"ShowIcon"; +const wchar_t c_extendedContextMenuOnly[] = L"ExtendedContextMenuOnly"; +const wchar_t c_persistState[] = L"PersistState"; +const wchar_t c_maxMRUSize[] = L"MaxMRUSize"; +const wchar_t c_flags[] = L"Flags"; +const wchar_t c_searchText[] = L"SearchText"; +const wchar_t c_replaceText[] = L"ReplaceText"; +const wchar_t c_mruEnabled[] = L"MRUEnabled"; const bool c_enabledDefault = true; const bool c_showIconOnMenuDefault = true; @@ -167,15 +167,15 @@ bool CSettings::GetRegStringValue(_In_ PCWSTR valueName, __out_ecount(cchBuf) PW return (SUCCEEDED(HRESULT_FROM_WIN32(SHGetValue(HKEY_CURRENT_USER, c_rootRegPath, valueName, &type, value, &cb) == ERROR_SUCCESS))); } +typedef int(CALLBACK* MRUCMPPROC)(LPCWSTR, LPCWSTR); -typedef int (CALLBACK* MRUCMPPROC)(LPCWSTR, LPCWSTR); - -typedef struct { - DWORD cbSize; - UINT uMax; - UINT fFlags; - HKEY hKey; - LPCTSTR lpszSubKey; +typedef struct +{ + DWORD cbSize; + UINT uMax; + UINT fFlags; + HKEY hKey; + LPCTSTR lpszSubKey; MRUCMPPROC lpfnCompare; } MRUINFO; @@ -190,15 +190,21 @@ class CRenameMRU : { public: // IUnknown - IFACEMETHODIMP_(ULONG) AddRef(); - IFACEMETHODIMP_(ULONG) Release(); + IFACEMETHODIMP_(ULONG) + AddRef(); + IFACEMETHODIMP_(ULONG) + Release(); IFACEMETHODIMP QueryInterface(_In_ REFIID riid, _Outptr_ void** ppv); // IEnumString IFACEMETHODIMP Next(__in ULONG celt, __out_ecount_part(celt, *pceltFetched) LPOLESTR* rgelt, __out_opt ULONG* pceltFetched); IFACEMETHODIMP Skip(__in ULONG) { return E_NOTIMPL; } IFACEMETHODIMP Reset(); - IFACEMETHODIMP Clone(__deref_out IEnumString** ppenum) { *ppenum = nullptr; return E_NOTIMPL; } + IFACEMETHODIMP Clone(__deref_out IEnumString** ppenum) + { + *ppenum = nullptr; + return E_NOTIMPL; + } // IPowerRenameMRU IFACEMETHODIMP AddMRUString(_In_ PCWSTR entry); @@ -215,19 +221,20 @@ private: int _EnumMRUList(_In_ int nItem, _Out_ void* lpData, _In_ UINT uLen); void _FreeMRUList(); - long m_refCount = 0; - HKEY m_hKey = NULL; - ULONG m_maxMRUSize = 0; - ULONG m_mruIndex = 0; - ULONG m_mruSize = 0; + long m_refCount = 0; + HKEY m_hKey = NULL; + ULONG m_maxMRUSize = 0; + ULONG m_mruIndex = 0; + ULONG m_mruSize = 0; HANDLE m_mruHandle = NULL; HMODULE m_hComctl32Dll = NULL; - PWSTR m_regPath = nullptr; + PWSTR m_regPath = nullptr; }; CRenameMRU::CRenameMRU() : m_refCount(1) -{} +{ +} CRenameMRU::~CRenameMRU() { @@ -270,12 +277,14 @@ HRESULT CRenameMRU::CreateInstance(_In_ PCWSTR regPathMRU, _In_ ULONG maxMRUSize } // IUnknown -IFACEMETHODIMP_(ULONG) CRenameMRU::AddRef() +IFACEMETHODIMP_(ULONG) +CRenameMRU::AddRef() { return InterlockedIncrement(&m_refCount); } -IFACEMETHODIMP_(ULONG) CRenameMRU::Release() +IFACEMETHODIMP_(ULONG) +CRenameMRU::Release() { long refCount = InterlockedDecrement(&m_refCount); @@ -466,7 +475,6 @@ void CRenameMRU::_FreeMRUList() { pfnFreeMRUList(m_mruHandle); } - } m_mruHandle = NULL; } diff --git a/src/modules/powerrename/lib/resource.h b/src/modules/powerrename/lib/resource.h deleted file mode 100644 index ff2a4a1a64..0000000000 --- a/src/modules/powerrename/lib/resource.h +++ /dev/null @@ -1,28 +0,0 @@ -//{{NO_DEPENDENCIES}} -// Microsoft Visual C++ generated include file. -// Used by PowerRenameLib.rc - -#define IDS_ROOT_PATH 100 -#define IDS_SEARCH_PATH 101 -#define IDS_REPLACE_PATH 102 -#define IDS_ENABLED 103 -#define IDS_SHOW_ICON 104 -#define IDS_CONTEXT_MENU 105 -#define IDS_PERSIST_STATE 106 -#define IDS_MAX_MRU_SIZE 107 -#define IDS_FLAGS 108 -#define IDS_SEARCH_TEXT 109 -#define IDS_REPLACE_TEXT 110 -#define IDS_MRU_ENABLED 111 - - -// Next default values for new objects -// -#ifdef APSTUDIO_INVOKED -#ifndef APSTUDIO_READONLY_SYMBOLS -#define _APS_NEXT_RESOURCE_VALUE 101 -#define _APS_NEXT_COMMAND_VALUE 40001 -#define _APS_NEXT_CONTROL_VALUE 1001 -#define _APS_NEXT_SYMED_VALUE 101 -#endif -#endif From bf487293541fbdcfb5a05c74afe211d3e578a835 Mon Sep 17 00:00:00 2001 From: Alekhya Kommuru Date: Fri, 17 Jan 2020 11:57:53 -0800 Subject: [PATCH 11/15] rebuilt project PowerRename --- src/modules/powerrename/lib/PowerRenameLib.vcxproj | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/modules/powerrename/lib/PowerRenameLib.vcxproj b/src/modules/powerrename/lib/PowerRenameLib.vcxproj index 8acd064065..52d9243d62 100644 --- a/src/modules/powerrename/lib/PowerRenameLib.vcxproj +++ b/src/modules/powerrename/lib/PowerRenameLib.vcxproj @@ -147,7 +147,6 @@ - @@ -168,9 +167,6 @@ - - - From 197286c21e923e3b962a76c83d73fe72c560f637 Mon Sep 17 00:00:00 2001 From: Alekhya Kommuru Date: Fri, 17 Jan 2020 14:23:07 -0800 Subject: [PATCH 12/15] moved app name to constructor to init only once --- src/modules/powerrename/dll/PowerRenameExt.cpp | 2 +- src/modules/powerrename/dll/dllmain.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/powerrename/dll/PowerRenameExt.cpp b/src/modules/powerrename/dll/PowerRenameExt.cpp index 7778afa5b2..898fc92db1 100644 --- a/src/modules/powerrename/dll/PowerRenameExt.cpp +++ b/src/modules/powerrename/dll/PowerRenameExt.cpp @@ -21,6 +21,7 @@ struct InvokeStruct CPowerRenameMenu::CPowerRenameMenu() { ModuleAddRef(); + app_name = GET_RESOURCE_STRING(IDS_POWERRENAME); } CPowerRenameMenu::~CPowerRenameMenu() @@ -197,7 +198,6 @@ DWORD WINAPI CPowerRenameMenu::s_PowerRenameUIThreadProc(_In_ void* pData) HRESULT __stdcall CPowerRenameMenu::GetTitle(IShellItemArray* /*psiItemArray*/, LPWSTR* ppszName) { - app_name = GET_RESOURCE_STRING(IDS_POWERRENAME); return SHStrDup(app_name.c_str(), ppszName); } diff --git a/src/modules/powerrename/dll/dllmain.cpp b/src/modules/powerrename/dll/dllmain.cpp index 832ffac048..0bfa4459af 100644 --- a/src/modules/powerrename/dll/dllmain.cpp +++ b/src/modules/powerrename/dll/dllmain.cpp @@ -167,7 +167,6 @@ public: // Return the display name of the powertoy, this will be cached virtual PCWSTR get_name() override { - app_name = GET_RESOURCE_STRING(IDS_POWERRENAME); return app_name.c_str(); } @@ -301,6 +300,7 @@ public: PowerRenameModule() { init_settings(); + app_name = GET_RESOURCE_STRING(IDS_POWERRENAME); } ~PowerRenameModule(){}; From 8132bbac2eac25a7d460ee32bd03bc36a35d8ea7 Mon Sep 17 00:00:00 2001 From: Alekhya Kommuru Date: Fri, 17 Jan 2020 14:32:02 -0800 Subject: [PATCH 13/15] updated formatting of common.cpp --- src/common/common.cpp | 731 +++++++++++++++++++----------------------- 1 file changed, 323 insertions(+), 408 deletions(-) diff --git a/src/common/common.cpp b/src/common/common.cpp index 3d3d326241..73b3cef189 100644 --- a/src/common/common.cpp +++ b/src/common/common.cpp @@ -7,467 +7,382 @@ #include #include "version.h" -std::optional get_button_pos(HWND hwnd) -{ - RECT button; - if (DwmGetWindowAttribute(hwnd, DWMWA_CAPTION_BUTTON_BOUNDS, &button, sizeof(RECT)) == S_OK) - { - return button; - } - else - { - return {}; - } +std::optional get_button_pos(HWND hwnd) { + RECT button; + if (DwmGetWindowAttribute(hwnd, DWMWA_CAPTION_BUTTON_BOUNDS, &button, sizeof(RECT)) == S_OK) { + return button; + } else { + return {}; + } } -std::optional get_window_pos(HWND hwnd) -{ - RECT window; - if (DwmGetWindowAttribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &window, sizeof(window)) == S_OK) - { - return window; - } - else - { - return {}; - } +std::optional get_window_pos(HWND hwnd) { + RECT window; + if (DwmGetWindowAttribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &window, sizeof(window)) == S_OK) { + return window; + } else { + return {}; + } } -std::optional get_mouse_pos() -{ - POINT point; - if (GetCursorPos(&point) == 0) - { - return {}; - } - else - { - return point; - } +std::optional get_mouse_pos() { + POINT point; + if (GetCursorPos(&point) == 0) { + return {}; + } else { + return point; + } } -WindowAndProcPath get_filtered_base_window_and_path(HWND window) -{ - return hwnd_cache.get_window_and_path(window); +WindowAndProcPath get_filtered_base_window_and_path(HWND window) { + return hwnd_cache.get_window_and_path(window); } -HWND get_filtered_active_window() -{ - return hwnd_cache.get_window(GetForegroundWindow()); +HWND get_filtered_active_window() { + return hwnd_cache.get_window(GetForegroundWindow()); } -int width(const RECT& rect) -{ - return rect.right - rect.left; +int width(const RECT& rect) { + return rect.right - rect.left; } -int height(const RECT& rect) -{ - return rect.bottom - rect.top; +int height(const RECT& rect) { + return rect.bottom - rect.top; } -bool operator<(const RECT& lhs, const RECT& rhs) -{ - auto lhs_tuple = std::make_tuple(lhs.left, lhs.right, lhs.top, lhs.bottom); - auto rhs_tuple = std::make_tuple(rhs.left, rhs.right, rhs.top, rhs.bottom); - return lhs_tuple < rhs_tuple; +bool operator<(const RECT& lhs, const RECT& rhs) { + auto lhs_tuple = std::make_tuple(lhs.left, lhs.right, lhs.top, lhs.bottom); + auto rhs_tuple = std::make_tuple(rhs.left, rhs.right, rhs.top, rhs.bottom); + return lhs_tuple < rhs_tuple; } -RECT keep_rect_inside_rect(const RECT& small_rect, const RECT& big_rect) -{ - RECT result = small_rect; - if ((result.right - result.left) > (big_rect.right - big_rect.left)) - { - // small_rect is too big horizontally. resize it. - result.right = big_rect.right; - result.left = big_rect.left; - } - else - { - if (result.right > big_rect.right) - { - // move the rect left. - result.left -= result.right - big_rect.right; - result.right -= result.right - big_rect.right; - } - - if (result.left < big_rect.left) - { - // move the rect right. - result.right += big_rect.left - result.left; - result.left += big_rect.left - result.left; - } +RECT keep_rect_inside_rect(const RECT& small_rect, const RECT& big_rect) { + RECT result = small_rect; + if ((result.right - result.left) > (big_rect.right - big_rect.left)) { + // small_rect is too big horizontally. resize it. + result.right = big_rect.right; + result.left = big_rect.left; + } else { + if (result.right > big_rect.right) { + // move the rect left. + result.left -= result.right-big_rect.right; + result.right -= result.right-big_rect.right; } - if ((result.bottom - result.top) > (big_rect.bottom - big_rect.top)) - { - // small_rect is too big vertically. resize it. - result.bottom = big_rect.bottom; - result.top = big_rect.top; + if (result.left < big_rect.left) { + // move the rect right. + result.right += big_rect.left-result.left; + result.left += big_rect.left-result.left; } - else - { - if (result.bottom > big_rect.bottom) - { - // move the rect up. - result.top -= result.bottom - big_rect.bottom; - result.bottom -= result.bottom - big_rect.bottom; - } + } - if (result.top < big_rect.top) - { - // move the rect down. - result.bottom += big_rect.top - result.top; - result.top += big_rect.top - result.top; - } + if ((result.bottom - result.top) > (big_rect.bottom - big_rect.top)) { + // small_rect is too big vertically. resize it. + result.bottom = big_rect.bottom; + result.top = big_rect.top; + } else { + if (result.bottom > big_rect.bottom) { + // move the rect up. + result.top -= result.bottom-big_rect.bottom; + result.bottom -= result.bottom-big_rect.bottom; } - return result; + + if (result.top < big_rect.top) { + // move the rect down. + result.bottom += big_rect.top-result.top; + result.top += big_rect.top-result.top; + } + } + return result; } -int run_message_loop() -{ - MSG msg; - while (GetMessage(&msg, NULL, 0, 0)) - { - TranslateMessage(&msg); - DispatchMessage(&msg); - } - return static_cast(msg.wParam); +int run_message_loop() { + MSG msg; + while (GetMessage(&msg, NULL, 0, 0)) { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + return static_cast(msg.wParam); } -void show_last_error_message(LPCWSTR lpszFunction, DWORD dw) -{ - // Retrieve the system error message for the error code - LPWSTR lpMsgBuf = NULL; - if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - dw, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - lpMsgBuf, - 0, - NULL) > 0) - { - // Display the error message and exit the process - LPWSTR lpDisplayBuf = (LPWSTR)LocalAlloc(LMEM_ZEROINIT, (lstrlenW(lpMsgBuf) + lstrlenW(lpszFunction) + 40) * sizeof(WCHAR)); - if (lpDisplayBuf != NULL) - { - StringCchPrintfW(lpDisplayBuf, - LocalSize(lpDisplayBuf) / sizeof(WCHAR), - L"%s failed with error %d: %s", - lpszFunction, - dw, - lpMsgBuf); - MessageBoxW(NULL, (LPCTSTR)lpDisplayBuf, L"Error", MB_OK); - LocalFree(lpDisplayBuf); - } - LocalFree(lpMsgBuf); +void show_last_error_message(LPCWSTR lpszFunction, DWORD dw) { + // Retrieve the system error message for the error code + LPWSTR lpMsgBuf = NULL; + if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + dw, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + lpMsgBuf, + 0, NULL) > 0) { + // Display the error message and exit the process + LPWSTR lpDisplayBuf = (LPWSTR)LocalAlloc(LMEM_ZEROINIT, (lstrlenW(lpMsgBuf) + lstrlenW(lpszFunction) + 40) * sizeof(WCHAR)); + if (lpDisplayBuf != NULL) { + StringCchPrintfW(lpDisplayBuf, + LocalSize(lpDisplayBuf) / sizeof(WCHAR), + L"%s failed with error %d: %s", + lpszFunction, dw, lpMsgBuf); + MessageBoxW(NULL, (LPCTSTR)lpDisplayBuf, L"Error", MB_OK); + LocalFree(lpDisplayBuf); } + LocalFree(lpMsgBuf); + } } -WindowState get_window_state(HWND hwnd) -{ - WINDOWPLACEMENT placement; - placement.length = sizeof(WINDOWPLACEMENT); +WindowState get_window_state(HWND hwnd) { + WINDOWPLACEMENT placement; + placement.length = sizeof(WINDOWPLACEMENT); - if (GetWindowPlacement(hwnd, &placement) == 0) - { - return UNKNONW; - } - - if (placement.showCmd == SW_MINIMIZE || placement.showCmd == SW_SHOWMINIMIZED || IsIconic(hwnd)) - { - return MINIMIZED; - } - - if (placement.showCmd == SW_MAXIMIZE || placement.showCmd == SW_SHOWMAXIMIZED) - { - return MAXIMIZED; - } - - auto rectp = get_window_pos(hwnd); - if (!rectp) - { - return UNKNONW; - } - - auto rect = *rectp; - MONITORINFO monitor; - monitor.cbSize = sizeof(MONITORINFO); - auto h_monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); - GetMonitorInfo(h_monitor, &monitor); - bool top_left = monitor.rcWork.top == rect.top && monitor.rcWork.left == rect.left; - bool bottom_left = monitor.rcWork.bottom == rect.bottom && monitor.rcWork.left == rect.left; - bool top_right = monitor.rcWork.top == rect.top && monitor.rcWork.right == rect.right; - bool bottom_right = monitor.rcWork.bottom == rect.bottom && monitor.rcWork.right == rect.right; - - if (top_left && bottom_left) - return SNAPED_LEFT; - if (top_left) - return SNAPED_TOP_LEFT; - if (bottom_left) - return SNAPED_BOTTOM_LEFT; - if (top_right && bottom_right) - return SNAPED_RIGHT; - if (top_right) - return SNAPED_TOP_RIGHT; - if (bottom_right) - return SNAPED_BOTTOM_RIGHT; - - return RESTORED; + if (GetWindowPlacement(hwnd, &placement) == 0) { + return UNKNONW; + } + + if (placement.showCmd == SW_MINIMIZE || placement.showCmd == SW_SHOWMINIMIZED || IsIconic(hwnd)) { + return MINIMIZED; + } + + if (placement.showCmd == SW_MAXIMIZE || placement.showCmd == SW_SHOWMAXIMIZED) { + return MAXIMIZED; + } + + auto rectp = get_window_pos(hwnd); + if (!rectp) { + return UNKNONW; + } + + auto rect = *rectp; + MONITORINFO monitor; + monitor.cbSize = sizeof(MONITORINFO); + auto h_monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); + GetMonitorInfo(h_monitor, &monitor); + bool top_left = monitor.rcWork.top == rect.top && monitor.rcWork.left == rect.left; + bool bottom_left = monitor.rcWork.bottom == rect.bottom && monitor.rcWork.left == rect.left; + bool top_right = monitor.rcWork.top == rect.top && monitor.rcWork.right == rect.right; + bool bottom_right = monitor.rcWork.bottom == rect.bottom && monitor.rcWork.right == rect.right; + + if (top_left && bottom_left) return SNAPED_LEFT; + if (top_left) return SNAPED_TOP_LEFT; + if (bottom_left) return SNAPED_BOTTOM_LEFT; + if (top_right && bottom_right) return SNAPED_RIGHT; + if (top_right) return SNAPED_TOP_RIGHT; + if (bottom_right) return SNAPED_BOTTOM_RIGHT; + + return RESTORED; } -bool is_process_elevated() -{ - HANDLE token = nullptr; - bool elevated = false; +bool is_process_elevated() { + 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 (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; -} - -bool drop_elevated_privileges() -{ - HANDLE token = nullptr; - LPCTSTR lpszPrivilege = SE_SECURITY_NAME; - 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; - DWORD size = (DWORD)sizeof(TOKEN_MANDATORY_LABEL) + ::GetLengthSid(medium_sid); - - BOOL result = SetTokenInformation(token, TokenIntegrityLevel, &label, size); - LocalFree(medium_sid); + if (token) { CloseHandle(token); + } - return result; + return elevated; } -std::wstring get_process_path(DWORD pid) noexcept -{ - auto process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, TRUE, pid); - std::wstring name; - if (process != INVALID_HANDLE_VALUE) - { - name.resize(MAX_PATH); - DWORD name_length = static_cast(name.length()); - if (QueryFullProcessImageNameW(process, 0, (LPWSTR)name.data(), &name_length) == 0) - { - name_length = 0; - } - name.resize(name_length); - CloseHandle(process); - } - return name; +bool drop_elevated_privileges() { + HANDLE token = nullptr; + LPCTSTR lpszPrivilege = SE_SECURITY_NAME; + 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; + DWORD size = (DWORD)sizeof(TOKEN_MANDATORY_LABEL) + ::GetLengthSid(medium_sid); + + BOOL result = SetTokenInformation(token, TokenIntegrityLevel, &label, size); + LocalFree(medium_sid); + CloseHandle(token); + + return result; } -bool run_elevated(const std::wstring& file, const std::wstring& params) -{ - 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; - exec_info.lpDirectory = 0; - exec_info.hInstApp = 0; - - if (ShellExecuteExW(&exec_info)) - { - return exec_info.hProcess != nullptr; - } - else - { - return false; +std::wstring get_process_path(DWORD pid) noexcept { + auto process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, TRUE, pid); + std::wstring name; + if (process != INVALID_HANDLE_VALUE) { + name.resize(MAX_PATH); + DWORD name_length = static_cast(name.length()); + if (QueryFullProcessImageNameW(process, 0, (LPWSTR)name.data(), &name_length) == 0) { + name_length = 0; } + name.resize(name_length); + CloseHandle(process); + } + return name; } -bool run_non_elevated(const std::wstring& file, const std::wstring& params) -{ - auto executable_args = file; - if (!params.empty()) - { - executable_args += L" " + params; - } +bool run_elevated(const std::wstring& file, const std::wstring& params) { + 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; + exec_info.lpDirectory = 0; + exec_info.hInstApp = 0; - HWND hwnd = GetShellWindow(); - if (!hwnd) - { - return false; - } - DWORD pid; - GetWindowThreadProcessId(hwnd, &pid); - - winrt::handle process{ OpenProcess(PROCESS_CREATE_PROCESS, FALSE, pid) }; - if (!process) - { - return false; - } - - SIZE_T size = 0; - - InitializeProcThreadAttributeList(nullptr, 1, 0, &size); - auto pproc_buffer = std::make_unique(size); - auto pptal = reinterpret_cast(pproc_buffer.get()); - - if (!InitializeProcThreadAttributeList(pptal, 1, 0, &size)) - { - return false; - } - - HANDLE process_handle = process.get(); - if (!pptal || !UpdateProcThreadAttribute(pptal, - 0, - PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, - &process_handle, - sizeof(process_handle), - nullptr, - nullptr)) - { - return false; - } - - STARTUPINFOEX siex = { 0 }; - siex.lpAttributeList = pptal; - siex.StartupInfo.cb = sizeof(siex); - - PROCESS_INFORMATION process_info = { 0 }; - auto succedded = CreateProcessW(file.c_str(), - const_cast(executable_args.c_str()), - nullptr, - nullptr, - FALSE, - EXTENDED_STARTUPINFO_PRESENT, - nullptr, - nullptr, - &siex.StartupInfo, - &process_info); - if (process_info.hProcess) - { - CloseHandle(process_info.hProcess); - } - if (process_info.hThread) - { - CloseHandle(process_info.hThread); - } - return succedded; + if (ShellExecuteExW(&exec_info)) { + return exec_info.hProcess != nullptr; + } else { + return false; + } } -bool run_same_elevation(const std::wstring& file, const std::wstring& params) -{ - auto executable_args = file; - if (!params.empty()) - { - executable_args += L" " + params; - } - STARTUPINFO si = { 0 }; - PROCESS_INFORMATION pi = { 0 }; - auto succedded = CreateProcessW(file.c_str(), - const_cast(executable_args.c_str()), - nullptr, - nullptr, - FALSE, - 0, - nullptr, - nullptr, - &si, - &pi); - if (pi.hProcess) - { - CloseHandle(pi.hProcess); - } - if (pi.hThread) - { - CloseHandle(pi.hThread); - } - return succedded; +bool run_non_elevated(const std::wstring& file, const std::wstring& params) { + auto executable_args = file; + if (!params.empty()) { + executable_args += L" " + params; + } + + HWND hwnd = GetShellWindow(); + if (!hwnd) { + return false; + } + DWORD pid; + GetWindowThreadProcessId(hwnd, &pid); + + winrt::handle process{ OpenProcess(PROCESS_CREATE_PROCESS, FALSE, pid) }; + if (!process) { + return false; + } + + SIZE_T size = 0; + + InitializeProcThreadAttributeList(nullptr, 1, 0, &size); + auto pproc_buffer = std::make_unique(size); + auto pptal = reinterpret_cast(pproc_buffer.get()); + + if (!InitializeProcThreadAttributeList(pptal, 1, 0, &size)) { + return false; + } + + HANDLE process_handle = process.get(); + if (!pptal || !UpdateProcThreadAttribute(pptal, + 0, + PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, + &process_handle, + sizeof(process_handle), + nullptr, + nullptr)) { + return false; + } + + STARTUPINFOEX siex = { 0 }; + siex.lpAttributeList = pptal; + siex.StartupInfo.cb = sizeof(siex); + + PROCESS_INFORMATION process_info = { 0 }; + auto succedded = CreateProcessW(file.c_str(), + const_cast(executable_args.c_str()), + nullptr, + nullptr, + FALSE, + EXTENDED_STARTUPINFO_PRESENT, + nullptr, + nullptr, + &siex.StartupInfo, + &process_info); + if (process_info.hProcess) { + CloseHandle(process_info.hProcess); + } + if (process_info.hThread) { + CloseHandle(process_info.hThread); + } + return succedded; } -std::wstring get_process_path(HWND window) noexcept -{ - const static std::wstring app_frame_host = L"ApplicationFrameHost.exe"; - DWORD pid{}; - GetWindowThreadProcessId(window, &pid); - auto name = get_process_path(pid); - if (name.length() >= app_frame_host.length() && - name.compare(name.length() - app_frame_host.length(), app_frame_host.length(), app_frame_host) == 0) - { - // It is a UWP app. We will enumarate the windows and look for one created - // by something with a different PID - DWORD new_pid = pid; - EnumChildWindows( - window, [](HWND hwnd, LPARAM param) -> BOOL { - auto new_pid_ptr = reinterpret_cast(param); - DWORD pid; - GetWindowThreadProcessId(hwnd, &pid); - if (pid != *new_pid_ptr) - { - *new_pid_ptr = pid; - return FALSE; - } - else - { - return TRUE; - } - }, - reinterpret_cast(&new_pid)); - // If we have a new pid, get the new name. - if (new_pid != pid) - { - return get_process_path(new_pid); - } - } - return name; +bool run_same_elevation(const std::wstring& file, const std::wstring& params) { + auto executable_args = file; + if (!params.empty()) { + executable_args += L" " + params; + } + STARTUPINFO si = { 0 }; + PROCESS_INFORMATION pi = { 0 }; + auto succedded = CreateProcessW(file.c_str(), + const_cast(executable_args.c_str()), + nullptr, + nullptr, + FALSE, + 0, + nullptr, + nullptr, + &si, + &pi); + if (pi.hProcess) { + CloseHandle(pi.hProcess); + } + if (pi.hThread) { + CloseHandle(pi.hThread); + } + return succedded; } -std::wstring get_product_version() -{ - static std::wstring version = std::to_wstring(VERSION_MAJOR) + - L"." + std::to_wstring(VERSION_MINOR) + - L"." + std::to_wstring(VERSION_REVISION) + - L"." + std::to_wstring(VERSION_BUILD); - return version; +std::wstring get_process_path(HWND window) noexcept { + const static std::wstring app_frame_host = L"ApplicationFrameHost.exe"; + DWORD pid{}; + GetWindowThreadProcessId(window, &pid); + auto name = get_process_path(pid); + if (name.length() >= app_frame_host.length() && + name.compare(name.length() - app_frame_host.length(), app_frame_host.length(), app_frame_host) == 0) { + // It is a UWP app. We will enumarate the windows and look for one created + // by something with a different PID + DWORD new_pid = pid; + EnumChildWindows(window, [](HWND hwnd, LPARAM param) -> BOOL { + auto new_pid_ptr = reinterpret_cast(param); + DWORD pid; + GetWindowThreadProcessId(hwnd, &pid); + if (pid != *new_pid_ptr) { + *new_pid_ptr = pid; + return FALSE; + } else { + return TRUE; + } + }, reinterpret_cast(&new_pid)); + // If we have a new pid, get the new name. + if (new_pid != pid) { + return get_process_path(new_pid); + } + } + return name; } -std::wstring get_resource_string(UINT resource_id, HINSTANCE instance, const wchar_t* fallback) -{ - wchar_t* text_ptr; - auto length = LoadStringW(instance, resource_id, reinterpret_cast(&text_ptr), 0); - if (length == 0) - { - return fallback; - } - else - { - return { text_ptr, static_cast(length) }; - } +std::wstring get_product_version() { + static std::wstring version = std::to_wstring(VERSION_MAJOR) + + L"." + std::to_wstring(VERSION_MINOR) + + L"." + std::to_wstring(VERSION_REVISION) + + L"." + std::to_wstring(VERSION_BUILD); + + return version; +} + +std::wstring get_resource_string(UINT resource_id, HINSTANCE instance, const wchar_t* fallback) { + wchar_t* text_ptr; + auto length = LoadStringW(instance, resource_id, reinterpret_cast(&text_ptr), 0); + if (length == 0) { + return fallback; + } else { + return { text_ptr, static_cast(length) }; + } } std::wstring get_module_filename(HMODULE mod) From 653a84d3a990106c72d5f20f973b846d4919aaf3 Mon Sep 17 00:00:00 2001 From: Alekhya Kommuru Date: Fri, 17 Jan 2020 14:38:41 -0800 Subject: [PATCH 14/15] reverting formatting of files --- src/common/common.h | 30 +++++++------- src/modules/powerrename/lib/Settings.cpp | 52 ++++++++++-------------- 2 files changed, 35 insertions(+), 47 deletions(-) diff --git a/src/common/common.h b/src/common/common.h index 5e17572a29..5fdcc9d451 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -14,10 +14,9 @@ std::optional get_mouse_pos(); // Gets active window, filtering out all "non standard" windows like the taskbar, etc. HWND get_filtered_active_window(); // Gets window ancestor (usualy the window we want to do stuff with), filtering out all "non standard" windows like the taskbar, etc. and provide the app process path -struct WindowAndProcPath -{ - HWND hwnd = nullptr; - std::wstring process_path; +struct WindowAndProcPath { + HWND hwnd = nullptr; + std::wstring process_path; }; WindowAndProcPath get_filtered_base_window_and_path(HWND window); @@ -33,18 +32,17 @@ int run_message_loop(); void show_last_error_message(LPCWSTR lpszFunction, DWORD dw); -enum WindowState -{ - UNKNONW, - MINIMIZED, - MAXIMIZED, - SNAPED_TOP_LEFT, - SNAPED_LEFT, - SNAPED_BOTTOM_LEFT, - SNAPED_TOP_RIGHT, - SNAPED_RIGHT, - SNAPED_BOTTOM_RIGHT, - RESTORED +enum WindowState { + UNKNONW, + MINIMIZED, + MAXIMIZED, + SNAPED_TOP_LEFT, + SNAPED_LEFT, + SNAPED_BOTTOM_LEFT, + SNAPED_TOP_RIGHT, + SNAPED_RIGHT, + SNAPED_BOTTOM_RIGHT, + RESTORED }; WindowState get_window_state(HWND hwnd); diff --git a/src/modules/powerrename/lib/Settings.cpp b/src/modules/powerrename/lib/Settings.cpp index bc5bfebaea..230cd2f2f8 100644 --- a/src/modules/powerrename/lib/Settings.cpp +++ b/src/modules/powerrename/lib/Settings.cpp @@ -1,10 +1,8 @@ - #include "stdafx.h" #include #include "Settings.h" #include "PowerRenameInterfaces.h" -// Note: Not moving these strings to the resource file as these are internal and used as IDs const wchar_t c_rootRegPath[] = L"Software\\Microsoft\\PowerRename"; const wchar_t c_mruSearchRegPath[] = L"SearchMRU"; const wchar_t c_mruReplaceRegPath[] = L"ReplaceMRU"; @@ -167,15 +165,15 @@ bool CSettings::GetRegStringValue(_In_ PCWSTR valueName, __out_ecount(cchBuf) PW return (SUCCEEDED(HRESULT_FROM_WIN32(SHGetValue(HKEY_CURRENT_USER, c_rootRegPath, valueName, &type, value, &cb) == ERROR_SUCCESS))); } -typedef int(CALLBACK* MRUCMPPROC)(LPCWSTR, LPCWSTR); -typedef struct -{ - DWORD cbSize; - UINT uMax; - UINT fFlags; - HKEY hKey; - LPCTSTR lpszSubKey; +typedef int (CALLBACK* MRUCMPPROC)(LPCWSTR, LPCWSTR); + +typedef struct { + DWORD cbSize; + UINT uMax; + UINT fFlags; + HKEY hKey; + LPCTSTR lpszSubKey; MRUCMPPROC lpfnCompare; } MRUINFO; @@ -190,21 +188,15 @@ class CRenameMRU : { public: // IUnknown - IFACEMETHODIMP_(ULONG) - AddRef(); - IFACEMETHODIMP_(ULONG) - Release(); + IFACEMETHODIMP_(ULONG) AddRef(); + IFACEMETHODIMP_(ULONG) Release(); IFACEMETHODIMP QueryInterface(_In_ REFIID riid, _Outptr_ void** ppv); // IEnumString IFACEMETHODIMP Next(__in ULONG celt, __out_ecount_part(celt, *pceltFetched) LPOLESTR* rgelt, __out_opt ULONG* pceltFetched); IFACEMETHODIMP Skip(__in ULONG) { return E_NOTIMPL; } IFACEMETHODIMP Reset(); - IFACEMETHODIMP Clone(__deref_out IEnumString** ppenum) - { - *ppenum = nullptr; - return E_NOTIMPL; - } + IFACEMETHODIMP Clone(__deref_out IEnumString** ppenum) { *ppenum = nullptr; return E_NOTIMPL; } // IPowerRenameMRU IFACEMETHODIMP AddMRUString(_In_ PCWSTR entry); @@ -221,20 +213,19 @@ private: int _EnumMRUList(_In_ int nItem, _Out_ void* lpData, _In_ UINT uLen); void _FreeMRUList(); - long m_refCount = 0; - HKEY m_hKey = NULL; - ULONG m_maxMRUSize = 0; - ULONG m_mruIndex = 0; - ULONG m_mruSize = 0; + long m_refCount = 0; + HKEY m_hKey = NULL; + ULONG m_maxMRUSize = 0; + ULONG m_mruIndex = 0; + ULONG m_mruSize = 0; HANDLE m_mruHandle = NULL; HMODULE m_hComctl32Dll = NULL; - PWSTR m_regPath = nullptr; + PWSTR m_regPath = nullptr; }; CRenameMRU::CRenameMRU() : m_refCount(1) -{ -} +{} CRenameMRU::~CRenameMRU() { @@ -277,14 +268,12 @@ HRESULT CRenameMRU::CreateInstance(_In_ PCWSTR regPathMRU, _In_ ULONG maxMRUSize } // IUnknown -IFACEMETHODIMP_(ULONG) -CRenameMRU::AddRef() +IFACEMETHODIMP_(ULONG) CRenameMRU::AddRef() { return InterlockedIncrement(&m_refCount); } -IFACEMETHODIMP_(ULONG) -CRenameMRU::Release() +IFACEMETHODIMP_(ULONG) CRenameMRU::Release() { long refCount = InterlockedDecrement(&m_refCount); @@ -475,6 +464,7 @@ void CRenameMRU::_FreeMRUList() { pfnFreeMRUList(m_mruHandle); } + } m_mruHandle = NULL; } From 9c743acd2d88a0832bd6602fd2fd4c0c7625ae49 Mon Sep 17 00:00:00 2001 From: Alekhya Kommuru Date: Mon, 20 Jan 2020 11:52:46 -0800 Subject: [PATCH 15/15] Removed some IDs from resource file. Changed SHIFT to Shift --- src/modules/powerrename/dll/PowerRenameExt.rc | 8 +------ src/modules/powerrename/dll/dllmain.cpp | 22 +++++++++---------- src/modules/powerrename/dll/resource.h | 18 +++++---------- 3 files changed, 18 insertions(+), 30 deletions(-) diff --git a/src/modules/powerrename/dll/PowerRenameExt.rc b/src/modules/powerrename/dll/PowerRenameExt.rc index e85b27e058..57c3116be9 100644 --- a/src/modules/powerrename/dll/PowerRenameExt.rc +++ b/src/modules/powerrename/dll/PowerRenameExt.rc @@ -93,18 +93,12 @@ BEGIN IDS_POWERRENAME "PowerRename" IDS_SETTINGS_DESCRIPTION L"A Windows Shell Extension for more advanced bulk renaming using search and replace or regular expressions." - IDS_SETTINGS_ICON L"pt-power-rename" IDS_OVERVIEW_LINK L"https://github.com/microsoft/PowerToys/tree/master/src/modules/powerrename" - IDS_BOOL_PERSIST L"bool_persist_input" IDS_RESTORE_SEARCH L"Restore search, replace and flags values on launch from previous run." - IDS_MRU_ENABLED L"bool_mru_enabled" IDS_ENABLE_AUTO L"Enable autocomplete and autosuggest of recently used inputs for search and replace values." - IDS_MAX_MRU_SIZE L"int_max_mru_size" IDS_MAX_ITEMS L"Maximum number of items to show in recently used list for autocomplete dropdown." - IDS_SHOW_ICON L"bool_show_icon_on_menu" IDS_ICON_CONTEXT_MENU L"Show icon on context menu." - IDS_EXTENDED_MENU L"bool_show_extended_menu" L"Only show the PowerRename menu item on the extended context menu (SHIFT + Right-click)." - IDS_EXTENDED_MENU_INFO L"Only show the PowerRename menu item on the extended context menu (SHIFT + Right-click)." + IDS_EXTENDED_MENU_INFO L"Only show the PowerRename menu item on the extended context menu (Shift + Right-click)." END #endif // English (United States) resources diff --git a/src/modules/powerrename/dll/dllmain.cpp b/src/modules/powerrename/dll/dllmain.cpp index 0bfa4459af..14f37fd56e 100644 --- a/src/modules/powerrename/dll/dllmain.cpp +++ b/src/modules/powerrename/dll/dllmain.cpp @@ -206,23 +206,23 @@ public: // Create a Settings object. PowerToysSettings::Settings settings(hinstance, get_name()); settings.set_description(GET_RESOURCE_STRING(IDS_SETTINGS_DESCRIPTION)); - settings.set_icon_key(GET_RESOURCE_STRING(IDS_SETTINGS_ICON)); + settings.set_icon_key(L"pt-power-rename"); // Link to the GitHub PowerRename sub-page settings.set_overview_link(GET_RESOURCE_STRING(IDS_OVERVIEW_LINK)); settings.add_bool_toogle( - GET_RESOURCE_STRING(IDS_BOOL_PERSIST), + L"bool_persist_input", GET_RESOURCE_STRING(IDS_RESTORE_SEARCH), CSettings::GetPersistState()); settings.add_bool_toogle( - GET_RESOURCE_STRING(IDS_MRU_ENABLED), + L"bool_mru_enabled", GET_RESOURCE_STRING(IDS_ENABLE_AUTO), CSettings::GetMRUEnabled()); settings.add_int_spinner( - GET_RESOURCE_STRING(IDS_MAX_MRU_SIZE), + L"int_max_mru_size", GET_RESOURCE_STRING(IDS_MAX_ITEMS), CSettings::GetMaxMRUSize(), 0, @@ -230,12 +230,12 @@ public: 1); settings.add_bool_toogle( - GET_RESOURCE_STRING(IDS_SHOW_ICON), + L"bool_show_icon_on_menu", GET_RESOURCE_STRING(IDS_ICON_CONTEXT_MENU), CSettings::GetShowIconOnMenu()); settings.add_bool_toogle( - GET_RESOURCE_STRING(IDS_EXTENDED_MENU), + L"bool_show_extended_menu", GET_RESOURCE_STRING(IDS_EXTENDED_MENU_INFO), CSettings::GetExtendedContextMenuOnly()); @@ -252,11 +252,11 @@ public: PowerToysSettings::PowerToyValues values = PowerToysSettings::PowerToyValues::from_json_string(config); - CSettings::SetPersistState(values.get_bool_value(GET_RESOURCE_STRING(IDS_BOOL_PERSIST)).value()); - CSettings::SetMRUEnabled(values.get_bool_value(GET_RESOURCE_STRING(IDS_MRU_ENABLED)).value()); - CSettings::SetMaxMRUSize(values.get_int_value(GET_RESOURCE_STRING(IDS_MAX_MRU_SIZE)).value()); - CSettings::SetShowIconOnMenu(values.get_bool_value(GET_RESOURCE_STRING(IDS_SHOW_ICON)).value()); - CSettings::SetExtendedContextMenuOnly(values.get_bool_value(GET_RESOURCE_STRING(IDS_EXTENDED_MENU)).value()); + CSettings::SetPersistState(values.get_bool_value(L"bool_persist_input").value()); + CSettings::SetMRUEnabled(values.get_bool_value(L"bool_mru_enabled").value()); + CSettings::SetMaxMRUSize(values.get_int_value(L"int_max_mru_size").value()); + CSettings::SetShowIconOnMenu(values.get_bool_value(L"bool_show_icon_on_menu").value()); + CSettings::SetExtendedContextMenuOnly(values.get_bool_value(L"bool_show_extended_menu").value()); } catch (std::exception) { diff --git a/src/modules/powerrename/dll/resource.h b/src/modules/powerrename/dll/resource.h index 6d2c78ffb3..cf00d79cce 100644 --- a/src/modules/powerrename/dll/resource.h +++ b/src/modules/powerrename/dll/resource.h @@ -1,18 +1,12 @@ #define IDS_POWERRENAME 801 #define IDI_RENAME 132 #define IDS_SETTINGS_DESCRIPTION 2101 -#define IDS_SETTINGS_ICON 2102 -#define IDS_OVERVIEW_LINK 2103 -#define IDS_BOOL_PERSIST 2104 -#define IDS_RESTORE_SEARCH 2105 -#define IDS_MRU_ENABLED 2106 -#define IDS_ENABLE_AUTO 2107 -#define IDS_MAX_MRU_SIZE 2108 -#define IDS_MAX_ITEMS 2109 -#define IDS_SHOW_ICON 2110 -#define IDS_ICON_CONTEXT_MENU 2111 -#define IDS_EXTENDED_MENU 2112 -#define IDS_EXTENDED_MENU_INFO 2113 +#define IDS_OVERVIEW_LINK 2102 +#define IDS_RESTORE_SEARCH 2103 +#define IDS_ENABLE_AUTO 2104 +#define IDS_MAX_ITEMS 2105 +#define IDS_ICON_CONTEXT_MENU 2106 +#define IDS_EXTENDED_MENU_INFO 2107 // Next default values for new objects