Reputation: 42710
I have a MFC exe, trying to dynamic load a MFC dll.
// This is code in MFC exe
HINSTANCE h = AfxLoadLibrary(_T("DLL.dll"));
typedef void(*FUN)();
FUN fun = (FUN)GetProcAddress(h, "loveme");
FreeLibrary(h);
Both MFC exe and MFC dll, are having their own resource file.
However, I realize that, if MFC exe and MFC dll are having a same resource ID, conflict may occur.
// This is code in MFC dll. Both exe and dll, are having resources with
// ID 101.
CString s;
s.LoadString(101);
// Resource 101 in exe is being shown :(
AfxMessageBox(s);
May I know how I can avoid resource ID conflict problem? Can we have two resource in both MFC and DLL, although their ID is different, but they are independent from each other?
This means, DLL will only load DLL's resource. EXE will only load EXE's resource.
Upvotes: 1
Views: 1131
Reputation: 3208
You will need to keep track handle to yourself, which will be passed in during dllmain.
HINSTANCE hDLLInstance = 0;
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
hDLLInstance = hInstance;
...
}
then when you want to refer to local resources (ie. LoadString), pass the dll handle
...
CString s;
s.LoadString(hDLLInstance, 101);
AfxMessageBox(s);
...
Upvotes: 3
Reputation: 52159
Try using AfxGetInstanceHandle()
in the MFC DLL to get an HINSTANCE
to the DLL. Then pass it to CString::LoadString()
:
/* Code in MFC DLL. */
CString s;
// Load resource 101 in the DLL.
s.LoadString(AfxGetInstanceHandle(), 101);
AfxMessageBox(s);
Upvotes: 0