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.

246 lines
6.4 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // iasjet.cpp
  8. //
  9. // SYNOPSIS
  10. //
  11. // Implementation of DLL exports for an ATL in proc server.
  12. //
  13. ///////////////////////////////////////////////////////////////////////////////
  14. #include <windows.h>
  15. #include <atlbase.h>
  16. CComModule _Module;
  17. #include <atlcom.h>
  18. #include <resource.h>
  19. #include <attrdnary.h>
  20. #include <iasdb.h>
  21. #include <oledbstore.h>
  22. #include <netshhelper.h>
  23. #include <setup.h>
  24. BEGIN_OBJECT_MAP(ObjectMap)
  25. OBJECT_ENTRY(__uuidof(AttributeDictionary), AttributeDictionary)
  26. OBJECT_ENTRY(__uuidof(OleDBDataStore), OleDBDataStore)
  27. OBJECT_ENTRY(__uuidof(CIASNetshJetHelper), CIASNetshJetHelper)
  28. END_OBJECT_MAP()
  29. //////////
  30. // DLL Entry Point
  31. //////////
  32. BOOL
  33. WINAPI
  34. DllMain(
  35. HINSTANCE hInstance,
  36. DWORD dwReason,
  37. LPVOID lpReserved
  38. )
  39. {
  40. if (dwReason == DLL_PROCESS_ATTACH)
  41. {
  42. _Module.Init(ObjectMap, hInstance);
  43. DisableThreadLibraryCalls(hInstance);
  44. }
  45. else if (dwReason == DLL_PROCESS_DETACH)
  46. {
  47. _Module.Term();
  48. }
  49. return TRUE;
  50. }
  51. //////////
  52. // Used to determine whether the DLL can be unloaded by OLE
  53. //////////
  54. STDAPI DllCanUnloadNow()
  55. {
  56. return (_Module.GetLockCount()==0) ? S_OK : S_FALSE;
  57. }
  58. //////////
  59. // Returns a class factory to create an object of the requested type.
  60. //////////
  61. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  62. {
  63. return _Module.GetClassObject(rclsid, riid, ppv);
  64. }
  65. //////////
  66. // DllRegisterServer - Adds entries to the system registry
  67. //////////
  68. STDAPI DllRegisterServer()
  69. {
  70. ////////////////////////////////////////////////////////////////////
  71. // Do the upgrade even if the registration failed. the upgrade code
  72. // does not rely on the registration.
  73. ////////////////////////////////////////////////////////////////////
  74. CIASMigrateOrUpgrade migrateOrUpgrade;
  75. // ignore return value, FALSE = not called from NetshellDataMigration
  76. migrateOrUpgrade.Execute(FALSE);
  77. return S_OK;
  78. }
  79. //////////
  80. // DllUnregisterServer - Removes entries from the system registry
  81. //////////
  82. STDAPI DllUnregisterServer()
  83. {
  84. return S_OK;
  85. }
  86. // Flag indicating whether we are running in-proc.
  87. BOOL theInprocFlag = TRUE;
  88. BOOL
  89. WINAPI
  90. IASIsInprocServer()
  91. {
  92. return theInprocFlag;
  93. }
  94. // The AppID for IAS Jet Database Access.
  95. struct __declspec(uuid("{A5CEB593-CCC3-486B-AB91-9C5C5ED4C9E1}")) theAppID;
  96. // Event used to signal the service to stop.
  97. HANDLE theStopEvent;
  98. // Service control handler.
  99. VOID
  100. WINAPI
  101. ServiceHandler(
  102. DWORD fdwControl // requested control code
  103. )
  104. {
  105. switch (fdwControl)
  106. {
  107. case SERVICE_CONTROL_SHUTDOWN:
  108. case SERVICE_CONTROL_STOP:
  109. SetEvent(theStopEvent);
  110. }
  111. }
  112. // Service Main.
  113. VOID
  114. WINAPI
  115. ServiceMain(
  116. DWORD /* dwArgc */,
  117. LPWSTR* /* lpszArgv */
  118. )
  119. {
  120. IASTraceInitializer traceInit;
  121. // We're being used as a service.
  122. theInprocFlag = FALSE;
  123. SERVICE_STATUS status =
  124. {
  125. SERVICE_WIN32_OWN_PROCESS, // dwServiceType;
  126. SERVICE_START_PENDING, // dwCurrentState;
  127. SERVICE_ACCEPT_STOP |
  128. SERVICE_ACCEPT_SHUTDOWN, // dwControlsAccepted;
  129. NO_ERROR, // dwWin32ExitCode;
  130. 0, // dwServiceSpecificExitCode;
  131. 0, // dwCheckPoint;
  132. 0 // dwWaitHint;
  133. };
  134. // Register the service control handler.
  135. SERVICE_STATUS_HANDLE statusHandle = RegisterServiceCtrlHandlerW(
  136. L"IASJet",
  137. ServiceHandler
  138. );
  139. // Let the SCM know we're starting.
  140. SetServiceStatus(statusHandle, &status);
  141. // Create the stop event.
  142. theStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  143. if (theStopEvent)
  144. {
  145. // Initialize the COM run-time.
  146. HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  147. if (SUCCEEDED(hr))
  148. {
  149. // Get the security settings from our AppID key in the registry.
  150. hr = CoInitializeSecurity(
  151. (PVOID)&__uuidof(theAppID),
  152. -1,
  153. NULL,
  154. NULL,
  155. 0,
  156. 0,
  157. NULL,
  158. EOAC_APPID,
  159. NULL
  160. );
  161. if (SUCCEEDED(hr))
  162. {
  163. // Register the class objects we support.
  164. hr = _Module.RegisterClassObjects(
  165. CLSCTX_LOCAL_SERVER,
  166. REGCLS_MULTIPLEUSE
  167. );
  168. if (SUCCEEDED(hr))
  169. {
  170. // Let the SCM know we're running.
  171. status.dwCurrentState = SERVICE_RUNNING;
  172. SetServiceStatus(statusHandle, &status);
  173. // Wait until someone tells us to stop.
  174. WaitForSingleObject(theStopEvent, INFINITE);
  175. status.dwCurrentState = SERVICE_STOP_PENDING;
  176. ++status.dwCheckPoint;
  177. status.dwWaitHint = 5 * 60 * 1000; // 5 minutes
  178. SetServiceStatus(statusHandle, &status);
  179. IASTraceString("IASJet service stopping");
  180. // Revoke the class objects.
  181. _Module.RevokeClassObjects();
  182. // wait for all clients to disconnect
  183. while(_Module.GetLockCount() > 0)
  184. {
  185. IASTracePrintf("IASJet service stopping. Still waiting "
  186. "Lock count = %ld", _Module.GetLockCount());
  187. Sleep(15);
  188. ++status.dwCheckPoint;
  189. SetServiceStatus(statusHandle, &status);
  190. }
  191. SwitchToThread();
  192. IASTraceString("IASJet service stopped");
  193. }
  194. }
  195. // Shutdown the COM runtime.
  196. CoUninitialize();
  197. }
  198. // Clean-up the stop event.
  199. CloseHandle(theStopEvent);
  200. theStopEvent = NULL;
  201. status.dwWin32ExitCode = hr;
  202. }
  203. else
  204. {
  205. status.dwWin32ExitCode = GetLastError();
  206. }
  207. // We're stopped.
  208. status.dwCurrentState = SERVICE_STOPPED;
  209. SetServiceStatus(statusHandle, &status);
  210. }
  211. #include <newop.cpp>
  212. #include <atlimpl.cpp>