Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ServiceHandle.cs
1using System;
2using System.ComponentModel;
4using System.ServiceProcess;
7
9{
10 internal class ServiceHandle : SafeHandle
11 {
12 internal ServiceHandle() : 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 public virtual void Start(bool throwIfAlreadyRunning = true)
31 {
32 if (!Win32.StartServiceW(this, 0, IntPtr.Zero))
33 {
34 int win32Error = Marshal.GetLastWin32Error();
35
36 if (win32Error != Win32.ERROR_SERVICE_ALREADY_RUNNING || throwIfAlreadyRunning)
37 throw new Win32Exception(win32Error);
38 }
39 }
40
41 public virtual void Delete()
42 {
43 if (!Win32.DeleteService(this))
44 throw new Win32Exception(Marshal.GetLastWin32Error());
45 }
46
47 public virtual void SetDescription(string description)
48 {
49 ServiceDescriptionInfo descriptionInfo = new(description ?? string.Empty);
50 IntPtr lpDescriptionInfo = Marshal.AllocHGlobal(Marshal.SizeOf<ServiceDescriptionInfo>());
51 try
52 {
53 Marshal.StructureToPtr(descriptionInfo, lpDescriptionInfo, fDeleteOld: false);
54 try
55 {
56 if (!Win32.ChangeServiceConfig2W(this, ServiceConfigInfoTypeLevel.ServiceDescription, lpDescriptionInfo))
57 throw new Win32Exception(Marshal.GetLastWin32Error());
58 }
59 finally
60 {
61 Marshal.DestroyStructure<ServiceDescriptionInfo>(lpDescriptionInfo);
62 }
63 }
64 finally
65 {
66 Marshal.FreeHGlobal(lpDescriptionInfo);
67 }
68 }
69
70 public virtual void SetFailureActions(ServiceFailureActions serviceFailureActions)
71 {
72 ServiceFailureActionsInfo failureActions = serviceFailureActions is null ? ServiceFailureActionsInfo.Default : new ServiceFailureActionsInfo(serviceFailureActions.ResetPeriod, serviceFailureActions.RebootMessage, serviceFailureActions.RestartCommand, serviceFailureActions.Actions);
73 IntPtr lpFailureActions = Marshal.AllocHGlobal(Marshal.SizeOf<ServiceFailureActionsInfo>());
74 try
75 {
76 Marshal.StructureToPtr(failureActions, lpFailureActions, fDeleteOld: false);
77 try
78 {
79 if (!Win32.ChangeServiceConfig2W(this, ServiceConfigInfoTypeLevel.FailureActions, lpFailureActions))
80 throw new Win32Exception(Marshal.GetLastWin32Error());
81 }
82 finally
83 {
84 Marshal.DestroyStructure<ServiceFailureActionsInfo>(lpFailureActions);
85 }
86 }
87 finally
88 {
89 Marshal.FreeHGlobal(lpFailureActions);
90 }
91 }
92
93 public virtual void SetFailureActionFlag(bool enabled)
94 {
95 ServiceFailureActionsFlag failureActionsFlag = new(enabled);
96 IntPtr lpFailureActionsFlag = Marshal.AllocHGlobal(Marshal.SizeOf<ServiceFailureActionsFlag>());
97 try
98 {
99 Marshal.StructureToPtr(failureActionsFlag, lpFailureActionsFlag, fDeleteOld: false);
100 try
101 {
102 bool result = Win32.ChangeServiceConfig2W(this, ServiceConfigInfoTypeLevel.FailureActionsFlag, lpFailureActionsFlag);
103 if (!result)
104 throw new Win32Exception(Marshal.GetLastWin32Error());
105 }
106 finally
107 {
108 Marshal.DestroyStructure<ServiceFailureActionsFlag>(lpFailureActionsFlag);
109 }
110 }
111 finally
112 {
113 Marshal.FreeHGlobal(lpFailureActionsFlag);
114 }
115 }
116
117 public virtual void ChangeConfig(string displayName, string binaryPath, ServiceType serviceType, ServiceStartType startupType, ErrorSeverity errorSeverity, Win32ServiceCredentials credentials)
118 {
119 bool success = Win32.ChangeServiceConfigW(this, serviceType, startupType, errorSeverity, binaryPath, null, IntPtr.Zero, null, credentials.UserName, credentials.Password, displayName);
120 if (!success)
121 throw new Win32Exception(Marshal.GetLastWin32Error());
122 }
123 }
124}
Handles interaction with Windows Service API.
Definition: Win32.cs:16
ServiceStartType
How the windows service should be started.