home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / dllmodul.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  6KB  |  248 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef _DEBUG
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. #define new DEBUG_NEW
  19.  
  20. /////////////////////////////////////////////////////////////////////////////
  21. // global data
  22.  
  23. // The following symbol used to force inclusion of this module for _USRDLL
  24. #ifdef _X86_
  25. extern "C" { int _afxForceUSRDLL; }
  26. #else
  27. extern "C" { int __afxForceUSRDLL; }
  28. #endif
  29.  
  30. #ifdef _AFXDLL
  31.  
  32. static AFX_EXTENSION_MODULE controlDLL;
  33.  
  34. // force initialization early
  35. #pragma warning(disable: 4074)
  36. #pragma init_seg(lib)
  37.  
  38. /////////////////////////////////////////////////////////////////////////////
  39. // static-linked version of AfxWndProc for use by this module
  40.  
  41. LRESULT CALLBACK AfxWndProcDllStatic(HWND, UINT, WPARAM, LPARAM);
  42.  
  43. class _AFX_DLL_MODULE_STATE : public AFX_MODULE_STATE
  44. {
  45. public:
  46.     _AFX_DLL_MODULE_STATE() : AFX_MODULE_STATE(TRUE, AfxWndProcDllStatic, _MFC_VER)
  47.         { }
  48. };
  49.  
  50. static _AFX_DLL_MODULE_STATE afxModuleState;
  51.  
  52. #undef AfxWndProc
  53. LRESULT CALLBACK
  54. AfxWndProcDllStatic(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  55. {
  56.     AFX_MANAGE_STATE(&afxModuleState);
  57.     return AfxWndProc(hWnd, nMsg, wParam, lParam);
  58. }
  59.  
  60. AFX_MODULE_STATE* AFXAPI AfxGetStaticModuleState()
  61. {
  62.     AFX_MODULE_STATE* pModuleState = &afxModuleState;
  63.     return pModuleState;
  64. }
  65.  
  66. #endif
  67.  
  68. /////////////////////////////////////////////////////////////////////////////
  69. // export DllMain for the DLL
  70.  
  71. extern "C"
  72. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
  73. {
  74.     if (dwReason == DLL_PROCESS_ATTACH)
  75.     {
  76.         BOOL bResult = FALSE;
  77.  
  78. #ifdef _AFXDLL
  79.         // wire up resources from core DLL
  80.         AfxCoreInitModule();
  81. #endif
  82.  
  83.         _AFX_THREAD_STATE* pState = AfxGetThreadState();
  84.         AFX_MODULE_STATE* pPrevModState = pState->m_pPrevModuleState;
  85.  
  86.         // Initialize DLL's instance(/module) not the app's
  87.         if (!AfxWinInit(hInstance, NULL, _T(""), 0))
  88.         {
  89.             AfxWinTerm();
  90.             goto Cleanup;       // Init Failed
  91.         }
  92.  
  93.         // initialize the single instance DLL
  94.         CWinApp* pApp; pApp = AfxGetApp();
  95.         if (pApp != NULL && !pApp->InitInstance())
  96.         {
  97.             pApp->ExitInstance();
  98.             AfxWinTerm();
  99.             goto Cleanup;       // Init Failed
  100.         }
  101.  
  102.         pState->m_pPrevModuleState = pPrevModState;
  103. #ifdef _AFXDLL
  104.         // wire up this DLL into the resource chain
  105.         VERIFY(AfxInitExtensionModule(controlDLL, hInstance));
  106.         CDynLinkLibrary* pDLL; pDLL = new CDynLinkLibrary(controlDLL);
  107.         ASSERT(pDLL != NULL);
  108. #else
  109.         AfxInitLocalData(hInstance);
  110. #endif
  111.  
  112.         bResult = TRUE;
  113.  
  114. Cleanup:
  115.         pState->m_pPrevModuleState = pPrevModState;
  116. #ifdef _AFXDLL
  117.         // restore previously-saved module state
  118.         VERIFY(AfxSetModuleState(AfxGetThreadState()->m_pPrevModuleState) ==
  119.             &afxModuleState);
  120.         DEBUG_ONLY(AfxGetThreadState()->m_pPrevModuleState = NULL);
  121. #endif
  122.         return bResult;
  123.     }
  124.     else if (dwReason == DLL_PROCESS_DETACH)
  125.     {
  126. #ifdef _AFXDLL
  127.         // set module state for cleanup
  128.         ASSERT(AfxGetThreadState()->m_pPrevModuleState == NULL);
  129.         AfxGetThreadState()->m_pPrevModuleState =
  130.             AfxSetModuleState(&afxModuleState);
  131. #endif
  132.  
  133.         CWinApp* pApp = AfxGetApp();
  134.         if (pApp != NULL)
  135.             pApp->ExitInstance();
  136.  
  137. #ifdef _DEBUG
  138.         // check for missing AfxLockTempMap calls
  139.         if (AfxGetModuleThreadState()->m_nTempMapLock != 0)
  140.         {
  141.             TRACE1("Warning: Temp map lock count non-zero (%ld).\n",
  142.                 AfxGetModuleThreadState()->m_nTempMapLock);
  143.         }
  144. #endif
  145.         AfxLockTempMaps();
  146.         AfxUnlockTempMaps(-1);
  147.  
  148.         // terminate the library before destructors are called
  149.         AfxWinTerm();
  150.  
  151. #ifdef _AFXDLL
  152.         AfxTermExtensionModule(controlDLL, TRUE);
  153. #else
  154.         AfxTermLocalData(hInstance, TRUE);
  155. #endif
  156.     }
  157.     else if (dwReason == DLL_THREAD_DETACH)
  158.     {
  159.         AFX_MANAGE_STATE(&afxModuleState);
  160.  
  161. #ifdef _DEBUG
  162.         // check for missing AfxLockTempMap calls
  163.         if (AfxGetModuleThreadState()->m_nTempMapLock != 0)
  164.         {
  165.             TRACE1("Warning: Temp map lock count non-zero (%ld).\n",
  166.                 AfxGetModuleThreadState()->m_nTempMapLock);
  167.         }
  168. #endif
  169.         AfxLockTempMaps();
  170.         AfxUnlockTempMaps(-1);
  171.  
  172.         AfxTermThread(hInstance);
  173.     }
  174.  
  175.     return TRUE;
  176. }
  177.  
  178. #ifdef _AFXDLL
  179.  
  180. /////////////////////////////////////////////////////////////////////////////
  181. // initialize app state such that it points to this module's core state
  182.  
  183. extern "C" BOOL WINAPI RawDllMain(HINSTANCE, DWORD dwReason, LPVOID);
  184. extern "C" BOOL (WINAPI* _pRawDllMain)(HINSTANCE, DWORD, LPVOID) = &RawDllMain;
  185.  
  186. extern "C"
  187. BOOL WINAPI RawDllMain(HINSTANCE, DWORD dwReason, LPVOID)
  188. {
  189.     if (dwReason == DLL_PROCESS_ATTACH)
  190.     {
  191.         // make sure we have enough memory to attempt to start (8kb)
  192.         void* pMinHeap = LocalAlloc(NONZEROLPTR, 0x2000);
  193.         if (pMinHeap == NULL)
  194.             return FALSE;   // fail if memory alloc fails
  195.         LocalFree(pMinHeap);
  196.  
  197. #ifndef _AFXDLL
  198.         if (!AfxCriticalInit())
  199.             return FALSE;
  200. #endif
  201.  
  202.         // set module state before initialization
  203.         _AFX_THREAD_STATE* pState = AfxGetThreadState();
  204.         pState->m_pPrevModuleState = AfxSetModuleState(&afxModuleState);
  205.  
  206.     }
  207.     else if (dwReason == DLL_PROCESS_DETACH)
  208.     {
  209.         // restore module state after cleanup
  210.         _AFX_THREAD_STATE* pState = AfxGetThreadState();
  211.         VERIFY(AfxSetModuleState(pState->m_pPrevModuleState) ==
  212.             &afxModuleState);
  213.         DEBUG_ONLY(pState->m_pPrevModuleState = NULL);
  214.  
  215. #ifndef _AFXDLL
  216.         AfxCriticalTerm();
  217. #endif
  218.     }
  219.     return TRUE;
  220. }
  221.  
  222. #endif //_AFXDLL
  223.  
  224. /////////////////////////////////////////////////////////////////////////////
  225. // Special case for static library startup/termination
  226.  
  227. #ifndef _AFXDLL
  228.  
  229. // force initialization early
  230. #pragma warning(disable: 4074)
  231. #pragma init_seg(lib)
  232.  
  233. void AFX_CDECL AfxTermDllState()
  234. {
  235.     // terminate local data and critical sections
  236.     AfxTermLocalData(NULL, TRUE);
  237.     AfxCriticalTerm();
  238.  
  239.     // release the reference to thread local storage data
  240.     AfxTlsRelease();
  241. }
  242.  
  243. char _afxTermDllState = (char)(AfxTlsAddRef(), atexit(&AfxTermDllState));
  244.  
  245. #endif // !_AFXDLL
  246.  
  247. /////////////////////////////////////////////////////////////////////////////
  248.