dllmodul.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 _DEBUG
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. #define new DEBUG_NEW
  16. /////////////////////////////////////////////////////////////////////////////
  17. // global data
  18. // The following symbol used to force inclusion of this module for _USRDLL
  19. #ifdef _X86_
  20. extern "C" { int _afxForceUSRDLL; }
  21. #else
  22. extern "C" { int __afxForceUSRDLL; }
  23. #endif
  24. #ifdef _AFXDLL
  25. static AFX_EXTENSION_MODULE controlDLL;
  26. // force initialization early
  27. #pragma warning(disable: 4074)
  28. #pragma init_seg(lib)
  29. /////////////////////////////////////////////////////////////////////////////
  30. // static-linked version of AfxWndProc for use by this module
  31. LRESULT CALLBACK AfxWndProcDllStatic(HWND, UINT, WPARAM, LPARAM);
  32. class _AFX_DLL_MODULE_STATE : public AFX_MODULE_STATE
  33. {
  34. public:
  35. _AFX_DLL_MODULE_STATE() : AFX_MODULE_STATE(TRUE, AfxWndProcDllStatic, _MFC_VER)
  36. { }
  37. };
  38. static _AFX_DLL_MODULE_STATE afxModuleState;
  39. #undef AfxWndProc
  40. LRESULT CALLBACK
  41. AfxWndProcDllStatic(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  42. {
  43. AFX_MANAGE_STATE(&afxModuleState);
  44. return AfxWndProc(hWnd, nMsg, wParam, lParam);
  45. }
  46. AFX_MODULE_STATE* AFXAPI AfxGetStaticModuleState()
  47. {
  48. AFX_MODULE_STATE* pModuleState = &afxModuleState;
  49. return pModuleState;
  50. }
  51. #endif
  52. /////////////////////////////////////////////////////////////////////////////
  53. // export DllMain for the DLL
  54. extern "C"
  55. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
  56. {
  57. if (dwReason == DLL_PROCESS_ATTACH)
  58. {
  59. BOOL bResult = FALSE;
  60. #ifdef _AFXDLL
  61. // wire up resources from core DLL
  62. AfxCoreInitModule();
  63. #endif
  64. _AFX_THREAD_STATE* pState = AfxGetThreadState();
  65. AFX_MODULE_STATE* pPrevModState = pState->m_pPrevModuleState;
  66. // Initialize DLL's instance(/module) not the app's
  67. if (!AfxWinInit(hInstance, NULL, _T(""), 0))
  68. {
  69. AfxWinTerm();
  70. goto Cleanup; // Init Failed
  71. }
  72. // initialize the single instance DLL
  73. CWinApp* pApp; pApp = AfxGetApp();
  74. if (pApp != NULL && !pApp->InitInstance())
  75. {
  76. pApp->ExitInstance();
  77. AfxWinTerm();
  78. goto Cleanup; // Init Failed
  79. }
  80. pState->m_pPrevModuleState = pPrevModState;
  81. #ifdef _AFXDLL
  82. // wire up this DLL into the resource chain
  83. VERIFY(AfxInitExtensionModule(controlDLL, hInstance));
  84. CDynLinkLibrary* pDLL; pDLL = new CDynLinkLibrary(controlDLL);
  85. ASSERT(pDLL != NULL);
  86. #else
  87. AfxInitLocalData(hInstance);
  88. #endif
  89. bResult = TRUE;
  90. Cleanup:
  91. pState->m_pPrevModuleState = pPrevModState;
  92. #ifdef _AFXDLL
  93. // restore previously-saved module state
  94. VERIFY(AfxSetModuleState(AfxGetThreadState()->m_pPrevModuleState) ==
  95. &afxModuleState);
  96. DEBUG_ONLY(AfxGetThreadState()->m_pPrevModuleState = NULL);
  97. #endif
  98. return bResult;
  99. }
  100. else if (dwReason == DLL_PROCESS_DETACH)
  101. {
  102. #ifdef _AFXDLL
  103. // set module state for cleanup
  104. ASSERT(AfxGetThreadState()->m_pPrevModuleState == NULL);
  105. AfxGetThreadState()->m_pPrevModuleState =
  106. AfxSetModuleState(&afxModuleState);
  107. #endif
  108. CWinApp* pApp = AfxGetApp();
  109. if (pApp != NULL)
  110. pApp->ExitInstance();
  111. #ifdef _DEBUG
  112. // check for missing AfxLockTempMap calls
  113. if (AfxGetModuleThreadState()->m_nTempMapLock != 0)
  114. {
  115. TRACE1("Warning: Temp map lock count non-zero (%ld).\n",
  116. AfxGetModuleThreadState()->m_nTempMapLock);
  117. }
  118. #endif
  119. AfxLockTempMaps();
  120. AfxUnlockTempMaps(-1);
  121. // terminate the library before destructors are called
  122. AfxWinTerm();
  123. #ifdef _AFXDLL
  124. AfxTermExtensionModule(controlDLL, TRUE);
  125. #else
  126. AfxTermLocalData(hInstance, TRUE);
  127. #endif
  128. }
  129. else if (dwReason == DLL_THREAD_DETACH)
  130. {
  131. AFX_MANAGE_STATE(&afxModuleState);
  132. #ifdef _DEBUG
  133. // check for missing AfxLockTempMap calls
  134. if (AfxGetModuleThreadState()->m_nTempMapLock != 0)
  135. {
  136. TRACE1("Warning: Temp map lock count non-zero (%ld).\n",
  137. AfxGetModuleThreadState()->m_nTempMapLock);
  138. }
  139. #endif
  140. AfxLockTempMaps();
  141. AfxUnlockTempMaps(-1);
  142. AfxTermThread(hInstance);
  143. }
  144. return TRUE;
  145. }
  146. #ifdef _AFXDLL
  147. /////////////////////////////////////////////////////////////////////////////
  148. // initialize app state such that it points to this module's core state
  149. extern "C" BOOL WINAPI RawDllMain(HINSTANCE, DWORD dwReason, LPVOID);
  150. extern "C" BOOL (WINAPI* _pRawDllMain)(HINSTANCE, DWORD, LPVOID) = &RawDllMain;
  151. extern "C"
  152. BOOL WINAPI RawDllMain(HINSTANCE, DWORD dwReason, LPVOID)
  153. {
  154. if (dwReason == DLL_PROCESS_ATTACH)
  155. {
  156. // make sure we have enough memory to attempt to start (8kb)
  157. void* pMinHeap = LocalAlloc(NONZEROLPTR, 0x2000);
  158. if (pMinHeap == NULL)
  159. return FALSE; // fail if memory alloc fails
  160. LocalFree(pMinHeap);
  161. #ifndef _AFXDLL
  162. if (!AfxCriticalInit())
  163. return FALSE;
  164. #endif
  165. // set module state before initialization
  166. _AFX_THREAD_STATE* pState = AfxGetThreadState();
  167. pState->m_pPrevModuleState = AfxSetModuleState(&afxModuleState);
  168. }
  169. else if (dwReason == DLL_PROCESS_DETACH)
  170. {
  171. // restore module state after cleanup
  172. _AFX_THREAD_STATE* pState = AfxGetThreadState();
  173. VERIFY(AfxSetModuleState(pState->m_pPrevModuleState) ==
  174. &afxModuleState);
  175. DEBUG_ONLY(pState->m_pPrevModuleState = NULL);
  176. #ifndef _AFXDLL
  177. AfxCriticalTerm();
  178. #endif
  179. }
  180. return TRUE;
  181. }
  182. #endif //_AFXDLL
  183. /////////////////////////////////////////////////////////////////////////////
  184. // Special case for static library startup/termination
  185. #ifndef _AFXDLL
  186. // force initialization early
  187. #pragma warning(disable: 4074)
  188. #pragma init_seg(lib)
  189. void AFX_CDECL AfxTermDllState()
  190. {
  191. // terminate local data and critical sections
  192. AfxTermLocalData(NULL, TRUE);
  193. AfxCriticalTerm();
  194. // release the reference to thread local storage data
  195. AfxTlsRelease();
  196. }
  197. char _afxTermDllState = (char)(AfxTlsAddRef(), atexit(&AfxTermDllState));
  198. #endif // !_AFXDLL
  199. /////////////////////////////////////////////////////////////////////////////