private static bool ReadServiceDelayedAutoStartConfiguration(SafeServiceHandle serviceHandle) { NativeMethods.QueryServiceConfig2(serviceHandle, NativeMethods.SERVICE_CONFIG_DELAYED_AUTO_START_INFO, 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.QueryServiceConfig2(serviceHandle, NativeMethods.SERVICE_CONFIG_DELAYED_AUTO_START_INFO, ptr, BytesNeeded, out BytesNeeded)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } NativeMethods.ServiceDelayedAutoStartInfo ServiceDelayedAutoStart = new NativeMethods.ServiceDelayedAutoStartInfo(); Marshal.PtrToStructure(ptr, ServiceDelayedAutoStart); return(ServiceDelayedAutoStart.fDelayedAutostart); } finally { Marshal.FreeHGlobal(ptr); } }
public static void ChangeServiceDelayedAutoStartConfiguration(SafeServiceHandle serviceHandle, bool useDelayedAutoStart) { NativeMethods.ServiceDelayedAutoStartInfo ServiceDelayedAutoStartInfo = new NativeMethods.ServiceDelayedAutoStartInfo { fDelayedAutostart = useDelayedAutoStart }; IntPtr lpInfo = Marshal.AllocHGlobal(NativeMethods.ServiceDelayedAutoStartInfo.SizeOf); try { Marshal.StructureToPtr(ServiceDelayedAutoStartInfo, lpInfo, false); if (!NativeMethods.ChangeServiceConfig2( serviceHandle, NativeMethods.SERVICE_CONFIG_DELAYED_AUTO_START_INFO, lpInfo)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } finally { Marshal.FreeHGlobal(lpInfo); } }