'How to get a pointer to AFX_MODULE_STATE of a DLL?
Our MFC-project is split into an exe and several dlls, which may also contain MFC-dialogs. For loading such a dialog, the dll contains an exported dll function. We have troubles loading the right resources for the dialog.
At the moment, the function looks like this:
extern "C" __declspec( dllexport ) int ShowDialog()
{
HINSTANCE hInstOld = AfxGetResourceHandle();
AfxSetResourceHandle(DXFDECDLL.hResource);
CMyDllDialog dlg;
AfxSetResourceHandle(hInstOld);
dlg.DoModal();
return 1;
}
It is working (most of the time), but seems to be bad. The function AfxSetResourceHandle sets the resource handle of the current AFX_MODULE_STATE and this module state is the one of the exe. If some other thread tries to load a string from the resource in the meantime, it will fail.
I think, we have to change the active module state, but I don't know how.
I have read the following article, but its guildlines fail. https://docs.microsoft.com/en-us/cpp/mfc/tn058-mfc-module-state-implementation?view=msvc-170
- The AFX_MODULE_STATE does not seem to be changed automatically, if I load the DLL with
LoadLibraryand calling the method via a function pointer. Do I need to edit the automatically generatedDLLMain? - If I try to call
AFX_MANAGE_STATE(AfxGetStaticModuleState());in my function ShowDialog, I get a linker error LINK2005: "_DLLMain@12 is already defined in DXFDEC.obj"
Concerning the comment of @IInspectable, here is a minimal example, which generates a linker error. It is generated by the Visual Studio code generator. I have just added the function MyFunction
#include "pch.h"
#include "framework.h"
#include <afxwin.h>
#include <afxdllx.h>
static AFX_EXTENSION_MODULE MFCLibrary3DLL = { false, nullptr };
extern "C" int APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
UNREFERENCED_PARAMETER(lpReserved);
if (dwReason == DLL_PROCESS_ATTACH)
{
if (!AfxInitExtensionModule(MFCLibrary3DLL, hInstance))
return 0;
new CDynLinkLibrary(MFCLibrary3DLL);
}
else if (dwReason == DLL_PROCESS_DETACH)
{
AfxTermExtensionModule(MFCLibrary3DLL);
}
return 1; // OK
}
extern "C" __declspec(dllexport) int MyFunction()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
