- 1 year ago
How to read STRING TABLE resources for given language into map
To read STRING TABLE resources for a given language into a map in a Windows application, you can use the WinAPI functions such as LoadString
and EnumResourceLanguages
to retrieve the resource strings and their corresponding IDs. Here's an example in C++:
cpp#include <iostream>
#include <Windows.h>
#include <map>
std::map<int, std::wstring> ReadStringTable(int languageId)
{
std::map<int, std::wstring> stringTable;
HMODULE hModule = ::GetModuleHandle(nullptr); // Get the handle to the current module
HRSRC hResource = ::FindResourceEx(hModule, RT_STRING, MAKEINTRESOURCE(1), languageId);
if (hResource != nullptr)
{
HGLOBAL hGlobal = ::LoadResource(hModule, hResource);
if (hGlobal != nullptr)
{
LPWSTR pResource = static_cast<LPWSTR>(::LockResource(hGlobal));
for (int stringId = 1; stringId <= 16; ++stringId) // Assuming there are 16 string entries
{
WCHAR buffer[256];
int length = ::LoadString(hModule, stringId, buffer, sizeof(buffer) / sizeof(buffer[0]));
if (length > 0)
{
stringTable[stringId] = buffer;
}
}
}
}
return stringTable;
}
int main()
{
int languageId = MAKELANGID(LANG_FRENCH, SUBLANG_DEFAULT); // Replace with the desired language identifier
std::map<int, std::wstring> stringTable = ReadStringTable(languageId);
// Print the string table
for (const auto& entry : stringTable)
{
std::wcout << L"ID: " << entry.first << L", Value: " << entry.second << std::endl;
}
return 0;
}
In this example, the ReadStringTable
function reads the string resources from the STRING TABLE resource of the given languageId
into a std::map<int, std::wstring>
. It uses WinAPI functions such as FindResourceEx
, LoadResource
, LockResource
, and LoadString
to retrieve and load the string resources.
In the main
function, you can specify the desired languageId
using the MAKELANGID
macro. For example, MAKELANGID(LANG_FRENCH, SUBLANG_DEFAULT)
represents the French language.
The resulting stringTable
map contains the string resource IDs as keys and the corresponding string values as values. You can modify the code to suit your specific requirements, such as adjusting the range of string IDs or using a different container for storing the strings.
Note that this code snippet is specific to C++ and the WinAPI. If you are using a different programming language, you will need to use the corresponding language bindings or libraries to interact with the WinAPI functions.