2using System.ComponentModel;
3using System.Reflection;
10 internal delegate
void ServiceMainFunction(
int numArs, IntPtr argPtrPtr);
17 private readonly
string serviceName;
18 private readonly
string instanceName;
22 this.serviceName = ServiceName;
23 this.instanceName = InstanceName;
25 if (!
string.IsNullOrEmpty(InstanceName))
26 this.serviceName +=
" " + InstanceName;
50 string Path = Assembly.GetExecutingAssembly().Location.Replace(
".dll",
".exe");
52 if (!
string.IsNullOrEmpty(this.instanceName))
53 Path +=
" -instance \"" + this.instanceName +
"\"";
57 using ServiceControlManager mgr = ServiceControlManager.Connect(
null,
null, ServiceControlManagerAccessRights.All);
59 if (mgr.TryOpenService(
this.serviceName, ServiceControlAccessRights.All, out ServiceHandle existingService,
60 out Win32Exception errorException))
62 using (existingService)
64 existingService.ChangeConfig(DisplayName, Path, ServiceType.Win32OwnProcess, StartType,
65 ErrorSeverity.Normal, Credentials);
67 if (!
string.IsNullOrEmpty(Description))
68 existingService.SetDescription(Description);
70 if (FailureActions is not
null)
72 existingService.SetFailureActions(FailureActions);
73 existingService.SetFailureActionFlag(
true);
76 existingService.SetFailureActionFlag(
false);
80 existingService.Start(throwIfAlreadyRunning:
false);
89 if (errorException.NativeErrorCode ==
Win32.ERROR_SERVICE_DOES_NOT_EXIST)
91 using ServiceHandle svc = mgr.CreateService(this.serviceName, DisplayName, Path, ServiceType.Win32OwnProcess,
92 StartType, ErrorSeverity.Normal, Credentials);
94 if (!
string.IsNullOrEmpty(Description))
95 svc.SetDescription(Description);
97 if (FailureActions is not
null)
99 svc.SetFailureActions(FailureActions);
100 svc.SetFailureActionFlag(
true);
103 svc.SetFailureActionFlag(
false);
105 if (StartImmediately)
114 throw errorException;
117 catch (DllNotFoundException dllException)
119 throw new PlatformNotSupportedException(
"Unable to call windows service management API.", dllException);
132 using ServiceControlManager mgr = ServiceControlManager.Connect(
null,
null, ServiceControlManagerAccessRights.All);
134 if (mgr.TryOpenService(
this.serviceName, ServiceControlAccessRights.All, out ServiceHandle existingService,
135 out Win32Exception errorException))
137 using (existingService)
139 existingService.Delete();
145 if (errorException.NativeErrorCode ==
Win32.ERROR_SERVICE_DOES_NOT_EXIST)
148 throw errorException;
151 catch (DllNotFoundException dllException)
153 throw new PlatformNotSupportedException(
"Unable to call windows service management API.", dllException);
A managed class that holds data referring to a T:DasMulli.Win32.ServiceUtils.ServiceFailureActionsInf...
Handles interaction with Windows Service API.
bool Uninstall()
Uninstalls the service.
int Install(string DisplayName, string Description, ServiceStartType StartType, bool StartImmediately, ServiceFailureActions FailureActions, Win32ServiceCredentials Credentials)
Installs the service.
Handles interaction with Windows Service API.
ServiceStartType
How the windows service should be started.