-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
95 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Runtime.InteropServices; | ||
using System.ServiceProcess; | ||
|
||
namespace EvilLimiter.Windows.Utilities | ||
{ | ||
public static class ServiceUtilities | ||
{ | ||
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)] | ||
public 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); | ||
|
||
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)] | ||
static extern IntPtr OpenService( | ||
IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess); | ||
|
||
[DllImport("advapi32.dll", EntryPoint = "OpenSCManagerW", ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)] | ||
public static extern IntPtr OpenSCManager( | ||
string machineName, string databaseName, uint dwAccess); | ||
|
||
[DllImport("advapi32.dll", EntryPoint = "CloseServiceHandle")] | ||
public static extern int CloseServiceHandle(IntPtr hSCObject); | ||
|
||
private const uint SERVICE_NO_CHANGE = 0xFFFFFFFF; | ||
private const uint SERVICE_QUERY_CONFIG = 0x00000001; | ||
private const uint SERVICE_CHANGE_CONFIG = 0x00000002; | ||
private const uint SC_MANAGER_ALL_ACCESS = 0x000F003F; | ||
|
||
public static void ChangeStartMode(ServiceController svc, ServiceStartMode mode) | ||
{ | ||
var scManagerHandle = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS); | ||
if (scManagerHandle == IntPtr.Zero) | ||
{ | ||
throw new ExternalException("Open Service Manager Error"); | ||
} | ||
|
||
var serviceHandle = OpenService( | ||
scManagerHandle, | ||
svc.ServiceName, | ||
SERVICE_QUERY_CONFIG | SERVICE_CHANGE_CONFIG); | ||
|
||
if (serviceHandle == IntPtr.Zero) | ||
{ | ||
throw new ExternalException("Open Service Error"); | ||
} | ||
|
||
var result = ChangeServiceConfig( | ||
serviceHandle, | ||
SERVICE_NO_CHANGE, | ||
(uint)mode, | ||
SERVICE_NO_CHANGE, | ||
null, | ||
null, | ||
IntPtr.Zero, | ||
null, | ||
null, | ||
null, | ||
null); | ||
|
||
if (result == false) | ||
{ | ||
int nError = Marshal.GetLastWin32Error(); | ||
var win32Exception = new Win32Exception(nError); | ||
throw new ExternalException("Could not change service start type: " | ||
+ win32Exception.Message); | ||
} | ||
|
||
CloseServiceHandle(serviceHandle); | ||
CloseServiceHandle(scManagerHandle); | ||
} | ||
} | ||
} |