How to use your own DllMain in MFC Dll's | CodeGuru

How to use your own DllMain in MFC Dll’s

Environment:VC 6.0 If you want to write your own DllMain in a MFC Dll then you have to do some real cool stuff because MFC internally calls its DllMain. If you try to write your own DllMain, you get a linker error saying “DllMain already defined”. To resolve this issue you have to copy “DllModul.cpp” […]

Written By
CodeGuru Staff
CodeGuru Staff
Jan 29, 2002
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment:VC 6.0

If you want to write your own DllMain in a MFC Dll then you have to do some real cool stuff because MFC internally calls its DllMain. If you try to write your own DllMain, you get a linker error saying “DllMain already defined”. To resolve this issue you have to copy “DllModul.cpp” from MFC source directory to your project directory and include it in your own project.

The code for this follows:

//////////////////////////////////////////////
// export DllMain for the DLL
extern "C"
BOOL WINAPI DllMain(HINSTANCE hInstance,
                    DWORD dwReason,
                    LPVOID /*lpReserved*/)
{
  //……………..
  // Add your own initialisation Here
  if (dwReason == DLL_PROCESS_ATTACH)
  {
    BOOL bResult = FALSE;
#ifdef _AFXDLL
    // wire up resources from core DLL
    AfxCoreInitModule();
#endif
    …
    …
    …
    return TRUE;
}
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.