dllinit.cpp 21 KB

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