using System.Runtime.InteropServices;
using System.Runtime.Versioning;
namespace Chronos.Server.Hosting.Windows;
/// Service Control Manager and event log functions of advapi32 (no Windows-only package needed).
[SupportedOSPlatform("windows")]
internal static unsafe partial class ServiceNative
{
public const uint ServiceWin32OwnProcess = 0x10;
public const uint ServiceAutoStart = 2;
public const uint ServiceErrorNormal = 1;
public const uint ScManagerConnect = 0x1;
public const uint ScManagerCreateService = 0x2;
public const uint ServiceAllAccess = 0xF01FF;
public const uint ServiceQueryStatus = 0x4;
public const uint ServiceStop = 0x20;
public const uint Delete = 0x10000;
public const uint ServiceConfigDescription = 1;
public const uint ControlStop = 1;
public const uint ControlInterrogate = 4;
public const uint ControlShutdown = 5;
public const int ErrorFailedServiceControllerConnect = 1063;
public const int ErrorServiceNotActive = 1062;
public const int ErrorServiceMarkedForDelete = 1072;
public const uint NoError = 0;
public const uint ErrorCallNotImplemented = 120;
public const uint ErrorExceptionInService = 1064;
public const ushort EventLogErrorType = 0x1;
[StructLayout(LayoutKind.Sequential)]
public struct ServiceStatus
{
public uint ServiceType;
public uint CurrentState;
public uint ControlsAccepted;
public uint Win32ExitCode;
public uint ServiceSpecificExitCode;
public uint CheckPoint;
public uint WaitHint;
}
[StructLayout(LayoutKind.Sequential)]
public struct ServiceTableEntry
{
public char* ServiceName;
public delegate* unmanaged[Stdcall] ServiceProc;
}
[StructLayout(LayoutKind.Sequential)]
public struct ServiceDescription
{
public char* Description;
}
[LibraryImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool StartServiceCtrlDispatcherW(ServiceTableEntry* table);
[LibraryImport("advapi32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
public static partial nint RegisterServiceCtrlHandlerExW(string serviceName, delegate* unmanaged[Stdcall] handler, nint context);
[LibraryImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool SetServiceStatus(nint statusHandle, ServiceStatus* status);
[LibraryImport("advapi32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
public static partial nint OpenSCManagerW(string? machineName, string? databaseName, uint desiredAccess);
[LibraryImport("advapi32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
public static partial nint CreateServiceW(nint manager, string serviceName, string displayName, uint desiredAccess, uint serviceType, uint startType,
uint errorControl, string binaryPathName, string? loadOrderGroup, nint tagId, string? dependencies, string? serviceStartName, string? password);
[LibraryImport("advapi32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
public static partial nint OpenServiceW(nint manager, string serviceName, uint desiredAccess);
[LibraryImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool ChangeServiceConfig2W(nint service, uint infoLevel, void* info);
[LibraryImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool ControlService(nint service, uint control, ServiceStatus* status);
[LibraryImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool DeleteService(nint service);
[LibraryImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool CloseServiceHandle(nint handle);
[LibraryImport("advapi32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
public static partial nint RegisterEventSourceW(string? uncServerName, string sourceName);
[LibraryImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool ReportEventW(nint eventLog, ushort type, ushort category, uint eventId, nint userSid, ushort stringCount, uint dataSize,
char** strings, void* rawData);
[LibraryImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool DeregisterEventSource(nint eventLog);
}