Skip to content

Commit

Permalink
user correct API to close handles from Service Control Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
thoemmi authored and TravisTheTechie committed Feb 24, 2017
1 parent 0b352d8 commit 004f3ac
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/Topshelf/Runtime/Windows/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,17 @@ public enum SYSTEM_ACCESS : uint

[DllImport("advapi32.dll", EntryPoint = "OpenSCManagerW", ExactSpelling = true, CharSet = CharSet.Unicode,
SetLastError = true)]
public static extern SafeTokenHandle OpenSCManager(string machineName, string databaseName, uint dwAccess);
public static extern SCMHandle OpenSCManager(string machineName, string databaseName, uint dwAccess);

[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseServiceHandle(SafeTokenHandle hSCObject);
public static extern bool CloseServiceHandle(IntPtr hSCObject);

[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern SafeTokenHandle OpenService(SafeTokenHandle hSCManager, string lpServiceName, uint dwDesiredAccess);
public static extern SCMHandle OpenService(SCMHandle hSCManager, string lpServiceName, uint dwDesiredAccess);

[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool ChangeServiceConfig2(SafeTokenHandle serviceHandle, uint infoLevel,
public static extern bool ChangeServiceConfig2(SCMHandle serviceHandle, uint infoLevel,
IntPtr lpInfo);

[DllImport("advapi32.dll", SetLastError = true)]
Expand Down
27 changes: 27 additions & 0 deletions src/Topshelf/Runtime/Windows/SCMHandle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2007-2017 Chris Patterson, Dru Sellers, Travis Smith, et. al.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
namespace Topshelf.Runtime.Windows
{
using Microsoft.Win32.SafeHandles;

public class SCMHandle : SafeHandleZeroOrMinusOneIsInvalid
{
public SCMHandle()
: base(true) { }

protected override bool ReleaseHandle()
{
return NativeMethods.CloseServiceHandle(handle);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class WindowsServiceRecoveryController
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
public void SetServiceRecoveryOptions(HostSettings settings, ServiceRecoveryOptions options)
{
SafeTokenHandle scmHandle = null;
SafeTokenHandle serviceHandle = null;
SCMHandle scmHandle = null;
SCMHandle serviceHandle = null;
IntPtr lpsaActions = IntPtr.Zero;
IntPtr lpInfo = IntPtr.Zero;
IntPtr lpFlagInfo = IntPtr.Zero;
Expand Down Expand Up @@ -127,9 +127,9 @@ public void SetServiceRecoveryOptions(HostSettings settings, ServiceRecoveryOpti
if (lpsaActions != IntPtr.Zero)
Marshal.FreeHGlobal(lpsaActions);
if (serviceHandle != null)
NativeMethods.CloseServiceHandle(serviceHandle);
serviceHandle.Close();
if (scmHandle != null)
NativeMethods.CloseServiceHandle(scmHandle);
scmHandle.Close();
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Topshelf/Topshelf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@
<Compile Include="Runtime\Windows\RestartSystemRecoveryAction.cs" />
<Compile Include="Runtime\Windows\RunProgramRecoveryAction.cs" />
<Compile Include="Runtime\Windows\SafeTokenHandle.cs" />
<Compile Include="Runtime\Windows\SCMHandle.cs" />
<Compile Include="Runtime\Windows\ServiceRecoveryAction.cs" />
<Compile Include="Runtime\Windows\ServiceRecoveryOptions.cs" />
<Compile Include="Runtime\Windows\WindowsHostEnvironment.cs" />
Expand Down

0 comments on commit 004f3ac

Please sign in to comment.