Initializing Regular DLLs


The new home for Visual Studio documentation is Visual Studio 2017 Documentation on

The latest version of this topic can be found at Initializing Regular DLLs.

Because regular DLLs have a CWinApp object, they should perform their initialization and termination tasks in the same location as an MFC application: in the InitInstance and ExitInstance member functions of the DLL's CWinApp-derived class. Because MFC provides a DllMain function that is called by _DllMainCRTStartup for PROCESS_ATTACH and PROCESS_DETACH, you should not write your own DllMain function. The MFC-provided DllMain function calls InitInstance when your DLL is loaded and it calls ExitInstance before the DLL is unloaded.

A regular DLL can keep track of multiple threads by calling TlsAlloc and TlsGetValue in its InitInstance function. These functions allow the DLL to track thread-specific data.

In your regular DLL that dynamically links to MFC, if you are using any MFC OLE, MFC Database (or DAO), or MFC Sockets support, respectively, the MFC debug extension DLLs MFCOxxD.dll, MFCDxxD.dll, and MFCNxxD.dll (where xx is the version number) are linked in automatically. You must call one of the following predefined initialization functions for each of these DLLs that you are using in your regular DLL's CWinApp::InitInstance.

Type of MFC support Initialization function to call
MFC OLE (MFCOxxD.dll) AfxOleInitModule
MFC Database (MFCDxxD.dll) AfxDbInitModule
MFC Sockets (MFCNxxD.dll) AfxNetInitModule

What do you want to do?

What do you want to know more about?

See Also

Initializing a DLL