Article ID: 148791 - Last Review: November 21, 2006 - Revision: 2.1 HOWTO: How to Provide Your Own DllMain in an MFC Regular DLLThis article was previously published under Q148791 On This PageSUMMARY
By design, MFC Regular DLLs have a default DllMain function provided
automatically by MFC. Regular DLLs should not provide their own DllMain.
Any initialization which is necessary when the DLL is loaded should be done
in the InitInstance member function of the one CWinApp-derived class in the
Regular DLL. Deinitialization and termination code should go in the
ExitInstance member function.
However, InitInstance is only called by MFC's DllMain when a process attaches (DLL_PROCESS_ATTACH) to the DLL and ExitInstance is called only when a process detaches (DLL_PROCESS_DETACH) from the DLL. If it is necessary to handle thread attachment to and detachment from (DLL_THREAD_ATTACH and DLL_THREAD_DETACH) in MFC Regular DLL, the Regular DLL will need to provide its own DllMain. This article explains how to do it. MORE INFORMATION
When a Regular DLL is created, the MFC source forcibly links in the code
for the source file \Msdev\Mfc\Src\Dllmodul.cpp. Dllmodul.cpp contains most
of the code added to a Regular DLL to support MFC in that DLL. One of the
most important functions in Dllmodul.cpp is the DllMain function.
To add code to MFC's DllMain, copy the \Msdev\Mfc\Src\Dllmodul.cpp source file to your project directory, and include the copy in your project. This copy of Dllmodul.cpp will be compiled and linked into your DLL instead of the Dllmodul.cpp in the Mfc\Src directory, so changes to the DllMain in it will show up in the final DLL. The primary caveat is that this is not a recommended solution and should only be used when absolutely necessary. Any changes to the code in Dllmodul.cpp will undoubtedly have unpredictable results. Add code only, do not remove or modify the existing code. For regular DLLs that use MFC in the Shared Lib, the module state should be set at the beginning of any added code and restored before returning from DllMain. Refer to the sample code in this article for an example of a DllMain that handles the DLL_THREAD_ATTACH and DLL_THREAD_DETACH notifications and properly switches the module state as necessary. Additional care must be taken when relying on DllMain being called with DLL_THREAD_ATTACH and DLL_THREAD_DETACH because of the following conditions:
Sample CodeREFERENCES
For more information on MFC objects being thread local, please see the
following article in the Microsoft Knowledge Base:
147578
(http://support.microsoft.com/kb/147578/EN-US/
)
CWnd Derived MFC Objects and Multi-threaded Applications
For more information on module state information, please see the following
article in the Microsoft Knowledge Base:
140850
(http://support.microsoft.com/kb/140850/EN-US/
)
How to Convert DLLTRACE to Use MFC in Shared Library
For more information on why you shouldn't create a thread in DllMain,
please see the following article in the Microsoft Knowledge Base:
142243
(http://support.microsoft.com/kb/142243/EN-US/
)
PRB: Cannot Create an MFC Thread During DLL Startup
APPLIES TO
| Article Translations
|

Back to the top
