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” 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"
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

return TRUE;

More by Author

Must Read