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