public static bool DeviceIoControl <TIn, TOut>(SafeFileHandle hDev, uint ioControlCode, TIn inVal, out TOut outVal) where TIn : struct where TOut : struct { using (SafeHGlobalHandle ptrIn = SafeHGlobalHandle.Alloc(inVal), ptrOut = SafeHGlobalHandle.Alloc <TOut>()) { uint bRet; var ret = Kernel32.DeviceIoControl(hDev, ioControlCode, (IntPtr)ptrIn, (uint)ptrIn.Size, (IntPtr)ptrOut, (uint)ptrOut.Size, out bRet, IntPtr.Zero); outVal = ptrOut.ToStructure <TOut>(); return(ret); } }
public static bool QueryServiceConfig2 <T>(IntPtr hService, ServiceConfigOption dwInfoLevel, out T configInfo) { var b = QueryServiceConfig2(hService, dwInfoLevel, IntPtr.Zero, 0, out uint size); configInfo = default(T); if (!b && Win32Error.GetLastError() != Win32Error.ERROR_INSUFFICIENT_BUFFER) { return(false); } if (size < Marshal.SizeOf(typeof(T))) { throw new ArgumentException("Type mismatch", nameof(configInfo)); } using (var buf = new SafeHGlobalHandle((int)size)) { if (!QueryServiceConfig2(hService, dwInfoLevel, (IntPtr)buf, size, out size)) { return(false); } configInfo = buf.ToStructure <T>(); } return(true); }