dllole.cpp 7.8 KB

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