private static NativeMethods.ServiceConfigurationInfo ReadServiceConfiguration(SafeServiceHandle serviceHandle) { NativeMethods.QueryServiceConfig(serviceHandle, IntPtr.Zero, 0, out uint BytesNeeded); int ErrorCode = Marshal.GetLastWin32Error(); if (ErrorCode != NativeMethods.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(ErrorCode); } IntPtr ptr = Marshal.AllocHGlobal((int)BytesNeeded); try { if (!NativeMethods.QueryServiceConfig(serviceHandle, ptr, BytesNeeded, out BytesNeeded)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } NativeMethods.ServiceConfigurationInfo ServiceConfiguration = new NativeMethods.ServiceConfigurationInfo(); Marshal.PtrToStructure(ptr, ServiceConfiguration); return(ServiceConfiguration); } finally { Marshal.FreeHGlobal(ptr); } }
public static (ServiceConfiguration ServiceConfiguration, string BinaryPath) ReadServiceConfiguration(SafeServiceHandle serviceHandle, string serviceName) { NativeMethods.ServiceConfigurationInfo ServiceConfiguration = ReadServiceConfiguration(serviceHandle); (ServiceAccount Account, string?Username) = ServiceController.ConvertWindowsUsernameToServiceConfiguration(ServiceConfiguration.lpServiceStartName); ServiceStartMode StartMode = ReadServiceDelayedAutoStartConfiguration(serviceHandle) ? ServiceStartMode.AutomaticDelayedStart : (ServiceStartMode)ServiceConfiguration.dwStartType; return( new ServiceConfiguration { ServiceName = serviceName, DisplayName = ServiceConfiguration.lpDisplayName, Description = ReadServiceDescription(serviceHandle), StartMode = StartMode, Account = Account, Username = Username }, ServiceConfiguration.lpBinaryPathName ?? string.Empty); }