home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / dlldb.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-16  |  3.9 KB  |  140 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 AFX_INIT_SEG
  14. #pragma code_seg(AFX_INIT_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // Initialization of MFC extension DLL
  24.  
  25. static AFX_EXTENSION_MODULE extensionDLL;
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // Library initialization and cleanup
  29.  
  30. extern "C" BOOL WINAPI RawDllMain(HINSTANCE, DWORD dwReason, LPVOID);
  31. extern "C" BOOL (WINAPI* _pRawDllMain)(HINSTANCE, DWORD, LPVOID) = &RawDllMain;
  32.  
  33. #ifdef _DEBUG
  34. #ifndef _UNICODE
  35. #define MFC42_DLL   "MFC42D.DLL"
  36. #define MFCO42_DLL  "MFCO42D.DLL"
  37. #else
  38. #define MFC42_DLL   "MFC42UD.DLL"
  39. #define MFCO42_DLL  "MFCO42UD.DLL"
  40. #endif
  41. #else
  42. #ifndef _UNICODE
  43. #define MFC42_DLL   "MFC42.DLL"
  44. #define MFCO42_DLL  "MFCO42.DLL"
  45. #else
  46. #define MFC42_DLL   "MFC42U.DLL"
  47. #define MFCO42_DLL  "MFCO42U.DLL"
  48. #endif
  49. #endif
  50.  
  51. extern "C"
  52. BOOL WINAPI RawDllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID)
  53. {
  54.     UNUSED_ALWAYS(hInstance);
  55.     if (dwReason == DLL_PROCESS_ATTACH)
  56.     {
  57.         // Prevent the MFC DLLs from being unloaded prematurely
  58.         LoadLibraryA(MFC42_DLL);
  59.         LoadLibraryA(MFCO42_DLL);
  60.  
  61.         // make sure we have enough memory to attempt to start (8kb)
  62.         void* pMinHeap = LocalAlloc(NONZEROLPTR, 0x2000);
  63.         if (pMinHeap == NULL)
  64.             return FALSE;   // fail if memory alloc fails
  65.         LocalFree(pMinHeap);
  66.  
  67.         // save critical data pointers before running the constructors
  68.         AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  69.         pModuleState->m_pClassInit = pModuleState->m_classList;
  70.         pModuleState->m_pFactoryInit = pModuleState->m_factoryList;
  71.         pModuleState->m_classList.m_pHead = NULL;
  72.         pModuleState->m_factoryList.m_pHead = NULL;
  73.     }
  74.     else if (dwReason == DLL_PROCESS_DETACH)
  75.     {
  76.         // Now it's OK for the MFC DLLs to be unloaded (see LoadLibrary above)
  77.         FreeLibrary(GetModuleHandleA(MFCO42_DLL));
  78.         FreeLibrary(GetModuleHandleA(MFC42_DLL));
  79.     }
  80.     return TRUE;    // ok
  81. }
  82.  
  83. extern "C"
  84. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
  85. {
  86.     if (dwReason == DLL_PROCESS_ATTACH)
  87.     {
  88.         // shared initialization
  89.         VERIFY(AfxInitExtensionModule(extensionDLL, hInstance));
  90.  
  91.         // wire up this DLL into the resource chain
  92.         CDynLinkLibrary* pDLL = new CDynLinkLibrary(extensionDLL, TRUE);
  93.         ASSERT(pDLL != NULL);
  94.     }
  95.     else if (dwReason == DLL_PROCESS_DETACH)
  96.     {
  97.         AfxTermExtensionModule(extensionDLL);
  98.     }
  99.     else if (dwReason == DLL_THREAD_DETACH)
  100.     {
  101.         AfxTermThread(hInstance);
  102.     }
  103.     return TRUE;    // ok
  104. }
  105.  
  106. ////////////////////////////////////////////////////////////////////////////
  107. // Special initialization entry point for controls
  108.  
  109. void AFXAPI AfxDbInitModule()
  110. {
  111.     ASSERT(AfxGetModuleState() != AfxGetAppModuleState());
  112.  
  113.     CDynLinkLibrary* pDLL = new CDynLinkLibrary(extensionDLL, TRUE);
  114.     ASSERT(pDLL != NULL);
  115. }
  116.  
  117. /////////////////////////////////////////////////////////////////////////////
  118. // Special code to wire up vector deleting destructors
  119.  
  120. #ifdef AFX_VDEL_SEG
  121. #pragma code_seg(AFX_VDEL_SEG)
  122. #endif
  123. static void _AfxForceVectorDelete()
  124. {
  125.     ASSERT(FALSE);  // never called
  126.  
  127.     new CDatabase[2];
  128.     new CLongBinary[2];
  129.  
  130. #ifndef _AFX_NO_DAO_SUPPORT
  131.     new CDaoWorkspace[2];
  132.     new CDaoException[2];
  133.     new CDaoDatabase[2];
  134.     new CDaoRecordset[2];
  135. #endif
  136. }
  137. void (*_afxForceVectorDelete_mfcd)() = &_AfxForceVectorDelete;
  138.  
  139. /////////////////////////////////////////////////////////////////////////////
  140.