mirror of https://github.com/winsw/winsw
Internal updates
parent
0df32ccd44
commit
d865678b80
|
@ -100,7 +100,7 @@ namespace WinSW.Native
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <exception cref="CommandException" />
|
/// <exception cref="CommandException" />
|
||||||
internal unsafe (IntPtr Services, int Count) EnumerateServices()
|
internal ServiceEnumerator EnumerateServices()
|
||||||
{
|
{
|
||||||
int resume = 0;
|
int resume = 0;
|
||||||
_ = EnumServicesStatus(
|
_ = EnumServicesStatus(
|
||||||
|
@ -129,7 +129,7 @@ namespace WinSW.Native
|
||||||
Throw.Command.Win32Exception("Failed to enumerate services.");
|
Throw.Command.Win32Exception("Failed to enumerate services.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return (services, count);
|
return new(services, count);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
|
@ -183,6 +183,31 @@ namespace WinSW.Native
|
||||||
|
|
||||||
this.handle = IntPtr.Zero;
|
this.handle = IntPtr.Zero;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal ref struct ServiceEnumerator
|
||||||
|
{
|
||||||
|
private readonly IntPtr services;
|
||||||
|
private readonly int count;
|
||||||
|
|
||||||
|
private int index;
|
||||||
|
|
||||||
|
internal ServiceEnumerator(IntPtr services, int count)
|
||||||
|
{
|
||||||
|
this.services = services;
|
||||||
|
this.count = count;
|
||||||
|
this.index = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public unsafe ENUM_SERVICE_STATUS* Current => (ENUM_SERVICE_STATUS*)this.services + this.index;
|
||||||
|
|
||||||
|
public void Dispose() => Marshal.FreeHGlobal(this.services);
|
||||||
|
|
||||||
|
public ServiceEnumerator GetEnumerator() => this;
|
||||||
|
|
||||||
|
public bool MoveNext() => ++this.index < this.count;
|
||||||
|
|
||||||
|
public void Reset() => throw new NotImplementedException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal ref struct Service
|
internal ref struct Service
|
||||||
|
|
|
@ -10,7 +10,6 @@ using System.IO;
|
||||||
using System.IO.Pipes;
|
using System.IO.Pipes;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
using System.Security.AccessControl;
|
using System.Security.AccessControl;
|
||||||
using System.Security.Principal;
|
using System.Security.Principal;
|
||||||
using System.ServiceProcess;
|
using System.ServiceProcess;
|
||||||
|
@ -889,39 +888,30 @@ namespace WinSW
|
||||||
if (all)
|
if (all)
|
||||||
{
|
{
|
||||||
using var scm = ServiceManager.Open(ServiceManagerAccess.EnumerateService);
|
using var scm = ServiceManager.Open(ServiceManagerAccess.EnumerateService);
|
||||||
(var services, int count) = scm.EnumerateServices();
|
int prevProcessId = -1;
|
||||||
try
|
foreach (var status in scm.EnumerateServices())
|
||||||
{
|
{
|
||||||
int prevProcessId = -1;
|
using var sc = scm.OpenService(status->ServiceName, ServiceAccess.QueryConfig | ServiceAccess.QueryStatus);
|
||||||
for (int i = 0; i < count; i++)
|
if (sc.ExecutablePath.StartsWith($"\"{ExecutablePath}\""))
|
||||||
{
|
{
|
||||||
var status = (ENUM_SERVICE_STATUS*)services + i;
|
int processId = sc.ProcessId;
|
||||||
using var sc = scm.OpenService(status->ServiceName, ServiceAccess.QueryConfig | ServiceAccess.QueryStatus);
|
if (processId >= 0)
|
||||||
if (sc.ExecutablePath.StartsWith($"\"{ExecutablePath}\""))
|
|
||||||
{
|
{
|
||||||
int processId = sc.ProcessId;
|
if (prevProcessId >= 0)
|
||||||
if (processId >= 0)
|
|
||||||
{
|
{
|
||||||
if (prevProcessId >= 0)
|
using var process = Process.GetProcessById(prevProcessId);
|
||||||
{
|
Draw(process, string.Empty, false);
|
||||||
using var process = Process.GetProcessById(prevProcessId);
|
|
||||||
Draw(process, string.Empty, false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
prevProcessId = processId;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (prevProcessId >= 0)
|
prevProcessId = processId;
|
||||||
{
|
|
||||||
using var process = Process.GetProcessById(prevProcessId);
|
|
||||||
Draw(process, string.Empty, true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally
|
|
||||||
|
if (prevProcessId >= 0)
|
||||||
{
|
{
|
||||||
Marshal.FreeHGlobal(services);
|
using var process = Process.GetProcessById(prevProcessId);
|
||||||
|
Draw(process, string.Empty, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -996,23 +986,14 @@ namespace WinSW
|
||||||
static unsafe void DevList()
|
static unsafe void DevList()
|
||||||
{
|
{
|
||||||
using var scm = ServiceManager.Open(ServiceManagerAccess.EnumerateService);
|
using var scm = ServiceManager.Open(ServiceManagerAccess.EnumerateService);
|
||||||
(var services, int count) = scm.EnumerateServices();
|
foreach (var status in scm.EnumerateServices())
|
||||||
try
|
|
||||||
{
|
{
|
||||||
for (int i = 0; i < count; i++)
|
using var sc = scm.OpenService(status->ServiceName, ServiceAccess.QueryConfig);
|
||||||
|
if (sc.ExecutablePath.StartsWith($"\"{ExecutablePath}\""))
|
||||||
{
|
{
|
||||||
var status = (ENUM_SERVICE_STATUS*)services + i;
|
Console.WriteLine(status->ToString());
|
||||||
using var sc = scm.OpenService(status->ServiceName, ServiceAccess.QueryConfig);
|
|
||||||
if (sc.ExecutablePath.StartsWith($"\"{ExecutablePath}\""))
|
|
||||||
{
|
|
||||||
Console.WriteLine(status->ToString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally
|
|
||||||
{
|
|
||||||
Marshal.FreeHGlobal(services);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Customize(string output, string manufacturer)
|
static void Customize(string output, string manufacturer)
|
||||||
|
|
Loading…
Reference in New Issue