Loading Win32 resource file in wstringstream format
This is a continuation of the question asked and the answer to here . I want to use a text file as a resource and then load it like stringstream
so I can parse it.
The following code shows what I have:
std::string filename("resource.txt");
HRSRC hrsrc = FindResource(GetModuleHandle(NULL), filename.c_str(), RT_RCDATA);
HGLOBAL res = LoadResource(GetModuleHandle(NULL), hrsrc);
LPBYTE data = (LPBYTE)LockResource(res);
std::stringstream stream((LPSTR)data);
However, I'm not sure how to expand this to read a unicode text file using wstringstream
. A naive approach gives unreadable characters:
...
LPBYTE data = (LPBYTE)LockResource(res);
std::wstringstream wstream((LPWSTR)data);
Since LPBYTE
- is nothing more than a CHAR*
, it should come as no surprise that this won't work, but the naive conversion of the resource to WCHAR*
( LPWSTR
) doesn't work either:
...
LPWSTR data = (LPWSTR)LockResource(res);
std::wstringstream wstream(data);
I assume this is because it WCHAR
is 16-bit instead of 8-bit like CHAR
, but I'm not sure how to get around this.
Thanks for any help!
Your comment will share the missing key. The file you compiled into a resource is encoded as UTF-8
. So the obvious options are:
- Using the code in your question, get a pointer to a resource encoded as UTF-8 and pass this one
MultiByteToWideChar
for conversion to UTF-16. Then you can enterwstring
. - Convert the file you are compiling to a resource to UTF-16 before compiling the resource. Then the code in your question will work.