Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ServiceInstaller.cs
1using System;
2using System.ComponentModel;
3using System.Reflection;
7
9{
10 internal delegate void ServiceMainFunction(int numArs, IntPtr argPtrPtr);
11
15 public class ServiceInstaller
16 {
17 private readonly string serviceName;
18 private readonly string instanceName;
19
20 internal ServiceInstaller(string ServiceName, string InstanceName)
21 {
22 this.serviceName = ServiceName;
23 this.instanceName = InstanceName;
24
25 if (!string.IsNullOrEmpty(InstanceName))
26 this.serviceName += " " + InstanceName;
27 }
28
47 public int Install(string DisplayName, string Description, ServiceStartType StartType, bool StartImmediately,
48 ServiceFailureActions FailureActions, Win32ServiceCredentials Credentials)
49 {
50 string Path = Assembly.GetExecutingAssembly().Location.Replace(".dll", ".exe");
51
52 if (!string.IsNullOrEmpty(this.instanceName))
53 Path += " -instance \"" + this.instanceName + "\"";
54
55 try
56 {
57 using ServiceControlManager mgr = ServiceControlManager.Connect(null, null, ServiceControlManagerAccessRights.All);
58
59 if (mgr.TryOpenService(this.serviceName, ServiceControlAccessRights.All, out ServiceHandle existingService,
60 out Win32Exception errorException))
61 {
62 using (existingService)
63 {
64 existingService.ChangeConfig(DisplayName, Path, ServiceType.Win32OwnProcess, StartType,
65 ErrorSeverity.Normal, Credentials);
66
67 if (!string.IsNullOrEmpty(Description))
68 existingService.SetDescription(Description);
69
70 if (FailureActions is not null)
71 {
72 existingService.SetFailureActions(FailureActions);
73 existingService.SetFailureActionFlag(true);
74 }
75 else
76 existingService.SetFailureActionFlag(false);
77
78 if (StartImmediately)
79 {
80 existingService.Start(throwIfAlreadyRunning: false);
81 return 3;
82 }
83 else
84 return 2;
85 }
86 }
87 else
88 {
89 if (errorException.NativeErrorCode == Win32.ERROR_SERVICE_DOES_NOT_EXIST)
90 {
91 using ServiceHandle svc = mgr.CreateService(this.serviceName, DisplayName, Path, ServiceType.Win32OwnProcess,
92 StartType, ErrorSeverity.Normal, Credentials);
93
94 if (!string.IsNullOrEmpty(Description))
95 svc.SetDescription(Description);
96
97 if (FailureActions is not null)
98 {
99 svc.SetFailureActions(FailureActions);
100 svc.SetFailureActionFlag(true);
101 }
102 else
103 svc.SetFailureActionFlag(false);
104
105 if (StartImmediately)
106 {
107 svc.Start();
108 return 1;
109 }
110 else
111 return 0;
112 }
113 else
114 throw errorException;
115 }
116 }
117 catch (DllNotFoundException dllException)
118 {
119 throw new PlatformNotSupportedException("Unable to call windows service management API.", dllException);
120 }
121 }
122
128 public bool Uninstall()
129 {
130 try
131 {
132 using ServiceControlManager mgr = ServiceControlManager.Connect(null, null, ServiceControlManagerAccessRights.All);
133
134 if (mgr.TryOpenService(this.serviceName, ServiceControlAccessRights.All, out ServiceHandle existingService,
135 out Win32Exception errorException))
136 {
137 using (existingService)
138 {
139 existingService.Delete();
140 return true;
141 }
142 }
143 else
144 {
145 if (errorException.NativeErrorCode == Win32.ERROR_SERVICE_DOES_NOT_EXIST)
146 return false;
147 else
148 throw errorException;
149 }
150 }
151 catch (DllNotFoundException dllException)
152 {
153 throw new PlatformNotSupportedException("Unable to call windows service management API.", dllException);
154 }
155 }
156
157 }
158}
A managed class that holds data referring to a T:DasMulli.Win32.ServiceUtils.ServiceFailureActionsInf...
Handles interaction with Windows Service API.
int Install(string DisplayName, string Description, ServiceStartType StartType, bool StartImmediately, ServiceFailureActions FailureActions, Win32ServiceCredentials Credentials)
Installs the service.
Handles interaction with Windows Service API.
Definition: Win32.cs:14
ServiceStartType
How the windows service should be started.