c++ - 为什么 LoadString 从资源中返回连接的字符串?

标签 c++ string visual-studio winapi resources

MS Visual Studio 2015 企业版; C++。

在我的控制台项目中,我添加了 RC 文件(即资源)并向其中添加了两个字符串:

enter image description here

我写了这样的函数来获取字符串:

PCTSTR LoadString(HMODULE h, DWORD id) {
    h = NULL == h ? GetModuleHandle(NULL) : h;
    PTSTR copyright = NULL;
    LoadString(h, id, (PTSTR)&copyright, 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)&copyright, 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/

相关文章:

c++ - 迭代器的麻烦

c++ - 关键字 "and"、 "or"和 "not"未定义

r - 从向量中的每个字符串中提取前 N 个字符

javascript - 将字符串转换为 Int Javascript

c# - 如何在没有 Build "Errors"的情况下将 Angular-CLI 与 Visual Studio 2017 一起使用

c++ - 类包含与类继承

c++ - std::u32string 与 std::string 和 std::u16string 之间的转换

python - 将具有相似(但略有不同)子字符串的字符串重新映射到相同的结果

visual-studio - 在 Visual Studio 中运行 dotnet 命令行

vb.net - 如何在VB.net中指定相对文件路径