public static void ChangeServiceDescription(SafeServiceHandle serviceHandle, string?description) { NativeMethods.ServiceDescriptionInfo ServiceDescriptionInfo = new NativeMethods.ServiceDescriptionInfo { lpDescription = description }; IntPtr lpInfo = Marshal.AllocHGlobal(NativeMethods.ServiceDescriptionInfo.SizeOf); try { Marshal.StructureToPtr(ServiceDescriptionInfo, lpInfo, false); if (!NativeMethods.ChangeServiceConfig2( serviceHandle, NativeMethods.SERVICE_CONFIG_DESCRIPTION, lpInfo)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } finally { Marshal.FreeHGlobal(lpInfo); } }
private static string?ReadServiceDescription(SafeServiceHandle serviceHandle) { NativeMethods.QueryServiceConfig2(serviceHandle, NativeMethods.SERVICE_CONFIG_DESCRIPTION, 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_DESCRIPTION, ptr, BytesNeeded, out BytesNeeded)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } NativeMethods.ServiceDescriptionInfo ServiceDescription = new NativeMethods.ServiceDescriptionInfo(); Marshal.PtrToStructure(ptr, ServiceDescription); return(ServiceDescription.lpDescription); } finally { Marshal.FreeHGlobal(ptr); } }