Skip to content

Commit

Permalink
Fix service startup issue. Close #2
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbrute committed Jul 12, 2019
1 parent 59fca52 commit def0e17
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

[ v1.0.1 ]
- Ship required WinDivert64.sys file
- Increased debuggability
- Increased debuggability

[ v1.0.2 ]
- Fixed IP routing service issue
1 change: 1 addition & 0 deletions EvilLimiter.Windows/EvilLimiter.Windows.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utilities\NetworkUtilities.cs" />
<Compile Include="Utilities\ServiceUtilities.cs" />
<EmbeddedResource Include="Forms\FrmAddHost.resx">
<DependentUpon>FrmAddHost.cs</DependentUpon>
</EmbeddedResource>
Expand Down
5 changes: 5 additions & 0 deletions EvilLimiter.Windows/Forms/FrmMain.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using EvilLimiter.Windows.Common;
using EvilLimiter.Windows.Data;
using EvilLimiter.Windows.Networking;
using EvilLimiter.Windows.Utilities;
using MetroFramework;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -280,6 +281,8 @@ private void TglRouting_CheckedChanged(object sender, EventArgs e)
Task.Run(() =>
{
Invoke(invokeGenerator(false));
ServiceUtilities.ChangeStartMode(Globals.RoutingService, ServiceStartMode.Manual);
Globals.RoutingService.Start();
while (Globals.RoutingService.Status != ServiceControllerStatus.Running)
Expand All @@ -296,7 +299,9 @@ private void TglRouting_CheckedChanged(object sender, EventArgs e)
Task.Run(() =>
{
Invoke(invokeGenerator(false));
Globals.RoutingService.Stop();
ServiceUtilities.ChangeStartMode(Globals.RoutingService, ServiceStartMode.Manual);
while (Globals.RoutingService.Status != ServiceControllerStatus.Stopped)
{
Expand Down
4 changes: 2 additions & 2 deletions EvilLimiter.Windows/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// indem Sie "*" wie unten gezeigt eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyVersion("1.0.2.0")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: NeutralResourcesLanguage("en-US")]

83 changes: 83 additions & 0 deletions EvilLimiter.Windows/Utilities/ServiceUtilities.cs
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);
}
}
}

0 comments on commit def0e17

Please sign in to comment.