2using System.ComponentModel;
4using System.ServiceProcess;
10 internal class ServiceControlManager : SafeHandle
12 internal ServiceControlManager() : base(IntPtr.Zero, ownsHandle:
true)
16 protected override bool ReleaseHandle()
18 return Win32.CloseServiceHandle(this.handle);
21 public override bool IsInvalid
26 return this.handle == IntPtr.Zero;
30 internal static ServiceControlManager Connect(
string machineName,
string databaseName, ServiceControlManagerAccessRights desiredAccessRights)
32 ServiceControlManager mgr =
Win32.OpenSCManagerW(machineName, databaseName, desiredAccessRights);
35 throw new Win32Exception(Marshal.GetLastWin32Error());
40 public ServiceHandle CreateService(
string serviceName,
string displayName,
string binaryPath,
43 ServiceHandle service =
Win32.CreateServiceW(
this, serviceName, displayName, ServiceControlAccessRights.All, serviceType, startupType, errorSeverity,
44 binaryPath,
null, IntPtr.Zero,
null, credentials.UserName, credentials.Password);
46 if (service.IsInvalid)
47 throw new Win32Exception(Marshal.GetLastWin32Error());
52 public ServiceHandle OpenService(
string serviceName, ServiceControlAccessRights desiredControlAccess)
54 if (!this.TryOpenService(serviceName, desiredControlAccess, out ServiceHandle service, out Win32Exception errorException))
60 public virtual bool TryOpenService(
string serviceName, ServiceControlAccessRights desiredControlAccess, out ServiceHandle serviceHandle,
61 out Win32Exception errorException)
63 ServiceHandle service =
Win32.OpenServiceW(
this, serviceName, desiredControlAccess);
65 if (service.IsInvalid)
67 errorException =
new Win32Exception(Marshal.GetLastWin32Error());
72 serviceHandle = service;
73 errorException =
null;
Handles interaction with Windows Service API.
ErrorSeverity
Error severity
ServiceStartType
How the windows service should be started.