From 969abe015c37b76321b7a21344a835d52c89e621 Mon Sep 17 00:00:00 2001 From: Alekhya Kommuru Date: Tue, 14 Jan 2020 15:00:05 -0800 Subject: [PATCH] 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()