PageRenderTime 136ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/CSControlWindowsService/ServiceControllerEx.cs

#
C# | 226 lines | 117 code | 17 blank | 92 comment | 17 complexity | 7aa7b7a696829d74b38e962aab303dd5 MD5 | raw file
  1. /********************************* Module Header *********************************\
  2. Module Name: ServiceController.cs
  3. Project: CSControlWindowsService
  4. Copyright (c) Microsoft Corporation.
  5. The file defines the helper methods for installing, uninstalling, starting,
  6. stopping, and updating the DACL of a Windows service.
  7. This source is subject to the Microsoft Public License.
  8. See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  9. All other rights reserved.
  10. THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  11. EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  12. WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  13. \*********************************************************************************/
  14. using System;
  15. using System.ServiceProcess;
  16. using System.ComponentModel;
  17. namespace CSControlWindowsService
  18. {
  19. static class ServiceControllerEx
  20. {
  21. /// <summary>
  22. /// Install a Windows service.
  23. /// </summary>
  24. /// <param name="machineName">
  25. /// The name of the target computer. If the value is null or an empty
  26. /// string, the function connects to the SCM on the local machine.
  27. /// </param>
  28. /// <param name="serviceName">
  29. /// The name of the service to install. The maximum string length is 256
  30. /// characters.
  31. /// </param>
  32. /// <param name="displayName">
  33. /// The display name to be used by user interface programs to identify the
  34. /// service. This string has a maximum length of 256 chars.
  35. /// </param>
  36. /// <param name="dwStartType">
  37. /// The service start options. The parameter can be one of the following
  38. /// values: SERVICE_AUTO_START, SERVICE_BOOT_START, SERVICE_DEMAND_START,
  39. /// SERVICE_DISABLED, SERVICE_SYSTEM_START.
  40. /// </param>
  41. /// <param name="binaryPath">
  42. /// The full path to the service binary file.
  43. /// </param>
  44. /// <param name="serviceStartName">
  45. /// The name of the account under which the service should run. If this
  46. /// parameter is NULL, CreateService uses the LocalSystem account. The
  47. /// parameter can also be "NT AUTHORITY\LocalService",
  48. /// "NT AUTHORITY\NetworkService", or in the form of "DomainName\UserName",
  49. /// or ".\UserName".
  50. /// </param>
  51. /// <param name="password">
  52. /// The password to the account name specified by the serviceStartName
  53. /// parameter. Specify an empty string if the account has no password or if
  54. /// the service runs in the LocalService, NetworkService, or LocalSystem
  55. /// account.
  56. /// </param>
  57. /// <returns></returns>
  58. public static void InstallService(string machineName, string serviceName,
  59. string displayName, ServiceStart dwStartType, string binaryPath,
  60. string serviceStartName, string password)
  61. {
  62. SafeServiceHandle schSCManager = null;
  63. SafeServiceHandle schService = null;
  64. try
  65. {
  66. // Get a handle to the SCM database.
  67. schSCManager = NativeMethods.OpenSCManager(
  68. machineName,
  69. null,
  70. ServiceControlAccessRights.SC_MANAGER_CREATE_SERVICE);
  71. if (schSCManager.IsInvalid)
  72. {
  73. throw new Win32Exception();
  74. }
  75. // Create the service.
  76. schService = NativeMethods.CreateService(
  77. schSCManager,
  78. serviceName,
  79. displayName,
  80. ServiceAccessRights.SERVICE_ALL_ACCESS,
  81. ServiceType.SERVICE_WIN32_OWN_PROCESS,
  82. dwStartType,
  83. ServiceError.SERVICE_ERROR_NORMAL,
  84. binaryPath,
  85. null,
  86. null,
  87. null,
  88. serviceStartName,
  89. password);
  90. if (schService.IsInvalid)
  91. {
  92. throw new Win32Exception();
  93. }
  94. }
  95. finally
  96. {
  97. if (schSCManager != null)
  98. {
  99. schSCManager.Close();
  100. }
  101. if (schService != null)
  102. {
  103. schService.Close();
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// Uninstall a Windows service. The specified service and the services
  109. /// that services that depend on the specified service will be stopped
  110. /// first, then the service is uninstalled.
  111. /// </summary>
  112. /// <param name="machineName">
  113. /// The name of the target computer. If the value is null or an empty
  114. /// string, the function connects to the SCM on the local machine.
  115. /// </param>
  116. /// <param name="serviceName">
  117. /// The name of the service to uninstall. The maximum string length is 256
  118. /// characters.
  119. /// </param>
  120. public static void UninstallService(string machineName, string serviceName)
  121. {
  122. // Try to stop the service and the services that depend on the service.
  123. ServiceController service = new ServiceController(serviceName, machineName);
  124. if (service.Status != ServiceControllerStatus.Stopped)
  125. {
  126. service.Stop();
  127. service.WaitForStatus(ServiceControllerStatus.Stopped,
  128. new TimeSpan(0, 0, 30));
  129. }
  130. SafeServiceHandle schSCManager = null;
  131. SafeServiceHandle schService = null;
  132. try
  133. {
  134. // Get a handle to the SCM database.
  135. schSCManager = NativeMethods.OpenSCManager(
  136. machineName,
  137. null,
  138. ServiceControlAccessRights.SC_MANAGER_CONNECT);
  139. if (schSCManager.IsInvalid)
  140. {
  141. throw new Win32Exception();
  142. }
  143. // Open the service with the delete permission.
  144. schService = NativeMethods.OpenService(
  145. schService,
  146. serviceName,
  147. ServiceAccessRights.SERVICE_STOP);
  148. if (schService.IsInvalid)
  149. {
  150. throw new Win32Exception();
  151. }
  152. // Now remove the service by calling DeleteService.
  153. if (!NativeMethods.DeleteService(schService))
  154. {
  155. throw new Win32Exception();
  156. }
  157. }
  158. finally
  159. {
  160. if (schSCManager != null)
  161. {
  162. schSCManager.Close();
  163. }
  164. if (schService != null)
  165. {
  166. schService.Close();
  167. }
  168. }
  169. }
  170. /// <summary>
  171. /// Update the service DACL to grant start, stop, delete and read control
  172. /// access to all authenticated users.
  173. /// </summary>
  174. /// <param name="machineName">
  175. /// The name of the target computer. If the value is null or an empty
  176. /// string, the function connects to the SCM on the local machine.
  177. /// </param>
  178. /// <param name="serviceName">
  179. /// The name of the service to update the DACL. The maximum string length
  180. /// is 256 characters.
  181. /// </param>
  182. public static void UpdateServiceDACL(string machineName, string serviceName)
  183. {
  184. }
  185. /// <summary>
  186. ///
  187. /// </summary>
  188. /// <param name="machineName"></param>
  189. /// <param name="serviceName"></param>
  190. /// <returns></returns>
  191. public static bool IsServiceInstalled(string machineName, string serviceName)
  192. {
  193. // Get the list of Windows services.
  194. ServiceController[] services = ServiceController.GetServices(machineName);
  195. // Try to find the service name.
  196. foreach (ServiceController service in services)
  197. {
  198. if (service.ServiceName == serviceName)
  199. {
  200. return true;
  201. }
  202. }
  203. return false;
  204. }
  205. }
  206. }