Leaked source code of windows server 2003
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.

214 lines
6.4 KiB

  1. #include <windows.h>
  2. #include <stdlib.h>
  3. #include <fusenetincludes.h>
  4. #include "CUnknown.h"
  5. #include "CFactory.h"
  6. #include "Resource.h"
  7. #include <update.h>
  8. #include "regdb.h"
  9. HWND g_hwndUpdateServer = NULL ;
  10. CRITICAL_SECTION g_csServer;
  11. // Signal that an update is available.
  12. extern BOOL g_fSignalUpdate;
  13. BOOL InitWindow(int nCmdShow) ;
  14. extern "C" LRESULT APIENTRY MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) ;
  15. //-----------------------------------------------------------------------------
  16. // WinMain
  17. // The main entry point via CoCreate or CreateProcess.
  18. //-----------------------------------------------------------------------------
  19. extern "C" int WINAPI WinMain(HINSTANCE hInstance,
  20. HINSTANCE hPrevInstance,
  21. LPSTR lpCmdLine,
  22. int nCmdShow)
  23. {
  24. HRESULT hr = S_OK;
  25. MAKE_ERROR_MACROS_STATIC(hr);
  26. // Initialize the COM Library.
  27. IF_FAILED_EXIT(CoInitializeEx(NULL, COINIT_MULTITHREADED));
  28. __try
  29. {
  30. ::InitializeCriticalSection(&g_csServer);
  31. }
  32. __except (GetExceptionCode() == STATUS_NO_MEMORY ?
  33. EXCEPTION_EXECUTE_HANDLER :
  34. EXCEPTION_CONTINUE_SEARCH )
  35. {
  36. hr = E_OUTOFMEMORY;
  37. }
  38. IF_FAILED_EXIT(hr);
  39. // Get Thread ID.
  40. CFactory::s_dwThreadID = ::GetCurrentThreadId() ;
  41. CFactory::s_hModule = hInstance ;
  42. IF_WIN32_FALSE_EXIT(InitWindow(SW_HIDE));
  43. // Increment artificial server lock.
  44. ::InterlockedIncrement(&CFactory::s_cServerLocks) ;
  45. // clean-up the jobs left out from previous login.
  46. IF_FAILED_EXIT(ProcessOrphanedJobs());
  47. // Initialize the subscription list and timers from registry.
  48. IF_FAILED_EXIT(CAssemblyUpdate::InitializeSubscriptions());
  49. // Register all of the class factories.
  50. IF_FAILED_EXIT(CFactory::StartFactories());
  51. // Wait for shutdown.
  52. MSG msg ;
  53. while (::GetMessage(&msg, 0, 0, 0))
  54. {
  55. ::DispatchMessage(&msg) ;
  56. }
  57. // Unregister the class factories.
  58. // BUGBUG - use the critsect instead
  59. // for race condition.
  60. // The check here is because StopFactories
  61. // will have already been called if an update
  62. // is signalled.
  63. if (!g_fSignalUpdate)
  64. CFactory::StopFactories() ;
  65. ::DeleteCriticalSection(&g_csServer);
  66. exit:
  67. return SUCCEEDED(hr) ? TRUE : FALSE;
  68. // Uninitialize the COM Library.
  69. CoUninitialize() ;
  70. return 0 ;
  71. }
  72. //-----------------------------------------------------------------------------
  73. // InitWindow
  74. // Initializes hidden window used by main service process thread.
  75. //-----------------------------------------------------------------------------
  76. BOOL InitWindow(int nCmdShow)
  77. {
  78. // Fill in window class structure with parameters
  79. // that describe the main window.
  80. WNDCLASS wcUpdateServer ;
  81. wcUpdateServer.style = 0 ;
  82. wcUpdateServer.lpfnWndProc = MainWndProc ;
  83. wcUpdateServer.cbClsExtra = 0 ;
  84. wcUpdateServer.cbWndExtra = 0 ;
  85. wcUpdateServer.hInstance = CFactory::s_hModule ;
  86. wcUpdateServer.hIcon = ::LoadIcon(CFactory::s_hModule,
  87. MAKEINTRESOURCE(IDC_ICON)) ;
  88. wcUpdateServer.hCursor = ::LoadCursor(NULL, IDC_ARROW) ;
  89. wcUpdateServer.hbrBackground = (HBRUSH) ::GetStockObject(GRAY_BRUSH) ;
  90. wcUpdateServer.lpszMenuName = NULL ;
  91. wcUpdateServer.lpszClassName = L"UpdateServiceServerInternalWindow" ;
  92. // returns GetLastError on fail.
  93. BOOL bResult = ::RegisterClass(&wcUpdateServer) ;
  94. if (!bResult)
  95. {
  96. return bResult ;
  97. }
  98. HWND hWndMain ;
  99. // returns getlasterror
  100. hWndMain = ::CreateWindow(
  101. L"UpdateServiceServerInternalWindow",
  102. L"Application Update Service",
  103. WS_OVERLAPPEDWINDOW,
  104. CW_USEDEFAULT, CW_USEDEFAULT,
  105. CW_USEDEFAULT, CW_USEDEFAULT,
  106. NULL,
  107. NULL,
  108. CFactory::s_hModule,
  109. NULL) ;
  110. // If window could not be created, return "failure".
  111. if (!hWndMain)
  112. {
  113. return FALSE ;
  114. }
  115. // Make the window visible; update its client area;
  116. // and return "success".
  117. ::ShowWindow(hWndMain, nCmdShow) ;
  118. ::UpdateWindow(hWndMain) ;
  119. return TRUE ;
  120. }
  121. //-----------------------------------------------------------------------------
  122. // MainWndProc
  123. // Window procedure for service process thread (hidden).
  124. //-----------------------------------------------------------------------------
  125. extern "C" LRESULT APIENTRY MainWndProc(
  126. HWND hWnd, // window handle
  127. UINT message, // type of message
  128. WPARAM wParam, // additional information
  129. LPARAM lParam) // additional information
  130. {
  131. DWORD dwStyle ;
  132. switch (message)
  133. {
  134. case WM_CREATE:
  135. {
  136. // Get size of main window
  137. CREATESTRUCT* pcs = (CREATESTRUCT*) lParam ;
  138. // Create a window. LISTBOX for no particular reason.
  139. g_hwndUpdateServer = ::CreateWindow(
  140. L"LISTBOX",
  141. NULL,
  142. WS_CHILD | WS_VISIBLE | LBS_USETABSTOPS
  143. | WS_VSCROLL | LBS_NOINTEGRALHEIGHT,
  144. 0, 0, pcs->cx, pcs->cy,
  145. hWnd,
  146. NULL,
  147. CFactory::s_hModule,
  148. NULL) ;
  149. if (g_hwndUpdateServer == NULL)
  150. {
  151. ASSERT(FALSE);
  152. return -1 ;
  153. }
  154. }
  155. break ;
  156. case WM_SIZE:
  157. ::MoveWindow(g_hwndUpdateServer, 0, 0,
  158. LOWORD(lParam), HIWORD(lParam), TRUE) ;
  159. break;
  160. case WM_DESTROY: // message: window being destroyed
  161. if (CFactory::CanUnloadNow() == S_OK)
  162. {
  163. // Only post the quit message, if there is
  164. // no one using the program.
  165. ::PostQuitMessage(0) ;
  166. }
  167. break ;
  168. case WM_CLOSE:
  169. // Decrement the lock count.
  170. ::InterlockedDecrement(&CFactory::s_cServerLocks) ;
  171. // The service is going away.
  172. g_hwndUpdateServer = NULL ;
  173. //Fall through
  174. default:
  175. return (DefWindowProc(hWnd, message, wParam, lParam)) ;
  176. }
  177. return 0 ;
  178. }