简体   繁体   English

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

[英]LoadString() method doesn't work in C++

I am trying to load a string from my Strin Table in the DLL file I am working on. 我正在尝试从我正在处理的DLL文件中的Strin表中加载一个字符串。 Here is the function that is supposed to load the string into a std::wstring (since my project uses Unicode charset). 这是应该将字符串加载到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);
}

I have created the last three lines for diagnosing the method. 我创建了最后三行来诊断方法。 The output of AfxMessageBox() is 0 . AfxMessageBox()的输出为0

Where am I wrong? 我哪里错了?

AfxGetInstanceHandle() gives you the HINSTANCE of the running executable . AfxGetInstanceHandle()为您提供正在运行的可执行文件HINSTANCE This means that your LoadStringW call will be looking in the exe's resource table for your string, which will fail, as the strings are in your DLL. 这意味着你的LoadStringW调用将在exe的资源表中查找你的字符串,这将失败,因为字符串在你的DLL中。

Instead, you'll need to grab the HINSTANCE of the DLL itself - this is provided as the first parameter to DllMain() in your DLL. 相反,您需要获取DLL本身HINSTANCE - 这是作为DLL中DllMain()的第一个参数提供的。

See this answer for an example: https://stackoverflow.com/a/2396380/1073843 请参阅此答案以获取示例: https//stackoverflow.com/a/2396380/1073843

EDIT : If you're using an MFC DLL, then it's possible you just need to add a call to AFX_MANAGE_STATE(AfxGetStaticModuleState()); 编辑 :如果您正在使用MFC DLL,那么您可能只需要添加对AFX_MANAGE_STATE(AfxGetStaticModuleState());的调用AFX_MANAGE_STATE(AfxGetStaticModuleState()); at the top of any entry points into your DLL (before AfxGetInstanceHandle() is called.) 在你的DLL的任何入口点的顶部(在AfxGetInstanceHandle()之前。)

看看这个问题 ,它将告诉你如何获得你的DLL的HINSTANCE ,如果它是一个MFC DLL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM