views:

44

answers:

2

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.

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. Why?

Am I missing something? Please help me.

I am using VC7.1 compiler.

A: 

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);
}
Dewfy
`szBuf` is unused.
pascal
Why make `lpMsgBuf` a `LPVOID` just to cast it to `LPTSTR` everywhere you use it? Why not make it `LPTSTR` right from the start?
sbi
@pascal - thanks, just removed
Dewfy
@sbi - I have provided this code just for diagnostic. Could you try and tell us whats wrong with DLL, to let us answer you question
Dewfy
@Dewfy: It wasn't my question.- Anyway, that's solved now.
sbi
+1  A: 

It might be a problem of finding "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. it's fine. When you run from outside the IDE, the executable is started from a different folder. It migh be that the DLL cannot be found from there.

sbi