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