Skip to content

Commit 91c31d8

Browse files
committed
Use common implementation for services.
1 parent a1af056 commit 91c31d8

2 files changed

Lines changed: 26 additions & 61 deletions

File tree

NtApiDotNet/Win32/Service/ServiceControlManager.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ public static ServiceControlManager Open(string machine_name, string database_na
8383
{
8484
return Open(machine_name, database_name, desired_access, true).Result;
8585
}
86+
87+
/// <summary>
88+
/// Open an instance of the SCM.
89+
/// </summary>
90+
/// <param name="machine_name">The machine name for the SCM.</param>
91+
/// <param name="desired_access">The desired access for the SCM connection.</param>
92+
/// <returns>The SCM instance.</returns>
93+
public static ServiceControlManager Open(string machine_name,
94+
ServiceControlManagerAccessRights desired_access)
95+
{
96+
return Open(machine_name, null, desired_access);
97+
}
8698
#endregion
8799

88100
#region Public Methods
@@ -160,5 +172,9 @@ public void Dispose()
160172
_scm.Close();
161173
}
162174
#endregion
175+
176+
#region Internal Members
177+
internal SafeServiceHandle Handle => _scm;
178+
#endregion
163179
}
164180
}

NtApiDotNet/Win32/ServiceUtils.cs

Lines changed: 10 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using NtApiDotNet.Utilities.Reflection;
1616
using NtApiDotNet.Win32.SafeHandles;
1717
using NtApiDotNet.Win32.Security.Native;
18+
using NtApiDotNet.Win32.Service;
1819
using System;
1920
using System.Collections.Generic;
2021
using System.Linq;
@@ -755,47 +756,6 @@ private static int GetServiceProcessId(SafeServiceHandle scm, string name)
755756
}
756757
}
757758

