在常规MFC的DLL初始化中,如何获取DLL_PROCESS_ATTACH 等4种通知?我只知道普通的DLLMAIN比如
BOOL WINAPI DllMain(HINSTANCE hinstDll, DWORD fdwReason, PVOID fImpLoad) {
   switch (fdwReason) {      case DLL_PROCESS_ATTACH:
         break;      case DLL_THREAD_ATTACH:
                break;      case DLL_THREAD_DETACH:
              break;      case DLL_PROCESS_DETACH:
         //这里处理清除工作
         break;
   }
   return(TRUE);
}那在 常规的MFC DLL中,如何判断这4种通知?

解决方案 »

  1.   

    只要是DLL,它就有DllMain,常规DLL也不例外,只不过被MS隐藏起来了,就像WinMain函数一样,你去找找吧!!!
      

  2.   

    ..\Microsoft Visual Studio\VC98\MFC\SRC\DLLMODUL.cpp
    从第71行看起
      

  3.   

    如果只是简单的资源分配回收可参考如下方法实现
    ●Initializing Regular DLLs
    Because regular DLLs have a CWinApp object, they should perform their initialization and termination tasks in the same location as an MFC application: in the InitInstance and ExitInstance member functions of the DLL's CWinApp-derived class. Because MFC provides a DllMain function that is called by _DllMainCRTStartup for PROCESS_ATTACH and PROCESS_DETACH, you should not write your own DllMain function. The MFC-provided DllMain function calls InitInstance when your DLL is loaded and it calls ExitInstance before the DLL is unloaded.如果就是想在常规MFC DLL中使用DllMain函数参考以下HOWTO//From MSDN Knowledge Base
    HOWTO: How to Provide Your Own DllMain in an MFC Regular DLL
    ●SUMMARY
    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: 
    When a thread is created in a process, the system calls DllMain with a value of DLL_THREAD_ATTACH for each of the DLLs mapped into into the process. However, if a process has several threads running in it when a new DLL is mapped into it, DllMain isn't called with a DLL_THREAD_ATTACH value for any of the existing threads. 
    DllMain is not called with a value DLL_THREAD_ATTACH for the process's primary thread. 
    On thread termination (by a call to ExitThread), DllMain is called with a value of DLL_THREAD_DETACH for each of the DLLs. DllMain is not called with DLL_THREAD_DETACH for any thread unless a thread terminates by calling ExitThread. 
    If a thread terminates due to a call to TerminateThread, DllMain isn't called with a value DLL_THREAD_DETACH. 
    It is possible for a thread in a process to call LoadLibrary to load a DLL causing a call to DllMain with DLL_PROCESS_ATTACH, and then on thread termination, cause a call to dllMain with DLL_THREAD_DETACH without ever calling DLL_THREAD_ATTACH. It is therefore best that the thread that calls LoadLibrary also call FreeLibrary. 
    NOTE: MFC CWnd objects, CDC objects, CMenu objects, GDI objects, and CImageList objects are restricted to a per-thread, per-module basis. In other words, MFC objects created in one module or thread cannot be passed to and/or used in a different module or thread. This has special relevance for any code added to handle DLL_THREAD_ATTACH or DLL_THREAD_DETACH in DllMain because DllMain is called for these reasons with different threads. CWnd objects, for instance, created in DllMain during DLL_PROCESS_ATTACH or in InitInstance will not be valid during DLL_THREAD_ATTACH.