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"
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;
}


Comments

  • good but why?

    Posted by flyingxu on 11/08/2007 02:35am

    I wrote a blog about why you shoud add that file:
    http://www.codediscuss.com/mfc/dllmain-in-mfc-dll/

    Reply
  • good - _-)b

    Posted by Legacy on 05/10/2003 12:00am

    Originally posted by: yo

    greate solved my problem

    Reply
  • Does VC++/C++ is supported by JNI?

    Posted by Legacy on 09/06/2002 12:00am

    Originally posted by: Padmashree Desai

    Dear Sir/Madam
    
    

    In our project we need to find RAM & HDD capacity using mobile agent concept. But JAVA gives only virtual m/c capacity. So we need to use JNI. We need to write a C++ or VC++ program for this API. Please tell me whether I should go for C++/VC++. & how?


    Thanking you,

    Padmashree.

    Reply
  • How do i capture clients scree using VC++ programming at the server end

    Posted by Legacy on 02/07/2002 12:00am

    Originally posted by: Rambhavan Verma

    Dear sir,
    this is rambhavan verma here.
    Please try to give me some suggestions/solutions on the above mentioned page title.
    I have to do one project on the above mentioned topic.If you can help me then try to send me some code or any suggestions on the respective topic.
    Thanking you,
    Rambhavan Verma.

    Reply
  • You don't need a DllMain anyway

    Posted by Legacy on 02/01/2002 12:00am

    Originally posted by: bug_crusher

    A Regular MFC dll doesn't even need a DllMain function. Its only needed for ISAPI dlls and MFC Extension Dlls. For a regular MFC dll, you are better off by leaving it alone and not writing your own. There is no reason to do this nonsense at all. I hope someone reads TN033 before they take this bad advice.

    • Imagine ...

      Posted by HaLbMiLcH on 03/19/2004 08:28pm

      of actions necessary at process attach time AND the need of using MFC classes in that DLL. A basic win32 dll can't resolve this use case.

      Reply
    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 29, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this eSeminar, Gene Kim will discuss these survey findings and will share woeful tales of artifact management gone wrong! Gene will also share examples of how …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds