-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConfigurer.cs
60 lines (52 loc) · 1.84 KB
/
Configurer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.ServiceProcess;
using System.Text;
namespace TaskSchedulerConfig
{
class Configurer
{
Validator v;
public Configurer(Validator validator)
{
v = validator;
}
internal bool EnableFirewall(object obj)
{
v.Firewall.Enabled = true;
return v.Firewall.Enabled;
}
internal bool EnableFirewallRule(object obj)
{
if (obj is Firewall.Rule)
{
v.Firewall.Rules[(Firewall.Rule)obj] = true;
return v.Firewall.Rules[(Firewall.Rule)obj];
}
throw new ArgumentException();
}
internal bool RunRemoteRegistryService(object obj)
{
v.RemoteRegistryService.SetStartType(ServiceStartMode.Automatic);
v.RemoteRegistryService.Start();
return v.RemoteRegistryServiceRunning;
}
}
internal static class ServiceHelper
{
[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());
}
}
}