/// <summary> /// Sends the control code to the device specified by handle. /// </summary> /// <typeparam name="TStructure"></typeparam> /// <param name="handle"></param> /// <param name="code"></param> /// <param name="structure"></param> /// <param name="bufferSize">Maximum size of returned buffer</param> /// <returns></returns> public static bool ControlWithInput <TStructure>( SafeFileHandle handle, FsCtl code, ref TStructure structure, int bufferSize, out byte[] buffer) where TStructure : struct { uint datalen; bool controlResult; buffer = new byte[bufferSize]; var bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned); var structureHandle = GCHandle.Alloc(structure, GCHandleType.Pinned); var bufferPointer = bufferHandle.AddrOfPinnedObject(); var structurePointer = structureHandle.AddrOfPinnedObject(); try { controlResult = DeviceIoControl(handle, (uint)code, structurePointer, (uint)Marshal.SizeOf(structure), bufferPointer, (uint)buffer.Length, out datalen, IntPtr.Zero); } finally { structureHandle.Free(); bufferHandle.Free(); } Array.Resize(ref buffer, (int)datalen); return(controlResult); }
/// <summary> /// Sends the control code to the device specified by handle. /// </summary> /// <typeparam name="TStructure"></typeparam> /// <param name="handle"></param> /// <param name="code"></param> /// <param name="structure"></param> internal static bool ControlWithOutput <TStructure>( SafeFileHandle handle, FsCtl code, ref TStructure structure) where TStructure : struct { bool controlResult; //get our object pointer var structureHandle = GCHandle.Alloc(structure, GCHandleType.Pinned); var structurePointer = structureHandle.AddrOfPinnedObject(); try { controlResult = DeviceIoControl(handle, (uint)code, IntPtr.Zero, 0, structurePointer, (uint)Marshal.SizeOf(structure), out _, IntPtr.Zero); } finally { // always release GH handle structureHandle.Free(); } if (controlResult) { structure = (TStructure)Marshal.PtrToStructure(structurePointer, typeof(TStructure)); } return(controlResult); }
public static extern bool DeviceIoControl( SafeFileHandle hDevice, FsCtl IoControlCode, [In] ref MFT_ENUM_DATA InBuffer, uint nInBufferSize, [In] IntPtr OutBuffer, uint nOutBufferSize, ref uint pBytesReturned, [In] IntPtr overlapped //[In] ref System.Threading.NativeOverlapped Overlapped );