MS Visual Studio 2015 企业版; C++。
在我的控制台项目中,我添加了 RC 文件(即资源)并向其中添加了两个字符串:
我写了这样的函数来获取字符串:
PCTSTR LoadString(HMODULE h, DWORD id) {
h = NULL == h ? GetModuleHandle(NULL) : h;
PTSTR copyright = NULL;
LoadString(h, id, (PTSTR)©right, 0); // This is WinAPI function
return copyright;
}
现在我尝试在我的代码中使用它:
// Set necessary locale
PCTSTR lang = LoadString(NULL, IDS_STRING_LOCALE);
_wsetlocale(LC_ALL, lang);
// Print copyright info
PCTSTR copyright = LoadString(NULL, IDS_STRING_COPYRIGHT);
if (NULL != copyright) {
// TODO: unexpected string output is here!
wcout << copyright << endl;
}
else {
DWORD errorCode = GetLastError();
lang
变量具有正确的值:Russian_Russia.1251
,但 copyright
变量具有Hello, World!Russian_Russia。 1251
而不是 Hello World!
。
为什么会这样?
最佳答案
PTSTR copyright = NULL; LoadString(h, id, (PTSTR)©right, 0); // This is WinAPI function
您正在调用 Win32 LoadString()
函数传递 0
作为最后一个参数的值。正如您可以从 MSDN documentation of LoadString()
中读到的那样,当最后一个参数为0时:
[...] If this parameter is 0, then lpBuffer receives a read-only pointer to the resource itself.
因此,从 LoadString()
成功返回, copyright
代码中的指针由 LoadString()
设置指向字符串资源本身。
但是,字符串资源默认不以NUL终止;它们以长度为前缀。
因此,由于您的字符串未以 NUL 结尾,因此当您使用 wcout
打印它时, 版权字符串后面的字符也会被打印出来。所以你得到了整个 Hello, World!Russian_Russia.1251
字符串。
要解决这个问题,您可以构建一个 std::wstring
,而不是使用原始的类 C 字符串指针。从资源加载的字符串中,并将其与 wcout
一起使用.请注意,资源字符串的长度是 LoadString()
的返回值。 .
For example:
std::wstring LoadStringFromResource(
_In_ UINT stringID,
_In_opt_ HINSTANCE instance = nullptr )
{
WCHAR * pBuf = nullptr;
int len = LoadStringW(
instance,
stringID,
reinterpret_cast< LPWSTR >( &pBuf ),
0 );
if( len )
return std::wstring( pBuf, len );
else
return std::wstring();
}
或者您可以使用 CString
来自 ATL/MFC 的类及其方便的方法 ( CString::LoadString()
overloads ) 或 proper constructor overload从资源中加载字符串。
在这种情况下,LoadString()
的复杂性调用隐藏在引擎盖下,在 CString
的执行下.
关于c++ - 为什么 LoadString 从资源中返回连接的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33264083/