Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.4 KiB

  1. /*---------------------------------------------------------------------------
  2. File: Dispatcher.cpp
  3. Comments: COM server implementation for dispatcher, generated by ATL.
  4. (c) Copyright 1999, Mission Critical Software, Inc., All Rights Reserved
  5. Proprietary and confidential to Mission Critical Software, Inc.
  6. REVISION LOG ENTRY
  7. Revision By: Christy Boles
  8. Revised on 02/18/99 11:34:16
  9. ---------------------------------------------------------------------------
  10. */// Dispatcher.cpp : Implementation of WinMain
  11. // Note: Proxy/Stub Information
  12. // To build a separate proxy/stub DLL,
  13. // run nmake -f Dispatcherps.mk in the project directory.
  14. #include "stdafx.h"
  15. #include "resource.h"
  16. #include <initguid.h>
  17. //#include "..\Common\Include\McsDispatcher.h"
  18. #include "Dispatch.h"
  19. //#include "McsDispatcher_i.c"
  20. #include "Dispatch_i.c"
  21. #include "DDisp.h"
  22. #include "DInst.h"
  23. #include <MigrationMutex.h>
  24. const DWORD dwTimeOut = 5000; // time for EXE to be idle before shutting down
  25. const DWORD dwPause = 1000; // time to wait for threads to finish up
  26. // Passed to CreateThread to monitor the shutdown event
  27. static DWORD WINAPI MonitorProc(void* pv)
  28. {
  29. CExeModule* p = (CExeModule*)pv;
  30. p->MonitorShutdown();
  31. return 0;
  32. }
  33. LONG CExeModule::Unlock()
  34. {
  35. LONG l = CComModule::Unlock();
  36. if (l == 0)
  37. {
  38. bActivity = true;
  39. SetEvent(hEventShutdown); // tell monitor that we transitioned to zero
  40. }
  41. return l;
  42. }
  43. //Monitors the shutdown event
  44. void CExeModule::MonitorShutdown()
  45. {
  46. while (1)
  47. {
  48. WaitForSingleObject(hEventShutdown, INFINITE);
  49. DWORD dwWait=0;
  50. do
  51. {
  52. bActivity = false;
  53. dwWait = WaitForSingleObject(hEventShutdown, dwTimeOut);
  54. } while (dwWait == WAIT_OBJECT_0);
  55. // timed out
  56. if (!bActivity && m_nLockCnt == 0) // if no activity let's really bail
  57. {
  58. #if _WIN32_WINNT >= 0x0400 & defined(_ATL_FREE_THREADED)
  59. CoSuspendClassObjects();
  60. if (!bActivity && m_nLockCnt == 0)
  61. #endif
  62. break;
  63. }
  64. }
  65. CloseHandle(hEventShutdown);
  66. PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
  67. }
  68. bool CExeModule::StartMonitor()
  69. {
  70. bool bCreated = false;
  71. hEventShutdown = CreateEvent(NULL, false, false, NULL);
  72. if (hEventShutdown == NULL)
  73. return false;
  74. DWORD dwThreadID;
  75. HANDLE h = CreateThread(NULL, 0, MonitorProc, this, 0, &dwThreadID);
  76. if (h != NULL)
  77. bCreated = true;
  78. CloseHandle(h);
  79. return bCreated;
  80. }
  81. CExeModule _Module;
  82. BEGIN_OBJECT_MAP(ObjectMap)
  83. OBJECT_ENTRY(CLSID_DCTDispatcher, CDCTDispatcher)
  84. OBJECT_ENTRY(CLSID_DCTInstaller, CDCTInstaller)
  85. END_OBJECT_MAP()
  86. LPCTSTR FindOneOf(LPCTSTR p1, LPCTSTR p2)
  87. {
  88. while (p1 != NULL && *p1 != NULL)
  89. {
  90. LPCTSTR p = p2;
  91. while (p != NULL && *p != NULL)
  92. {
  93. if (*p1 == *p)
  94. return CharNext(p1);
  95. p = CharNext(p);
  96. }
  97. p1 = CharNext(p1);
  98. }
  99. return NULL;
  100. }
  101. /////////////////////////////////////////////////////////////////////////////
  102. //
  103. extern "C" int WINAPI _tWinMain(HINSTANCE hInstance,
  104. HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int /*nShowCmd*/)
  105. {
  106. ATLTRACE(_T("{McsDispatcher.exe}_tWinMain(hInstance=0x%08lX,...)\n"), hInstance);
  107. // obtain dispatcher mutex
  108. // the migration driver uses this mutex to determine
  109. // if the dispatcher process is currently running
  110. CMigrationMutex mutex(DISPATCHER_MUTEX, true);
  111. // set debug flags to check memory allocations and leaks
  112. _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF);
  113. lpCmdLine = GetCommandLine(); //this line necessary for _ATL_MIN_CRT
  114. #if _WIN32_WINNT >= 0x0400 & defined(_ATL_FREE_THREADED)
  115. HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  116. #else
  117. // HRESULT hRes = CoInitialize(NULL);
  118. #endif
  119. _ASSERTE(SUCCEEDED(hRes));
  120. _Module.Init(ObjectMap, hInstance, &LIBID_MCSDISPATCHERLib);
  121. _Module.dwThreadID = GetCurrentThreadId();
  122. TCHAR szTokens[] = _T("-/");
  123. int nRet = 0;
  124. BOOL bRun = TRUE;
  125. LPCTSTR lpszToken = FindOneOf(lpCmdLine, szTokens);
  126. while (lpszToken != NULL)
  127. {
  128. if (lstrcmpi(lpszToken, _T("UnregServer"))==0)
  129. {
  130. _Module.UpdateRegistryFromResource(IDR_Dispatcher, FALSE);
  131. nRet = _Module.UnregisterServer(TRUE);
  132. bRun = FALSE;
  133. break;
  134. }
  135. if (lstrcmpi(lpszToken, _T("RegServer"))==0)
  136. {
  137. _Module.UpdateRegistryFromResource(IDR_Dispatcher, TRUE);
  138. nRet = _Module.RegisterServer(TRUE);
  139. bRun = FALSE;
  140. break;
  141. }
  142. lpszToken = FindOneOf(lpszToken, szTokens);
  143. }
  144. if (bRun)
  145. {
  146. _Module.StartMonitor();
  147. #if _WIN32_WINNT >= 0x0400 & defined(_ATL_FREE_THREADED)
  148. hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER,
  149. REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED);
  150. _ASSERTE(SUCCEEDED(hRes));
  151. hRes = CoResumeClassObjects();
  152. #else
  153. hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER,
  154. REGCLS_MULTIPLEUSE);
  155. #endif
  156. _ASSERTE(SUCCEEDED(hRes));
  157. MSG msg;
  158. while (GetMessage(&msg, 0, 0, 0))
  159. DispatchMessage(&msg);
  160. _Module.RevokeClassObjects();
  161. Sleep(dwPause); //wait for any threads to finish
  162. }
  163. ATLTRACE(_T("{McsDispatcher.exe}_tWinMain() : hInstance=0x%08lX\n"), hInstance);
  164. _Module.Term();
  165. CoUninitialize();
  166. return nRet;
  167. }