gpt4 book ai didi

c++ - LoadString() 方法在 C++ 中不起作用

转载 作者:行者123 更新时间:2023-11-28 02:58:50 25 4
gpt4 key购买 nike

我正在尝试从我正在处理的 DLL 文件中的 Strin 表加载一个字符串。这是应该将字符串加载到 std::wstring 中的函数(因为我的项目使用 Unicode 字符集)。

void ErrorHandler::load_error_string()
{
m_hInst = AfxGetInstanceHandle();
wchar_t buffer[1024] = { '\0' };
std::size_t string_length = LoadStringW(this->m_hInst, this->m_error_id, buffer, 1024);

this->m_raw_content = std::wstring(buffer, string_length);

CStringW output;
output.Format(L"%d", m_raw_content.length());

AfxMessageBox(output);
}

我已经创建了最后三行来诊断该方法。 AfxMessageBox() 的输出是 0

我哪里错了?

最佳答案

AfxGetInstanceHandle() 为您提供正在运行的可执行文件HINSTANCE。这意味着您的 LoadStringW 调用将在 exe 的资源表中查找您的字符串,这将失败,因为字符串在您的 DLL 中。

相反,您需要获取 DLL 本身HINSTANCE - 这是作为 DllMain() 的第一个参数提供的在你的 DLL 中。

请参阅此答案以获取示例: https://stackoverflow.com/a/2396380/1073843

编辑:如果您使用的是 MFC DLL,那么您可能只需要在 DLL 的任何入口点顶部添加对 AFX_MANAGE_STATE(AfxGetStaticModuleState()); 的调用(在 AfxGetInstanceHandle 之前() 被调用。)

关于c++ - LoadString() 方法在 C++ 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21382464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com