2using System.ComponentModel;
3using System.Runtime.InteropServices;
9 internal class ServiceHandle : SafeHandle
11 internal ServiceHandle() : base(IntPtr.Zero, ownsHandle: true)
15 protected override bool ReleaseHandle()
17 return Win32.CloseServiceHandle(handle);
20 public override bool IsInvalid
22 [System.Security.SecurityCritical]
25 return handle == IntPtr.Zero;
29 public virtual void Start(
bool throwIfAlreadyRunning =
true)
31 if (!
Win32.StartServiceW(
this, 0, IntPtr.Zero))
33 int win32Error = Marshal.GetLastWin32Error();
35 if (win32Error !=
Win32.ERROR_SERVICE_ALREADY_RUNNING || throwIfAlreadyRunning)
36 throw new Win32Exception(win32Error);
40 public virtual void Delete()
42 if (!
Win32.DeleteService(
this))
43 throw new Win32Exception(Marshal.GetLastWin32Error());
46 public virtual void SetDescription(
string description)
48 ServiceDescriptionInfo descriptionInfo =
new ServiceDescriptionInfo(description ??
string.Empty);
49 IntPtr lpDescriptionInfo = Marshal.AllocHGlobal(Marshal.SizeOf<ServiceDescriptionInfo>());
52 Marshal.StructureToPtr(descriptionInfo, lpDescriptionInfo, fDeleteOld:
false);
55 if (!
Win32.ChangeServiceConfig2W(
this, ServiceConfigInfoTypeLevel.ServiceDescription, lpDescriptionInfo))
56 throw new Win32Exception(Marshal.GetLastWin32Error());
60 Marshal.DestroyStructure<ServiceDescriptionInfo>(lpDescriptionInfo);
65 Marshal.FreeHGlobal(lpDescriptionInfo);
69 public virtual void SetFailureActions(ServiceFailureActions serviceFailureActions)
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>());
75 Marshal.StructureToPtr(failureActions, lpFailureActions, fDeleteOld:
false);
78 if (!
Win32.ChangeServiceConfig2W(
this, ServiceConfigInfoTypeLevel.FailureActions, lpFailureActions))
79 throw new Win32Exception(Marshal.GetLastWin32Error());
83 Marshal.DestroyStructure<ServiceFailureActionsInfo>(lpFailureActions);
88 Marshal.FreeHGlobal(lpFailureActions);
92 public virtual void SetFailureActionFlag(
bool enabled)
94 ServiceFailureActionsFlag failureActionsFlag =
new ServiceFailureActionsFlag(enabled);
95 IntPtr lpFailureActionsFlag = Marshal.AllocHGlobal(Marshal.SizeOf<ServiceFailureActionsFlag>());
98 Marshal.StructureToPtr(failureActionsFlag, lpFailureActionsFlag, fDeleteOld:
false);
101 bool result =
Win32.ChangeServiceConfig2W(
this, ServiceConfigInfoTypeLevel.FailureActionsFlag, lpFailureActionsFlag);
103 throw new Win32Exception(Marshal.GetLastWin32Error());
107 Marshal.DestroyStructure<ServiceFailureActionsFlag>(lpFailureActionsFlag);
112 Marshal.FreeHGlobal(lpFailureActionsFlag);
116 public virtual void ChangeConfig(
string displayName,
string binaryPath, ServiceType serviceType,
ServiceStartType startupType, ErrorSeverity errorSeverity,
Win32ServiceCredentials credentials)
118 bool success =
Win32.ChangeServiceConfigW(
this, serviceType, startupType, errorSeverity, binaryPath,
null, IntPtr.Zero,
null, credentials.UserName, credentials.Password, displayName);
120 throw new Win32Exception(Marshal.GetLastWin32Error());
Handles interaction with Windows Service API.
ServiceStartType
How the windows service should be started.