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)
{
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
// 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<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))