758-
private static IEnumerable<Win32Service> GetServices(string machine_name, SERVICE_STATE service_state, ServiceType service_types)
759-
{
760-
using (SafeServiceHandle scm = Win32NativeMethods.OpenSCManager(machine_name, null,
761-
ServiceControlManagerAccessRights.Connect | ServiceControlManagerAccessRights.EnumerateService))
762-
{
763-
if (scm.IsInvalid)
764-
{
765-
throw new SafeWin32Exception();
766-
}
767-
768-
const int Length = 32 * 1024;
769-
using (var buffer = new SafeHGlobalBuffer(Length))
770-
{
771-
int resume_handle = 0;
772-
while (true)
773-
{
774-
bool ret = Win32NativeMethods.EnumServicesStatusEx(scm, SC_ENUM_TYPE.SC_ENUM_PROCESS_INFO,
775-
service_types, service_state, buffer,
776-
buffer.Length, out int bytes_needed, out int services_returned, ref resume_handle, null);
777-
Win32Error error = Win32Utils.GetLastWin32Error();
778-
if (!ret && error != Win32Error.ERROR_MORE_DATA)
779-
{
780-
throw new SafeWin32Exception(error);
781-
}
782-
783-
ENUM_SERVICE_STATUS_PROCESS[] services = new ENUM_SERVICE_STATUS_PROCESS[services_returned];
784-
buffer.ReadArray(0, services, 0, services_returned);
785-
foreach (var service in services)
786-
{
787-
yield return new Win32Service(machine_name, service);
788-
}
789-
790-
if (ret)
791-
{
792-
break;
793-
}
794-
}
795-
}
796-
}
797-
}
798-
799759
private static NtResult<SafeServiceHandle> OpenService(SafeServiceHandle scm, string name, ServiceAccessRights desired_access, bool throw_on_error)
800760
{
801761
using (var service = Win32NativeMethods.OpenService(scm, name, desired_access))
@@ -1346,22 +1306,11 @@ public static Win32Service GetService(string name)
13461306
/// <returns>A list of registered services.</returns>
13471307
public static IEnumerable<Win32Service> GetServices(string machine_name, ServiceState state, ServiceType service_types)
13481308
{
1349-
SERVICE_STATE state_flags;
1350-
switch (state)
1309+
using (var scm = ServiceControlManager.Open(machine_name, null,
1310+
ServiceControlManagerAccessRights.Connect | ServiceControlManagerAccessRights.EnumerateService))
13511311
{
1352-
case ServiceState.All:
1353-
state_flags = SERVICE_STATE.SERVICE_STATE_ALL;
1354-
break;
1355-
case ServiceState.Active:
1356-
state_flags = SERVICE_STATE.SERVICE_ACTIVE;
1357-
break;
1358-
case ServiceState.InActive:
1359-
state_flags = SERVICE_STATE.SERVICE_INACTIVE;
1360-
break;
1361-
default:
1362-
throw new ArgumentException("Invalid state.", nameof(state));
1312+
return scm.GetServices(state, service_types);
13631313
}
1364-
return GetServices(machine_name, state_flags, service_types);
13651314
}
13661315

13671316
/// <summary>
@@ -1404,7 +1353,7 @@ public static ServiceType GetDriverTypes()
14041353
/// <returns>A list of registered services.</returns>
14051354
public static IEnumerable<Win32Service> GetServices()
14061355
{
1407-
return GetServices(null, SERVICE_STATE.SERVICE_STATE_ALL, GetServiceTypes());
1356+
return GetServices(null, ServiceState.All, GetServiceTypes());
14081357
}
14091358

14101359
/// <summary>
@@ -1413,7 +1362,7 @@ public static IEnumerable<Win32Service> GetServices()
14131362
/// <returns>A list of all active running services with process IDs.</returns>
14141363
public static IEnumerable<Win32Service> GetRunningServicesWithProcessIds()
14151364
{
1416-
return GetServices(null, SERVICE_STATE.SERVICE_ACTIVE, GetServiceTypes());
1365+
return GetServices(null, ServiceState.Active, GetServiceTypes());
14171366
}
14181367

14191368
/// <summary>
@@ -1422,7 +1371,7 @@ public static IEnumerable<Win32Service> GetRunningServicesWithProcessIds()
14221371
/// <returns>A list of all drivers.</returns>
14231372
public static IEnumerable<Win32Service> GetDrivers()
14241373
{
1425-
return GetServices(null, SERVICE_STATE.SERVICE_STATE_ALL, GetDriverTypes());
1374+
return GetServices(null, ServiceState.All, GetDriverTypes());
14261375
}
14271376

14281377
/// <summary>
@@ -1431,7 +1380,7 @@ public static IEnumerable<Win32Service> GetDrivers()
14311380
/// <returns>A list of all active running drivers.</returns>
14321381
public static IEnumerable<Win32Service> GetRunningDrivers()
14331382
{
1434-
return GetServices(null, SERVICE_STATE.SERVICE_ACTIVE, GetDriverTypes());
1383+
return GetServices(null, ServiceState.Active, GetDriverTypes());
14351384
}
14361385

14371386
/// <summary>
@@ -1440,7 +1389,7 @@ public static IEnumerable<Win32Service> GetRunningDrivers()
14401389
/// <returns>A list of all services and drivers.</returns>
14411390
public static IEnumerable<Win32Service> GetServicesAndDrivers()
14421391
{
1443-
return GetServices(null, SERVICE_STATE.SERVICE_STATE_ALL,
1392+
return GetServices(null, ServiceState.All,
14441393
GetDriverTypes() | GetServiceTypes());
14451394
}
14461395

@@ -1450,7 +1399,7 @@ public static IEnumerable<Win32Service> GetServicesAndDrivers()
14501399
/// <returns>A list of all services and drivers.</returns>
14511400
public static IEnumerable<Win32Service> GetRunningServicesAndDrivers()
14521401
{
1453-
return GetServices(null, SERVICE_STATE.SERVICE_ACTIVE,
1402+
return GetServices(null, ServiceState.Active,
14541403
GetDriverTypes() | GetServiceTypes());
14551404
}
14561405

0 commit comments

Comments
 (0)