It's so clear that there are three type DLLs: regular DLL, extension DLL and non-mfc dll on Windows. below table from msdn shows how to initialize it:
Typically, your dll has initialization code (such as allocating memory) that must execute when your DLL loads. when using Visual C ++, where you add code to initialize your DLL depends on the type of DLL you are building. if you do not need to add initialization or termination code, there is nothing special you have to do when building your DLL. if you need to initialize your DLL, the following table describes where to add your code.
| DLL type |
Where to add initialization and termination code |
Regular DLL |
In the DLL'sCwinapp object'sInitinstance andExitinstance. |
Extension DLL |
InDllmain function generated by the mfc dll wizard. |
Non-MFC DLL |
In a function calledDllmain that you provide. |
In Win32, all DLLs might contain an optional entry-point function (usually calledDllmain) That is called for both initialization and termination. this gives you an opportunity to allocate or release additional resources as needed. windows callthe entry-point function in four situations: Process attach, process detach, thread attach, and thread detach.
The C run-time Library provides an entry-point function called_ Dllmaincrtstartup, And it CILSDllmain. Depending on the type of DLL, you shoshould have a function calledDllmainIn your source code or you shoshould useDllmainProvided in the MFC library.
A. Key Points of initialization of extension DLL
1. For dll_process_attach: it must callAfxinitextensionmodule(). You shocould check the return valueAfxinitextensionmodule; If a zero value is returned fromAfxinitextensionmodule, Return zero from yourDllmainFunction. and creating a newCdynlinklibraryObject during initialization allows the extension DLL to exportCruntimeclassObjects or resources to the client application.
2. For dll_process_detach:AfxtermextensionmoduleIs not must to be called. Only if your extension DLL will be explicitly linked to an executable (meaning the executable CILSAfxloadlibraryTo link to the DLL), you should add a callAfxtermextensionmodule. This function allows MFC 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 ofAfxfreelibraryCall). If your extension DLL will be linked implicitly to the application, the callAfxtermextensionmoduleIs not necessary.