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;
3using System.Runtime.InteropServices;
6
8{
9 internal class ServiceHandle : SafeHandle
10 {
11 internal ServiceHandle() : base(IntPtr.Zero, ownsHandle: true)
12 {
13 }
14
15 protected override bool ReleaseHandle()
16 {
17 return Win32.CloseServiceHandle(handle);
18 }
19
20 public override bool IsInvalid
21 {
22 [System.Security.SecurityCritical]
23 get
24 {
25 return handle == IntPtr.Zero;
26 }
27 }
28
29 public virtual void Start(bool throwIfAlreadyRunning = true)
30 {
31 if (!Win32.StartServiceW(this, 0, IntPtr.Zero))
32 {
33 int win32Error = Marshal.GetLastWin32Error();
34
35 if (win32Error != Win32.ERROR_SERVICE_ALREADY_RUNNING || throwIfAlreadyRunning)
36 throw new Win32Exception(win32Error);
37 }
38 }
39
40 public virtual void Delete()
41 {
42 if (!Win32.DeleteService(this))
43 throw new Win32Exception(Marshal.GetLastWin32Error());
44 }
45
46 public virtual void SetDescription(string description)
47 {
48 ServiceDescriptionInfo descriptionInfo = new ServiceDescriptionInfo(description ?? string.Empty);
49 IntPtr lpDescriptionInfo = Marshal.AllocHGlobal(Marshal.SizeOf<ServiceDescriptionInfo>());
50 try
51 {
52 Marshal.StructureToPtr(descriptionInfo, lpDescriptionInfo, fDeleteOld: false);
53 try
54 {
55 if (!Win32.ChangeServiceConfig2W(this, ServiceConfigInfoTypeLevel.ServiceDescription, lpDescriptionInfo))
56 throw new Win32Exception(Marshal.GetLastWin32Error());
57 }
58 finally
59 {
60 Marshal.DestroyStructure<ServiceDescriptionInfo>(lpDescriptionInfo);
61 }
62 }
63 finally
64 {
65 Marshal.FreeHGlobal(lpDescriptionInfo);
66 }
67 }
68
69 public virtual void SetFailureActions(ServiceFailureActions serviceFailureActions)
70 {
71 ServiceFailureActionsInfo failureActions = serviceFailureActions is null ? ServiceFailureActionsInfo.Default : new ServiceFailureActionsInfo(serviceFailureActions.ResetPeriod, serviceFailureActions.RebootMessage, serviceFailureActions.RestartCommand, serviceFailureActions.Actions);
72 IntPtr lpFailureActions = Marshal.AllocHGlobal(Marshal.SizeOf<ServiceFailureActionsInfo>());
73 try
74 {
75 Marshal.StructureToPtr(failureActions, lpFailureActions, fDeleteOld: false);
76 try
77 {
78 if (!Win32.ChangeServiceConfig2W(this, ServiceConfigInfoTypeLevel.FailureActions, lpFailureActions))
79 throw new Win32Exception(Marshal.GetLastWin32Error());
80 }
81 finally
82 {
83 Marshal.DestroyStructure<ServiceFailureActionsInfo>(lpFailureActions);
84 }
85 }
86 finally
87 {
88 Marshal.FreeHGlobal(lpFailureActions);
89 }
90 }
91
92 public virtual void SetFailureActionFlag(bool enabled)
93 {
94 ServiceFailureActionsFlag failureActionsFlag = new ServiceFailureActionsFlag(enabled);
95 IntPtr lpFailureActionsFlag = Marshal.AllocHGlobal(Marshal.SizeOf<ServiceFailureActionsFlag>());
96 try
97 {
98 Marshal.StructureToPtr(failureActionsFlag, lpFailureActionsFlag, fDeleteOld: false);
99 try
100 {
101 bool result = Win32.ChangeServiceConfig2W(this, ServiceConfigInfoTypeLevel.FailureActionsFlag, lpFailureActionsFlag);
102 if (!result)
103 throw new Win32Exception(Marshal.GetLastWin32Error());
104 }
105 finally
106 {
107 Marshal.DestroyStructure<ServiceFailureActionsFlag>(lpFailureActionsFlag);
108 }
109 }
110 finally
111 {
112 Marshal.FreeHGlobal(lpFailureActionsFlag);
113 }
114 }
115
116 public virtual void ChangeConfig(string displayName, string binaryPath, ServiceType serviceType, ServiceStartType startupType, ErrorSeverity errorSeverity, Win32ServiceCredentials credentials)
117 {
118 bool success = Win32.ChangeServiceConfigW(this, serviceType, startupType, errorSeverity, binaryPath, null, IntPtr.Zero, null, credentials.UserName, credentials.Password, displayName);
119 if (!success)
120 throw new Win32Exception(Marshal.GetLastWin32Error());
121 }
122 }
123}
Handles interaction with Windows Service API.
Definition: Win32.cs:14
ServiceStartType
How the windows service should be started.