AfxInitExtensionModule
Call this function in an extension DLL's DllMain to initialize the DLL.
BOOL AFXAPI AfxInitExtensionModule( AFX_EXTENSION_MODULE& state, HMODULE hModule );
For example:
static AFX_EXTENSION_MODULE NVC_MFC_DLLDLL = { NULL, NULL }; extern "C" int APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) { // Remove this if you use lpReserved UNREFERENCED_PARAMETER(lpReserved); if (dwReason == DLL_PROCESS_ATTACH) { TRACE0("NVC_MFC_DLL.DLL Initializing!\n"); // Extension DLL one-time initialization if (!AfxInitExtensionModule(NVC_MFC_DLLDLL, hInstance)) return 0;
AfxInitExtensionModule makes a copy of the DLL's HMODULE and captures the DLL's runtime-classes (CRuntimeClass structures) as well as its object factories (COleObjectFactory objects) for use later when the CDynLinkLibrary object is created.
MFC extension DLLs need to do two things in their DllMain function:
-
Call AfxInitExtensionModule and check the return value.
-
Create a CDynLinkLibrary object if the DLL will be exporting CRuntimeClass Structure objects or has its own custom resources.
You can call AfxTermExtensionModule to clean up the extension DLL when each process detaches from the extension DLL (which happens when the process exits, or when the DLL is unloaded as a result of an AfxFreeLibrary call).
struct AFX_EXTENSION_MODULESo the C++ example, which does
{
BOOL bInitialized;
HMODULE hModule;
HMODULE hResource;
CRuntimeClass* pFirstSharedClass;
COleObjectFactory* pFirstSharedFactory;
};
static AFX_EXTENSION_MODULE NVC_MFC_DLLDLL = { NULL, NULL }
seems incorrect. It just works, as long as the macro NULL is defined as an integer (0). However, C++0x is likely to support defining NULL as nullptr, which may not be convertible to BOOL.
- 9/6/2010
- Niels_Dekker