home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / dllinit.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-16  |  21.0 KB  |  795 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. #include <stdarg.h>
  13.  
  14. #ifdef AFX_INIT_SEG
  15. #pragma code_seg(AFX_INIT_SEG)
  16. #endif
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. #ifndef _AFXDLL
  24.     #error file must be compiled with _AFXDLL
  25. #endif
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // _AFXDLL support
  29.  
  30. static AFX_EXTENSION_MODULE coreDLL;
  31. #ifdef _AFX_OLE_IMPL
  32. AFX_MODULE_STATE* AFXAPI _AfxGetOleModuleState();
  33. #endif
  34.  
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CDynLinkLibrary class
  37.  
  38. IMPLEMENT_DYNAMIC(CDynLinkLibrary, CCmdTarget)
  39.  
  40. // Constructor - will wire into the current application's list
  41. CDynLinkLibrary::CDynLinkLibrary(AFX_EXTENSION_MODULE& state, BOOL bSystem)
  42. {
  43. #ifndef _AFX_NO_OLE_SUPPORT
  44.     m_factoryList.Construct(offsetof(COleObjectFactory, m_pNextFactory));
  45. #endif
  46.     m_classList.Construct(offsetof(CRuntimeClass, m_pNextClass));
  47.  
  48.     // copy info from AFX_EXTENSION_MODULE struct
  49.     ASSERT(state.hModule != NULL);
  50.     m_hModule = state.hModule;
  51.     m_hResource = state.hResource;
  52.     m_classList.m_pHead = state.pFirstSharedClass;
  53. #ifndef _AFX_NO_OLE_SUPPORT
  54.     m_factoryList.m_pHead = state.pFirstSharedFactory;
  55. #endif
  56.     m_bSystem = bSystem;
  57.  
  58.     // insert at the head of the list (extensions will go in front of core DLL)
  59.     DEBUG_ONLY(m_pNextDLL = NULL);
  60.     AfxLockGlobals(CRIT_DYNLINKLIST);
  61.     m_pModuleState->m_libraryList.AddHead(this);
  62.     AfxUnlockGlobals(CRIT_DYNLINKLIST);
  63. }
  64.  
  65. CDynLinkLibrary::CDynLinkLibrary(HINSTANCE hModule, HINSTANCE hResource)
  66. {
  67. #ifndef _AFX_NO_OLE_SUPPORT
  68.     m_factoryList.Construct(offsetof(COleObjectFactory, m_pNextFactory));
  69. #endif
  70.     m_classList.Construct(offsetof(CRuntimeClass, m_pNextClass));
  71.  
  72.     m_hModule = hModule;
  73.     m_hResource = hResource;
  74.     m_classList.m_pHead = NULL;
  75. #ifndef _AFX_NO_OLE_SUPPORT
  76.     m_factoryList.m_pHead = NULL;
  77. #endif
  78.     m_bSystem = FALSE;
  79.  
  80.     // insert at the head of the list (extensions will go in front of core DLL)
  81.     DEBUG_ONLY(m_pNextDLL = NULL);
  82.     AfxLockGlobals(CRIT_DYNLINKLIST);
  83.     m_pModuleState->m_libraryList.AddHead(this);
  84.     AfxUnlockGlobals(CRIT_DYNLINKLIST);
  85. }
  86.  
  87. #ifdef AFX_TERM_SEG
  88. #pragma code_seg(AFX_TERM_SEG)
  89. #endif
  90.  
  91. CDynLinkLibrary::~CDynLinkLibrary()
  92. {
  93.     // remove this frame window from the list of frame windows
  94.     AfxLockGlobals(CRIT_DYNLINKLIST);
  95.     m_pModuleState->m_libraryList.Remove(this);
  96.     AfxUnlockGlobals(CRIT_DYNLINKLIST);
  97. }
  98.  
  99. /////////////////////////////////////////////////////////////////////////////
  100. // CDynLinkLibrary diagnostics
  101.  
  102. #ifdef _DEBUG
  103. void CDynLinkLibrary::AssertValid() const
  104. {
  105.     ASSERT(m_hModule != NULL);
  106. }
  107.  
  108. void CDynLinkLibrary::Dump(CDumpContext& dc) const
  109. {
  110.     CCmdTarget::Dump(dc);
  111.  
  112.     dc << "m_hModule = " << (UINT)m_hModule;
  113.     dc << "\nm_hResource = " << (UINT)m_hResource;
  114.  
  115.     if (m_hModule != NULL)
  116.     {
  117.         TCHAR szName[_MAX_PATH];
  118.         GetModuleFileName(m_hModule, szName, _countof(szName));
  119.         dc << "\nmodule name = " << szName;
  120.     }
  121.     else
  122.         dc << "\nmodule name is unknown";
  123.  
  124.     dc << "\n";
  125. }
  126. #endif //_DEBUG
  127.  
  128. #ifdef AFX_INIT_SEG
  129. #pragma code_seg(AFX_INIT_SEG)
  130. #endif
  131.  
  132. /////////////////////////////////////////////////////////////////////////////
  133. // special initialization and helper functions
  134.  
  135. // This is called in an extension DLL's DllMain
  136. //  It makes a copy of the DLL's HMODULE, as well as a copy of the
  137. //  runtime class objects that have been initialized by this
  138. //  extension DLL as part of normal static object construction
  139. //  executed before DllMain is entered.
  140.  
  141. BOOL AFXAPI AfxInitExtensionModule(AFX_EXTENSION_MODULE& state, HMODULE hModule)
  142. {
  143.     // only initialize once
  144.     if (state.bInitialized)
  145.     {
  146.         AfxInitLocalData(hModule);
  147.         return TRUE;
  148.     }
  149.     state.bInitialized = TRUE;
  150.  
  151.     // save the current HMODULE information for resource loading
  152.     ASSERT(hModule != NULL);
  153.     state.hModule = hModule;
  154.     state.hResource = hModule;
  155.  
  156.     // save the start of the runtime class list
  157.     AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  158.     state.pFirstSharedClass = pModuleState->m_classList.GetHead();
  159.     pModuleState->m_classList.m_pHead = pModuleState->m_pClassInit;
  160.  
  161. #ifndef _AFX_NO_OLE_SUPPORT
  162.     // save the start of the class factory list
  163.     state.pFirstSharedFactory = pModuleState->m_factoryList.GetHead();
  164.     pModuleState->m_factoryList.m_pHead = pModuleState->m_pFactoryInit;
  165. #endif
  166.  
  167.     return TRUE;
  168. }
  169.  
  170. #ifdef AFX_TERM_SEG
  171. #pragma code_seg(AFX_TERM_SEG)
  172. #endif
  173.  
  174. void AFXAPI AfxTermExtensionModule(AFX_EXTENSION_MODULE& state, BOOL bAll)
  175. {
  176.     // make sure initialized
  177.     if (!state.bInitialized)
  178.         return;
  179.  
  180.     // search for CDynLinkLibrary matching state.hModule and delete it
  181.     ASSERT(state.hModule != NULL);
  182.     AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  183.     AfxLockGlobals(CRIT_DYNLINKLIST);
  184.     for (CDynLinkLibrary* pDLL = pModuleState->m_libraryList; pDLL != NULL; )
  185.     {
  186.         CDynLinkLibrary* pNextDLL = pDLL->m_pNextDLL;
  187.         if (bAll || pDLL->m_hModule == state.hModule)
  188.             delete pDLL;    // will unwire itself
  189.         pDLL = pNextDLL;
  190.     }
  191.     AfxUnlockGlobals(CRIT_DYNLINKLIST);
  192.  
  193.     // delete any local storage attached to this module
  194.     AfxTermLocalData(state.hModule, TRUE);
  195.  
  196.     // remove any entries from the CWnd message map cache
  197.     AfxResetMsgCache();
  198. }
  199.  
  200. /////////////////////////////////////////////////////////////////////////////
  201. // special LoadLibrary and FreeLibrary for loading MFC extension DLLs
  202.  
  203. HINSTANCE AFXAPI AfxLoadLibrary(LPCTSTR lpszModuleName)
  204. {
  205.     ASSERT(lpszModuleName != NULL);
  206.     AfxLockGlobals(CRIT_LOCKSHARED);
  207.     HINSTANCE hInstLib = LoadLibrary(lpszModuleName);
  208.     AfxUnlockGlobals(CRIT_LOCKSHARED);
  209.     return hInstLib;
  210. }
  211.  
  212. BOOL AFXAPI AfxFreeLibrary(HINSTANCE hInstLib)
  213. {
  214.     AfxLockGlobals(CRIT_LOCKSHARED);
  215.     BOOL bResult = FreeLibrary(hInstLib);
  216.     AfxUnlockGlobals(CRIT_LOCKSHARED);
  217.     return bResult;
  218. }
  219.  
  220. /////////////////////////////////////////////////////////////////////////////
  221. // Resource helpers
  222.  
  223. #ifdef AFX_CORE2_SEG
  224. #pragma code_seg(AFX_CORE2_SEG)
  225. #endif
  226.  
  227. HINSTANCE AFXAPI AfxFindResourceHandle(LPCTSTR lpszName, LPCTSTR lpszType)
  228. {
  229.     ASSERT(lpszName != NULL);
  230.     ASSERT(lpszType != NULL);
  231.  
  232.     HINSTANCE hInst;
  233.  
  234.     // first check the main module state
  235.     AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  236.     if (!pModuleState->m_bSystem)
  237.     {
  238.         hInst = AfxGetResourceHandle();
  239.         if (::FindResource(hInst, lpszName, lpszType) != NULL)
  240.             return hInst;
  241.     }
  242.  
  243.     // check for non-system DLLs in proper order
  244.     AfxLockGlobals(CRIT_DYNLINKLIST);
  245.     for (CDynLinkLibrary* pDLL = pModuleState->m_libraryList; pDLL != NULL;
  246.         pDLL = pDLL->m_pNextDLL)
  247.     {
  248.         if (!pDLL->m_bSystem && pDLL->m_hResource != NULL &&
  249.             ::FindResource(pDLL->m_hResource, lpszName, lpszType) != NULL)
  250.         {
  251.             // found it in a DLL
  252.             AfxUnlockGlobals(CRIT_DYNLINKLIST);
  253.             return pDLL->m_hResource;
  254.         }
  255.     }
  256.     AfxUnlockGlobals(CRIT_DYNLINKLIST);
  257.  
  258.     // check language specific resource next
  259.     hInst = pModuleState->m_appLangDLL;
  260.     if (hInst != NULL && ::FindResource(hInst, lpszName, lpszType) != NULL)
  261.         return hInst;
  262.  
  263.     // check the main system module state
  264.     if (pModuleState->m_bSystem)
  265.     {
  266.         hInst = AfxGetResourceHandle();
  267.         if (::FindResource(hInst, lpszName, lpszType) != NULL)
  268.             return hInst;
  269.     }
  270.  
  271.     // check for system DLLs in proper order
  272.     AfxLockGlobals(CRIT_DYNLINKLIST);
  273.     for (pDLL = pModuleState->m_libraryList; pDLL != NULL; pDLL = pDLL->m_pNextDLL)
  274.     {
  275.         if (pDLL->m_bSystem && pDLL->m_hResource != NULL &&
  276.             ::FindResource(pDLL->m_hResource, lpszName, lpszType) != NULL)
  277.         {
  278.             // found it in a DLL
  279.             AfxUnlockGlobals(CRIT_DYNLINKLIST);
  280.             return pDLL->m_hResource;
  281.         }
  282.     }
  283.     AfxUnlockGlobals(CRIT_DYNLINKLIST);
  284.  
  285.     // if failed to find resource, return application resource
  286.     return AfxGetResourceHandle();
  287. }
  288.  
  289. // AfxLoadString must not only check for the appropriate string segment
  290. //   in the resource file, but also that the string is non-zero
  291. int AFXAPI AfxLoadString(UINT nID, LPTSTR lpszBuf, UINT nMaxBuf)
  292. {
  293.     ASSERT(AfxIsValidAddress(lpszBuf, nMaxBuf*sizeof(TCHAR)));
  294.  
  295.     LPCTSTR lpszName = MAKEINTRESOURCE((nID>>4)+1);
  296.     HINSTANCE hInst;
  297.     int nLen;
  298.  
  299.     // first check the main module state
  300.     AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  301.     if (!pModuleState->m_bSystem)
  302.     {
  303.         hInst = AfxGetResourceHandle();
  304.         if (::FindResource(hInst, lpszName, RT_STRING) != NULL &&
  305.             (nLen = ::LoadString(hInst, nID, lpszBuf, nMaxBuf)) != 0)
  306.         {
  307.             // found a non-zero string in app
  308.             return nLen;
  309.         }
  310.     }
  311.  
  312.     // check non-system DLLs in proper order
  313.     AfxLockGlobals(CRIT_DYNLINKLIST);
  314.     for (CDynLinkLibrary* pDLL = pModuleState->m_libraryList; pDLL != NULL;
  315.         pDLL = pDLL->m_pNextDLL)
  316.     {
  317.         if (!pDLL->m_bSystem && (hInst = pDLL->m_hResource) != NULL &&
  318.           ::FindResource(hInst, lpszName, RT_STRING) != NULL &&
  319.           (nLen = ::LoadString(hInst, nID, lpszBuf, nMaxBuf)) != 0)
  320.         {
  321.             AfxUnlockGlobals(CRIT_DYNLINKLIST);
  322.             return nLen;
  323.         }
  324.     }
  325.     AfxUnlockGlobals(CRIT_DYNLINKLIST);
  326.  
  327.     // check language specific DLL next
  328.     hInst = pModuleState->m_appLangDLL;
  329.     if (hInst != NULL && ::FindResource(hInst, lpszName, RT_STRING) != NULL &&
  330.         (nLen = ::LoadString(hInst, nID, lpszBuf, nMaxBuf)) != 0)
  331.     {
  332.         // found a non-zero string in language DLL
  333.         return nLen;
  334.     }
  335.  
  336.     // check the system module state
  337.     if (pModuleState->m_bSystem)
  338.     {
  339.         hInst = AfxGetResourceHandle();
  340.         if (::FindResource(hInst, lpszName, RT_STRING) != NULL &&
  341.             (nLen = ::LoadString(hInst, nID, lpszBuf, nMaxBuf)) != 0)
  342.         {
  343.             // found a non-zero string in app
  344.             return nLen;
  345.         }
  346.     }
  347.  
  348.     // check system DLLs in proper order
  349.     AfxLockGlobals(CRIT_DYNLINKLIST);
  350.     for (pDLL = pModuleState->m_libraryList; pDLL != NULL; pDLL = pDLL->m_pNextDLL)
  351.     {
  352.         if (pDLL->m_bSystem && (hInst = pDLL->m_hResource) != NULL &&
  353.           ::FindResource(hInst, lpszName, RT_STRING) != NULL &&
  354.           (nLen = ::LoadString(hInst, nID, lpszBuf, nMaxBuf)) != 0)
  355.         {
  356.             AfxUnlockGlobals(CRIT_DYNLINKLIST);
  357.             return nLen;
  358.         }
  359.     }
  360.     AfxUnlockGlobals(CRIT_DYNLINKLIST);
  361.  
  362.     // did not find it
  363.     lpszBuf[0] = '\0';
  364.     return 0;
  365. }
  366.  
  367. /////////////////////////////////////////////////////////////////////////////
  368. // Library initialization and cleanup
  369.  
  370. #ifdef _DEBUG
  371. #define MSVCRT_DLL "MSVCRTD.DLL"
  372. #else
  373. #define MSVCRT_DLL "MSVCRT.DLL"
  374. #endif
  375.  
  376. #ifdef AFX_INIT_SEG
  377. #pragma code_seg(AFX_INIT_SEG)
  378. #endif
  379.  
  380. extern "C" BOOL WINAPI RawDllMain(HINSTANCE, DWORD dwReason, LPVOID);
  381. extern "C" BOOL (WINAPI* _pRawDllMain)(HINSTANCE, DWORD, LPVOID) = &RawDllMain;
  382.  
  383. extern "C"
  384. BOOL WINAPI RawDllMain(HINSTANCE /*hInstance*/, DWORD dwReason, LPVOID)
  385. {
  386.     if (dwReason == DLL_PROCESS_ATTACH)
  387.     {
  388.         // Prevent the C runtime DLL from being unloaded prematurely
  389.         LoadLibraryA(MSVCRT_DLL);
  390.  
  391. #ifdef _UNICODE
  392.         // give error message and exit if running Unicode on non-Unicode system
  393.         if (GetVersion() & 0x80000000)
  394.         {
  395.             // Note: this message is for programmers who can read english
  396.             ::MessageBoxA(NULL,
  397.                 "This application or DLL can not be loaded "
  398.                 "on Windows 95 or on Windows 3.1.  It takes advantage "
  399.                 "of Unicode features only available on Windows NT.",
  400.                 "MFC Runtime Module", MB_ICONSTOP|MB_OK);
  401.             return FALSE; // and fail
  402.         }
  403. #endif
  404.  
  405.         SetErrorMode(SetErrorMode(0) |
  406.             SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX);
  407.  
  408.         // add a reference to thread local storage data
  409.         AfxTlsAddRef();
  410.  
  411.         // make sure we have enough memory to attempt to start (8kb)
  412.         void* pMinHeap = LocalAlloc(NONZEROLPTR, 0x2000);
  413.         if (pMinHeap == NULL)
  414.             return FALSE;   // fail if memory alloc fails
  415.         LocalFree(pMinHeap);
  416.  
  417.         // cause early initialization of _afxCriticalSection
  418.         if (!AfxCriticalInit())
  419.             return FALSE;
  420.  
  421. #ifdef _AFX_OLE_IMPL
  422.         // set module state before initialization
  423.         AFX_MODULE_STATE* pModuleState = _AfxGetOleModuleState();
  424.         _AFX_THREAD_STATE* pState = AfxGetThreadState();
  425.         pState->m_pPrevModuleState = AfxSetModuleState(pModuleState);
  426. #endif
  427.     }
  428.     else if (dwReason == DLL_PROCESS_DETACH)
  429.     {
  430. #ifdef _AFX_OLE_IMPL
  431.         _AFX_THREAD_STATE* pThreadState = _afxThreadState.GetDataNA();
  432.         if (pThreadState != NULL)
  433.         {
  434.             // restore previously-saved module state
  435.             VERIFY(AfxSetModuleState(pThreadState->m_pPrevModuleState) ==
  436.                 _AfxGetOleModuleState());
  437.             DEBUG_ONLY(pThreadState->m_pPrevModuleState = NULL);
  438.         }
  439. #endif
  440.  
  441.         // free up the _afxCriticalSection
  442.         AfxCriticalTerm();
  443.  
  444.         // Now it's OK for C runtime DLL to be unloaded (see LoadLibrary above)
  445.         FreeLibrary(GetModuleHandleA(MSVCRT_DLL));
  446.  
  447.         // remove reference from thread local data
  448.         AfxTlsRelease();
  449.     }
  450.  
  451.     return TRUE;    // ok
  452. }
  453.  
  454. extern "C"
  455. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID)
  456. {
  457.     if (dwReason == DLL_PROCESS_ATTACH)
  458.     {
  459. #ifdef _AFX_OLE_IMPL
  460.         BOOL bRegister = !coreDLL.bInitialized;
  461.  
  462.         // shared initialization
  463.         AFX_MODULE_STATE* pModuleState = _AfxGetOleModuleState();
  464.         pModuleState->m_hCurrentInstanceHandle = hInstance;
  465.         pModuleState->m_hCurrentResourceHandle = hInstance;
  466.         pModuleState->m_pClassInit = pModuleState->m_classList.GetHead();
  467.         pModuleState->m_pFactoryInit = pModuleState->m_factoryList.GetHead();
  468. #endif
  469.  
  470.         // initialize this DLL's extension module
  471.         VERIFY(AfxInitExtensionModule(coreDLL, hInstance));
  472.  
  473. #ifdef _AFX_OLE_IMPL
  474.         AfxWinInit(hInstance, NULL, &afxChNil, 0);
  475.  
  476.         // Register class factories in context of private module state
  477.         if (bRegister)
  478.             COleObjectFactory::RegisterAll();
  479. #endif
  480.  
  481. #ifdef _AFX_OLE_IMPL
  482.         // restore previously-saved module state
  483.         VERIFY(AfxSetModuleState(AfxGetThreadState()->m_pPrevModuleState) ==
  484.             _AfxGetOleModuleState());
  485.         DEBUG_ONLY(AfxGetThreadState()->m_pPrevModuleState = NULL);
  486. #endif
  487.  
  488.         // wire up this DLL into the resource chain
  489.         CDynLinkLibrary* pDLL = new CDynLinkLibrary(coreDLL, TRUE);
  490.         ASSERT(pDLL != NULL);
  491.         pDLL->m_factoryList.m_pHead = NULL;
  492.  
  493.         // load language specific DLL
  494.         // the DLL must be in the "system directory"
  495.         static const char szPrefix[] = "\\MFC42";
  496.         static const char szLOC[] = "LOC";
  497.         static const char szDLL[] = ".DLL";
  498.         char szLangDLL[_MAX_PATH+14]; // Note: 8.3 name
  499.         GetSystemDirectoryA(szLangDLL, _countof(szLangDLL));
  500.         lstrcatA(szLangDLL, szPrefix);
  501.  
  502.         // try MFC42LOC.DLL
  503.         lstrcatA(szLangDLL, szLOC);
  504.         lstrcatA(szLangDLL, szDLL);
  505.         HINSTANCE hLangDLL = LoadLibraryA(szLangDLL);
  506.         AFX_MODULE_STATE* pState = AfxGetModuleState();
  507.         pState->m_appLangDLL = hLangDLL;
  508.  
  509. #ifdef _AFX_OLE_IMPL
  510.         // copy it to the private OLE state too
  511.         pModuleState->m_appLangDLL = hLangDLL;
  512. #endif
  513.     }
  514.     else if (dwReason == DLL_PROCESS_DETACH)
  515.     {
  516.         // free language specific DLL
  517.         AFX_MODULE_STATE* pState = AfxGetModuleState();
  518.         if (pState->m_appLangDLL != NULL)
  519.         {
  520.             ::FreeLibrary(pState->m_appLangDLL);
  521.             pState->m_appLangDLL = NULL;
  522.         }
  523.  
  524.         // free the DLL info blocks
  525.         CDynLinkLibrary* pDLL;
  526.         while ((pDLL = pState->m_libraryList) != NULL)
  527.             delete pDLL;
  528.         ASSERT(pState->m_libraryList.IsEmpty());
  529.  
  530.         // cleanup module state for this process
  531.         AfxTermExtensionModule(coreDLL);
  532.  
  533. #ifdef _AFX_OLE_IMPL
  534.         // set module state for cleanup
  535.         ASSERT(AfxGetThreadState()->m_pPrevModuleState == NULL);
  536.         AfxGetThreadState()->m_pPrevModuleState =
  537.             AfxSetModuleState(_AfxGetOleModuleState());
  538. #endif
  539.  
  540.         // cleanup module state in OLE private module state
  541.         AfxTermExtensionModule(coreDLL, TRUE);
  542.  
  543.         // free any local data for this process/thread
  544.         AfxTermLocalData(NULL, TRUE);
  545.     }
  546.     else if (dwReason == DLL_THREAD_DETACH)
  547.     {
  548.         AfxTermThread();
  549.     }
  550.  
  551.     return TRUE;    // ok
  552. }
  553.  
  554. ////////////////////////////////////////////////////////////////////////////
  555. // Special initialization entry point for controls
  556.  
  557. void AFXAPI AfxCoreInitModule()
  558. {
  559.     ASSERT(AfxGetModuleState() != AfxGetAppModuleState());
  560.  
  561.     // construct new dynlink library in this context for core resources
  562.     CDynLinkLibrary* pDLL = new CDynLinkLibrary(coreDLL, TRUE);
  563.     ASSERT(pDLL != NULL);
  564.     pDLL->m_factoryList.m_pHead = NULL;
  565.  
  566.     // borrow resources from language specific DLL if loaded
  567.     AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  568.     AFX_MODULE_STATE* pAppState = AfxGetAppModuleState();
  569.     if (pModuleState->m_appLangDLL == NULL)
  570.         pModuleState->m_appLangDLL = pAppState->m_appLangDLL;
  571. }
  572.  
  573. ////////////////////////////////////////////////////////////////////////////
  574. // COM entry points
  575.  
  576. #ifdef _AFX_OLE_IMPL
  577. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  578. {
  579.     AFX_MANAGE_STATE(_AfxGetOleModuleState());
  580.     return AfxDllGetClassObject(rclsid, riid, ppv);
  581. }
  582.  
  583. STDAPI DllCanUnloadNow(void)
  584. {
  585.     AFX_MANAGE_STATE(_AfxGetOleModuleState());
  586.     return AfxDllCanUnloadNow();
  587. }
  588.  
  589. ////////////////////////////////////////////////////////////////////////////
  590. // Server registration
  591.  
  592. STDAPI DllRegisterServer(void)
  593. {
  594.     AFX_MANAGE_STATE(_AfxGetOleModuleState());
  595.  
  596.     if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
  597.         return SELFREG_E_CLASS;
  598.  
  599.     return S_OK;
  600. }
  601.  
  602. STDAPI DllUnregisterServer(void)
  603. {
  604.     AFX_MANAGE_STATE(_AfxGetOleModuleState());
  605.  
  606.     if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))
  607.         return SELFREG_E_CLASS;
  608.  
  609.     return S_OK;
  610. }
  611.  
  612. // This CWinApp is required so this module state has a CWinApp object!
  613. CWinApp _afxOleWinApp;
  614.  
  615. /////////////////////////////////////////////////////////////////////////////
  616. // static-linked version of AfxWndProc for use by this module
  617.  
  618. #undef AfxWndProc
  619.  
  620. LRESULT CALLBACK
  621. AfxWndProcDllOle(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  622. {
  623.     AFX_MANAGE_STATE(_AfxGetOleModuleState());
  624.     return AfxWndProc(hWnd, nMsg, wParam, lParam);
  625. }
  626.  
  627. /////////////////////////////////////////////////////////////////////////////
  628.  
  629. // force initialization early
  630. #pragma warning(disable: 4074)
  631. #pragma init_seg(lib)
  632.  
  633. static AFX_MODULE_STATE _afxOleModuleState(TRUE, &AfxWndProcDllOle,
  634.     _MFC_VER, TRUE);
  635.  
  636. AFX_MODULE_STATE* AFXAPI _AfxGetOleModuleState()
  637. {
  638.     return &_afxOleModuleState;
  639. }
  640. #endif
  641.  
  642. /////////////////////////////////////////////////////////////////////////////
  643. // Special code to wire up vector deleting destructors
  644.  
  645. #ifdef AFX_VDEL_SEG
  646. #pragma code_seg(AFX_VDEL_SEG)
  647. #endif
  648. static void _AfxForceVectorDelete()
  649. {
  650. #ifdef _DEBUG
  651.     ASSERT(FALSE);  // never called
  652. #endif
  653.  
  654.     new CBitmap[2];
  655.     new CBitmapButton[2];
  656.     new CBrush[2];
  657.     new CButton[2];
  658.     new CByteArray[2];
  659.     new CCmdTarget[2];
  660.     new CComboBox[2];
  661.     new CDC[2];
  662.     new CDWordArray[2];
  663.     new CDialog[2];
  664.     new CDialogBar[2];
  665.     new CEdit[2];
  666.     new CFile[2];
  667.     new CFont[2];
  668.     new CFrameWnd[2];
  669.     new CGdiObject[2];
  670.     new CListBox[2];
  671.     new CCheckListBox[2];
  672.     new CMapPtrToPtr[2];
  673.     new CMapPtrToWord[2];
  674.     new CMapStringToOb[2];
  675.     new CMapStringToPtr[2];
  676.     new CMapStringToString[2];
  677.     new CMapWordToOb[2];
  678.     new CMapWordToPtr[2];
  679.     new CMemFile[2];
  680.     new CMenu[2];
  681.     new CMetaFileDC[2];
  682.     new CObArray[2];
  683.     new CObList[2];
  684.     new CPalette[2];
  685.     new CPen[2];
  686.     new CPtrArray[2];
  687.     new CPtrList[2];
  688.     new CRectTracker[2];
  689.     new CRgn[2];
  690.     new CScrollBar[2];
  691.     new CSharedFile[2];
  692.     new CSplitterWnd[2];
  693.     new CStatic[2];
  694.     new CStatusBar[2];
  695.     new CStdioFile[2];
  696.     new CString[2];
  697.     new CStringArray[2];
  698.     new CStringList[2];
  699.     new CThreadSlotData[2];
  700.     new CTime[2];
  701.     new CTimeSpan[2];
  702.     new CToolBar[2];
  703.     new CUIntArray[2];
  704.     new CWnd[2];
  705.     new CWordArray[2];
  706.  
  707.     new CFileFind[2];
  708.     new CInternetSession[2];
  709.  
  710.     new CDragListBox[2];
  711.     new CStatusBarCtrl[2];
  712.     new CListCtrl[2];
  713.     new CTreeCtrl[2];
  714.     new CSpinButtonCtrl[2];
  715.     new CSliderCtrl[2];
  716.     new CProgressCtrl[2];
  717.     new CHeaderCtrl[2];
  718.     new CHotKeyCtrl[2];
  719.     new CToolTipCtrl[2];
  720.     new CTabCtrl[2];
  721.     new CAnimateCtrl[2];
  722.     new CImageList[2];
  723.     new CToolBarCtrl[2];
  724.     new CRichEditCtrl[2];
  725.  
  726.     new CMirrorFile[2];
  727.     new CDockState[2];
  728.  
  729.     new CListView[2];
  730.     new CTreeView[2];
  731.     new CCommandLineInfo[2];
  732.     new CDocManager[2];
  733.  
  734.     new CPageSetupDialog[2];
  735.  
  736.     new CSemaphore[2];
  737.     new CMutex[2];
  738.     new CEvent[2];
  739.     new CCriticalSection[2];
  740.  
  741. #ifdef _AFX_OLE_IMPL
  742.     new COleDataSource[2];
  743.     new COleDispatchDriver[2];
  744.     new COleDropSource[2];
  745.     new CMonikerFile[2];
  746.     new COleResizeBar[2];
  747.     new CAsyncMonikerFile[2];
  748.     new CCachedDataPathProperty[2];
  749.     new CDataPathProperty[2];
  750.     new COleStreamFile[2];
  751.     new COleTemplateServer[2];
  752.     new COleDataObject[2];
  753.     new COleDropTarget[2];
  754.     new COleIPFrameWnd[2];
  755.  
  756.     new COleDocIPFrameWnd[2];
  757.  
  758.     new COleVariant[2];
  759.     new CRichEditView[2];
  760.     new CRichEditCntrItem[2];
  761. #endif
  762.  
  763. #ifdef _AFX_DB_IMPL
  764.     new CDatabase[2];
  765.     new CLongBinary[2];
  766.  
  767.     new CDaoWorkspace[2];
  768.     new CDaoException[2];
  769.     new CDaoDatabase[2];
  770.     new CDaoRecordset[2];
  771. #endif
  772.  
  773. // Net
  774. #ifdef _AFX_NET_IMPL
  775.     new CAsyncSocket[2];
  776.     new CSocket[2];
  777. #endif
  778.  
  779. }
  780. void (*_afxForceVectorDelete_mfc)() = &_AfxForceVectorDelete;
  781.  
  782. void _AfxBinaryCompatibleStubFunction()
  783. {
  784.     ASSERT(FALSE);
  785. }
  786.  
  787. #ifndef _UNICODE
  788. HINSTANCE AFXAPI AfxGetResourceHandleCompat()
  789. {
  790.     return AfxGetResourceHandle();
  791. }
  792. #endif
  793.  
  794. /////////////////////////////////////////////////////////////////////////////
  795.