-
Notifications
You must be signed in to change notification settings - Fork 2
/
ServiceHelper.cs
21 lines (18 loc) · 1.03 KB
/
ServiceHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace System.ServiceProcess
{
internal static class ServiceControllerExtensions
{
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern Boolean ChangeServiceConfig(IntPtr hService, UInt32 nServiceType, UInt32 nStartType, UInt32 nErrorControl, String lpBinaryPathName, String lpLoadOrderGroup, IntPtr lpdwTagId, [In] char[] lpDependencies, String lpServiceStartName, String lpPassword, String lpDisplayName);
private const uint SERVICE_NO_CHANGE = 0xFFFFFFFF;
public static void SetStartType(this ServiceController svc, ServiceStartMode mode)
{
using (var serviceHandle = svc.ServiceHandle)
if (!ChangeServiceConfig(serviceHandle.DangerousGetHandle(), SERVICE_NO_CHANGE, (uint)mode, SERVICE_NO_CHANGE, null, null, IntPtr.Zero, null, null, null, null))
throw new ExternalException("Could not change service start type.", new Win32Exception());
}
}
}