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