我有一个MFC exe,尝试动态加载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);
MFC exe和MFC dll都有自己的资源文件。
但是,我意识到,如果MFC exe和MFC dll具有相同的资源ID,可能会发生冲突。
// 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);
我可以知道如何避免资源ID冲突问题吗?我们可以在MFC和DLL中有两个资源,虽然它们的ID不同,但是它们彼此独立吗?
这意味着,DLL只会加载DLL的资源。 EXE只会加载EXE的资源。
答案 0 :(得分:3)
您需要为自己保留跟踪句柄,这将在dllmain期间传递。
HINSTANCE hDLLInstance = 0;
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
hDLLInstance = hInstance;
...
}
然后当你想引用本地资源(即LoadString)时,传递dll句柄
...
CString s;
s.LoadString(hDLLInstance, 101);
AfxMessageBox(s);
...
答案 1 :(得分:0)
尝试在MFC DLL中使用AfxGetInstanceHandle()
来获取DLL的HINSTANCE
。然后将其传递给CString::LoadString()
:
/* Code in MFC DLL. */
CString s;
// Load resource 101 in the DLL.
s.LoadString(AfxGetInstanceHandle(), 101);
AfxMessageBox(s);