added the helper functions

This commit is contained in:
Alekhya Kommuru
2020-01-14 15:00:05 -08:00
parent 5e30721e01
commit 969abe015c
3 changed files with 42 additions and 3 deletions

View File

@@ -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<wchar_t*>(&text_ptr), 0);
std::wstring res_string = { *reinterpret_cast<wchar_t**>(&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) std::wstring get_module_filename(HMODULE mod)
{ {
wchar_t buffer[MAX_PATH + 1]; wchar_t buffer[MAX_PATH + 1];

View File

@@ -77,4 +77,16 @@ std::wstring get_resource_string(UINT resource_id, HINSTANCE instance, const wch
// Requires that // Requires that
// extern "C" IMAGE_DOS_HEADER __ImageBase; // extern "C" IMAGE_DOS_HEADER __ImageBase;
// is added to the .cpp file. // is added to the .cpp file.
#define GET_RESOURCE_STRING(resource_id) get_resource_string(resource_id, reinterpret_cast<HINSTANCE>(&__ImageBase), L#resource_id) #define GET_RESOURCE_STRING(resource_id) get_resource_string(resource_id, reinterpret_cast<HINSTANCE>(&__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<HINSTANCE>(&__ImageBase))

View File

@@ -4,7 +4,8 @@
#include <settings.h> #include <settings.h>
#include <trace.h> #include <trace.h>
#include <common/settings_objects.h> #include <common/settings_objects.h>
#include <common/common.h>
#include "resource.h"
#include <atomic> #include <atomic>
std::atomic<DWORD> g_dwModuleRefCount = 0; std::atomic<DWORD> g_dwModuleRefCount = 0;
@@ -160,12 +161,14 @@ class PowerRenameModule : public PowertoyModuleIface
private: private:
// Enabled by default // Enabled by default
bool m_enabled = true; bool m_enabled = true;
wchar_t* app_name;
public: public:
// Return the display name of the powertoy, this will be cached // Return the display name of the powertoy, this will be cached
virtual PCWSTR get_name() override virtual PCWSTR get_name() override
{ {
return L"PowerRename"; app_name = GET_RES_STRING_WCHAR(IDS_POWERRENAME);
return app_name;
} }
// Enable the powertoy // Enable the powertoy
@@ -299,6 +302,11 @@ public:
{ {
init_settings(); init_settings();
} }
~PowerRenameModule()
{
delete app_name;
}
}; };
extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create() extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create()