我想從.dll字符串表讀取utf-8測試。 像這樣LPWSTR to wstring C++
LPWSTR nnW;
LoadStringW(hMod, id, nnW, MAX_PATH);
之後,我想轉換LPWSTR nnW
到std::wstring nnWstring
。我試過這種方式: LPWSTR nnW; LoadStringW(hMod,id,nnW,MAX_PATH);
const int length = MultiByteToWideChar(CP_UTF8,
0, // no flags required
(LPCSTR)nnW,
-1, // automatically determine length
NULL,
0);
std::wstring nnWstring(length, L'\0');
if (!MultiByteToWideChar(CP_UTF8,
0,
(LPCSTR)nnW,
-1,
&nnWstring[0],
length))
MessageBoxW(NULL, (LPCWSTR)nnWstring.c_str(), L"wstring", MB_OK | MB_ICONERROR);
之後在MessageBoxW中只顯示第一個字母。
你試過了什麼?有一個非常明顯的解決方案。你可能會感興趣的是你知道它和'char *'到'std :: string'是一樣的,因爲它們實際上都是'std :: basic_string'和不同的預定義的'CharT'。 –
chris
我更新了我的代碼。 –
你爲什麼叫'MultiByteToWideChar'? 'LoadStringW'和'wstring'都使用寬字符。 –