Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ServiceControlManager.cs
1using System;
2using System.ComponentModel;
4using System.ServiceProcess;
7
9{
10 internal class ServiceControlManager : SafeHandle
11 {
12 internal ServiceControlManager() : base(IntPtr.Zero, ownsHandle: true)
13 {
14 }
15
16 protected override bool ReleaseHandle()
17 {
18 return Win32.CloseServiceHandle(this.handle);
19 }
20
21 public override bool IsInvalid
22 {
23 [System.Security.SecurityCritical]
24 get
25 {
26 return this.handle == IntPtr.Zero;
27 }
28 }
29
30 internal static ServiceControlManager Connect(string machineName, string databaseName, ServiceControlManagerAccessRights desiredAccessRights)
31 {
32 ServiceControlManager mgr = Win32.OpenSCManagerW(machineName, databaseName, desiredAccessRights);
33
34 if (mgr.IsInvalid)
35 throw new Win32Exception(Marshal.GetLastWin32Error());
36
37 return mgr;
38 }
39
40 public ServiceHandle CreateService(string serviceName, string displayName, string binaryPath,
41 ServiceType serviceType, ServiceStartType startupType, ErrorSeverity errorSeverity, Win32ServiceCredentials credentials)
42 {
43 ServiceHandle service = Win32.CreateServiceW(this, serviceName, displayName, ServiceControlAccessRights.All, serviceType, startupType, errorSeverity,
44 binaryPath, null, IntPtr.Zero, null, credentials.UserName, credentials.Password);
45
46 if (service.IsInvalid)
47 throw new Win32Exception(Marshal.GetLastWin32Error());
48
49 return service;
50 }
51
52 public ServiceHandle OpenService(string serviceName, ServiceControlAccessRights desiredControlAccess)
53 {
54 if (!this.TryOpenService(serviceName, desiredControlAccess, out ServiceHandle service, out Win32Exception errorException))
55 throw errorException;
56
57 return service;
58 }
59
60 public virtual bool TryOpenService(string serviceName, ServiceControlAccessRights desiredControlAccess, out ServiceHandle serviceHandle,
61 out Win32Exception errorException)
62 {
63 ServiceHandle service = Win32.OpenServiceW(this, serviceName, desiredControlAccess);
64
65 if (service.IsInvalid)
66 {
67 errorException = new Win32Exception(Marshal.GetLastWin32Error());
68 serviceHandle = null;
69 return false;
70 }
71
72 serviceHandle = service;
73 errorException = null;
74 return true;
75 }
76 }
77}
Handles interaction with Windows Service API.
Definition: Win32.cs:16
ServiceStartType
How the windows service should be started.