简体   繁体   English

如何从不同进程的资源中加载字符串?

[英]How to load a string from the resource of a different process?

I need to load a string which is placed in the resource dll of a different process, provided that the process will be running at the time of call. 我需要加载一个字符串,该字符串放在另一个进程的资源dll中,条件是该进程将在调用时运行。

I tried following code - 我尝试了以下代码-

    HMODULE hRes = ::LoadLibrary(_T("SomeResource.dll"));

    TCHAR buffer[50];
    ::LoadString(hRes, IDS_SOME_ID, buffer, 50);

This code is working fine while running in debug mode. 在调试模式下运行时,此代码可以正常工作。 But in release mode LoadLibrary returns zero. 但是在释放模式下,LoadLibrary返回零。 Why? 为什么?

Am I missing something? 我想念什么吗? Please help me. 请帮我。

I am using VC7.1 compiler. 我正在使用VC7.1编译器。

It might be a problem of finding "SomeResource.dll" . 查找 "SomeResource.dll"可能是一个问题。 When you run from the debugger, the executable is started from the project's path. 从调试器运行时,可执行文件从项目路径启动。 If the DLL can be found from there. 如果可以从那里找到DLL。 it's fine. 没关系。 When you run from outside the IDE, the executable is started from a different folder. 当您从IDE外部运行时,可执行文件将从其他文件夹启动。 It migh be that the DLL cannot be found from there. 可能是无法从那里找到DLL。

I'm not pretended on answer, but could please add following code to diagnose problem: 我没有假装回答,但是可以添加以下代码来诊断问题:

if( hRes == 0 ){
LPVOID lpMsgBuf;
DWORD dw = GetLastError();

FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER | 
    FORMAT_MESSAGE_FROM_SYSTEM,
    NULL,
    dw,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    (LPTSTR) &lpMsgBuf,
    0, NULL );


MessageBox(NULL, (LPTSTR)lpMsgBuf, "Error", MB_OK);

LocalFree(lpMsgBuf);
}

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

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