Export (0) Print
Expand All
MFC
TRY
Expand Minimize

AfxTermExtensionModule

Call this function to allow MFC to cleanup the extension DLL when each process detaches from the DLL (which happens when the process exits, or when the DLL is unloaded as a result of a AfxFreeLibrary call).

void AFXAPI AfxTermExtensionModule(
   AFX_EXTENSION_MODULE& state,
   BOOL bAll  = FALSE 
);

state

A reference to the AFX_EXTENSION_MODULE structure that contains the state of extension DLL module.

bAll

If TRUE, cleanup all extension DLL modules. Otherwise, cleanup only the current DLL module.

AfxTermExtensionModule will delete any local storage attached to the module and remove any entries from the message map cache. 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;

		new CMyDynLinkLibrary(NVC_MFC_DLLDLL);

	}
	else if (dwReason == DLL_PROCESS_DETACH)
	{
		TRACE0("NVC_MFC_DLL.DLL Terminating!\n");

		// Terminate the library before destructors are called
		AfxTermExtensionModule(NVC_MFC_DLLDLL);
	}
	return 1;   // ok
}


If your application loads and frees extension DLLs dynamically, be sure to call AfxTermExtensionModule. Since most extension DLLs are not dynamically loaded (usually, they are linked via their import libraries), the call to AfxTermExtensionModule is usually not necessary.

MFC extension DLLs need to call AfxInitExtensionModule in their DllMain. If the DLL will be exporting CRuntimeClass objects or has its own custom resources, you also need to create a CDynLinkLibrary object in DllMain.

Header: afxdll_.h

Community Additions

ADD
Show:
© 2015 Microsoft