home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / DLLOLE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  7.7 KB  |  281 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1997 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. AFX_MODULE_STATE* AFXAPI _AfxGetOleModuleState();
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // Library initialization and cleanup
  30.  
  31. extern "C" BOOL WINAPI RawDllMain(HINSTANCE, DWORD dwReason, LPVOID);
  32. extern "C" BOOL (WINAPI* _pRawDllMain)(HINSTANCE, DWORD, LPVOID) = &RawDllMain;
  33.  
  34. #ifdef _DEBUG
  35. #ifndef _UNICODE
  36. #define MFC42_DLL "MFC42D.DLL"
  37. #else
  38. #define MFC42_DLL "MFC42UD.DLL"
  39. #endif
  40. #else
  41. #ifndef _UNICODE
  42. #define MFC42_DLL "MFC42.DLL"
  43. #else
  44. #define MFC42_DLL "MFC42U.DLL"
  45. #endif
  46. #endif
  47.  
  48. extern "C"
  49. BOOL WINAPI RawDllMain(HINSTANCE, DWORD dwReason, LPVOID)
  50. {
  51.     if (dwReason == DLL_PROCESS_ATTACH)
  52.     {
  53. #ifndef _MAC
  54.         // Prevent the MFC DLL from being unloaded prematurely
  55.         LoadLibraryA(MFC42_DLL);
  56. #endif
  57.  
  58.         // make sure we have enough memory to attempt to start (8kb)
  59.         void* pMinHeap = LocalAlloc(NONZEROLPTR, 0x2000);
  60.         if (pMinHeap == NULL)
  61.             return FALSE;   // fail if memory alloc fails
  62.         LocalFree(pMinHeap);
  63.  
  64.         // save critical data pointers before running the constructors
  65.         AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  66.         pModuleState->m_pClassInit = pModuleState->m_classList;
  67.         pModuleState->m_pFactoryInit = pModuleState->m_factoryList;
  68.         pModuleState->m_classList.m_pHead = NULL;
  69.         pModuleState->m_factoryList.m_pHead = NULL;
  70.  
  71.         // set module state before initialization
  72.         pModuleState = _AfxGetOleModuleState();
  73.         _AFX_THREAD_STATE* pState = AfxGetThreadState();
  74.         pState->m_pPrevModuleState = AfxSetModuleState(pModuleState);
  75.     }
  76.     else if (dwReason == DLL_PROCESS_DETACH)
  77.     {
  78.         // restore previously-saved module state
  79.         VERIFY(AfxSetModuleState(AfxGetThreadState()->m_pPrevModuleState) ==
  80.             _AfxGetOleModuleState());
  81.         DEBUG_ONLY(AfxGetThreadState()->m_pPrevModuleState = NULL);
  82.  
  83. #ifndef _MAC
  84.         // Now it's OK for the MFC  DLL to be unloaded (see LoadLibrary above)
  85.         FreeLibrary(GetModuleHandleA(MFC42_DLL));
  86. #endif
  87.     }
  88.     return TRUE;    // ok
  89. }
  90.  
  91. extern "C"
  92. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
  93. {
  94.     if (dwReason == DLL_PROCESS_ATTACH)
  95.     {
  96.         // shared initialization
  97.         BOOL bRegister = !extensionDLL.bInitialized;
  98.         AFX_MODULE_STATE* pModuleState = _AfxGetOleModuleState();
  99.         pModuleState->m_hCurrentInstanceHandle = hInstance;
  100.         pModuleState->m_hCurrentResourceHandle = hInstance;
  101.         pModuleState->m_pClassInit = pModuleState->m_classList.GetHead();
  102.         pModuleState->m_pFactoryInit = pModuleState->m_factoryList.GetHead();
  103.         VERIFY(AfxInitExtensionModule(extensionDLL, hInstance));
  104.  
  105.         // wire up base DLL class list into private module state
  106.         AfxCoreInitModule();
  107.  
  108.         AfxWinInit(hInstance, NULL, &afxChNil, 0);
  109.  
  110.         // Register class factories in context of private module state
  111.         if (bRegister)
  112.             COleObjectFactory::RegisterAll();
  113.  
  114.         // restore previously-saved module state
  115.         VERIFY(AfxSetModuleState(AfxGetThreadState()->m_pPrevModuleState) ==
  116.             _AfxGetOleModuleState());
  117.         DEBUG_ONLY(AfxGetThreadState()->m_pPrevModuleState = NULL);
  118.  
  119.         // wire up this DLL into the base module state resource chain
  120.         CDynLinkLibrary* pDLL = new CDynLinkLibrary(extensionDLL, TRUE);
  121.         ASSERT(pDLL != NULL);
  122.         pDLL->m_factoryList.m_pHead = NULL;
  123.     }
  124.     else if (dwReason == DLL_PROCESS_DETACH)
  125.     {
  126.         // cleanup module state in base module state
  127.         AfxTermExtensionModule(extensionDLL);
  128.  
  129.         // set module state for cleanup
  130.         ASSERT(AfxGetThreadState()->m_pPrevModuleState == NULL);
  131.         AfxGetThreadState()->m_pPrevModuleState =
  132.             AfxSetModuleState(_AfxGetOleModuleState());
  133.  
  134.         // cleanup module state in OLE private module state
  135.         AfxTermExtensionModule(extensionDLL, TRUE);
  136.     }
  137.     else if (dwReason == DLL_THREAD_DETACH)
  138.     {
  139.         AfxTermThread(hInstance);
  140.     }
  141.  
  142.     return TRUE;    // ok
  143. }
  144.  
  145. ////////////////////////////////////////////////////////////////////////////
  146. // Special initialization entry point for controls
  147.  
  148. void AFXAPI AfxOleInitModule()
  149. {
  150.     ASSERT(AfxGetModuleState() != AfxGetAppModuleState());
  151.  
  152.     CDynLinkLibrary* pDLL = new CDynLinkLibrary(extensionDLL, TRUE);
  153.     ASSERT(pDLL != NULL);
  154.     pDLL->m_factoryList.m_pHead = NULL;
  155. }
  156.  
  157. ////////////////////////////////////////////////////////////////////////////
  158. // COM entry points
  159.  
  160. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  161. {
  162.     AFX_MANAGE_STATE(_AfxGetOleModuleState());
  163.     return AfxDllGetClassObject(rclsid, riid, ppv);
  164. }
  165.  
  166. STDAPI DllCanUnloadNow(void)
  167. {
  168.     AFX_MANAGE_STATE(_AfxGetOleModuleState());
  169.     return AfxDllCanUnloadNow();
  170. }
  171.  
  172. ////////////////////////////////////////////////////////////////////////////
  173. // Server registration
  174.  
  175. STDAPI DllRegisterServer(void)
  176. {
  177.     AFX_MANAGE_STATE(_AfxGetOleModuleState());
  178.  
  179.     if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
  180.         return SELFREG_E_CLASS;
  181.  
  182.     return S_OK;
  183. }
  184.  
  185. STDAPI DllUnregisterServer(void)
  186. {
  187.     AFX_MANAGE_STATE(_AfxGetOleModuleState());
  188.  
  189.     if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))
  190.         return SELFREG_E_CLASS;
  191.  
  192.     return S_OK;
  193. }
  194.  
  195. // This CWinApp is required so this module state has a CWinApp object!
  196. CWinApp _afxOleWinApp;
  197.  
  198. /////////////////////////////////////////////////////////////////////////////
  199. // static-linked version of AfxWndProc for use by this module
  200.  
  201. #undef AfxWndProc
  202.  
  203. LRESULT CALLBACK
  204. AfxWndProcDllOle(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  205. {
  206.     AFX_MANAGE_STATE(_AfxGetOleModuleState());
  207.     return AfxWndProc(hWnd, nMsg, wParam, lParam);
  208. }
  209.  
  210. /////////////////////////////////////////////////////////////////////////////
  211. // initialize app state such that it points to this module's core state
  212.  
  213. #ifndef _AFX_NO_OCC_SUPPORT
  214. class _AFX_INIT_OLE_DLL_STATE
  215. {
  216. public:
  217.     ~_AFX_INIT_OLE_DLL_STATE();
  218. };
  219.  
  220. _AFX_INIT_OLE_DLL_STATE::~_AFX_INIT_OLE_DLL_STATE()
  221. {
  222.     // OLE dll is not loaded any more, so no OLE controls capability
  223.     afxOccManager = NULL;
  224. }
  225. #endif
  226.  
  227. /////////////////////////////////////////////////////////////////////////////
  228.  
  229. // force initialization early
  230. #pragma warning(disable: 4074)
  231. #pragma init_seg(lib)
  232.  
  233. static AFX_MODULE_STATE _afxOleModuleState(TRUE, &AfxWndProcDllOle,
  234.     _MFC_VER, TRUE);
  235. #ifndef _AFX_NO_OCC_SUPPORT
  236. _AFX_INIT_OLE_DLL_STATE _afxInitOleDllState;
  237. #endif
  238.  
  239. AFX_MODULE_STATE* AFXAPI _AfxGetOleModuleState()
  240. {
  241.     return &_afxOleModuleState;
  242. }
  243.  
  244. /////////////////////////////////////////////////////////////////////////////
  245.  
  246. #ifdef AFX_VDEL_SEG
  247. #pragma code_seg(AFX_VDEL_SEG)
  248. #endif
  249. static void _AfxForceVectorDelete()
  250. {
  251.     ASSERT(FALSE);  // never called
  252.  
  253.     new COleDataSource[2];
  254.     new COleDispatchDriver[2];
  255.     new COleDropSource[2];
  256. #ifndef _MAC
  257.     new COleResizeBar[2];
  258. #endif
  259.     new COleStreamFile[2];
  260.     new CMonikerFile[2];
  261.     new COleTemplateServer[2];
  262.     new COleDataObject[2];
  263.     new COleDropTarget[2];
  264.     new COleIPFrameWnd[2];
  265.  
  266. #ifndef _MAC
  267.     new COleDocIPFrameWnd[2];
  268.     new CAsyncMonikerFile[2];
  269.     new CCachedDataPathProperty[2];
  270.     new CDataPathProperty[2];
  271. #endif
  272.  
  273.     new COleVariant[2];
  274.  
  275.     new CRichEditView[2];
  276.     new CRichEditCntrItem[2];
  277. }
  278. void (*_afxForceVectorDelete_mfco)() = &_AfxForceVectorDelete;
  279.  
  280. /////////////////////////////////////////////////////////////////////////////
  281.