internal T GetDataForDevice(DeviceHandle device, IoControlCode controlCode) { bool functionResult = false; IntPtr ptrInputData = IntPtr.Zero; var structureSize = Marshal.SizeOf(typeof(T)); int lpBytesReturned = 0; NativeOverlapped nativeOverlapped = new NativeOverlapped(); int win32Error = 0; try { // First try ptrInputData = Marshal.AllocHGlobal(structureSize); while (functionResult == false) { lock (lockMe) { IntPtr handle = device.OpenDeviceHandle(); functionResult = UnsafeNativeMethods.DeviceIoControl(handle, controlCode, IntPtr.Zero, 0, ptrInputData, structureSize, ref lpBytesReturned, ref nativeOverlapped); win32Error = Marshal.GetLastWin32Error(); device.CloseDeviceHandle(); } if (functionResult == false) { if (win32Error == UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER) { Marshal.FreeHGlobal(ptrInputData); checked { structureSize = structureSize * 2; } ptrInputData = Marshal.AllocHGlobal(structureSize); } else { throw new Win32Exception("Could not acquire information from windows kernel.", new Win32Exception(win32Error)); } } } T data = (T)Marshal.PtrToStructure(ptrInputData, typeof(T)); return(data); } catch (Exception exp_gen) { throw new Win32Exception("Exception occurred while trying to work with Windows kernel", exp_gen); } finally { Marshal.FreeHGlobal(ptrInputData); } }
internal static extern bool DeviceIoControl(IntPtr hDevice, IoControlCode dwIoControlCode, IntPtr lpInBuffer, int nInBufferSize, IntPtr lpOutBuffer, int nOutBufferSize, ref int lpBytesReturned, [In] ref NativeOverlapped lpOverlapped);
static internal extern bool DeviceIoControl(SafeHandle hDevice, IoControlCode dwIoControlCode, SafeHandle lpInBuffer, UInt32 nInBufferSize, SafeHandle lpOutBuffer, UInt32 nOutBufferSize, out UInt32 lpBytesReturned, IntPtr lpOverlapped);
public static extern Boolean DeviceIoControl( FileSafeHandle hDevice, IoControlCode dwIoControlCode, ref DRIVE_LAYOUT_INFORMATION_EX lpInBuffer, int nInBufferSize, IntPtr lpOutBuffer, Int32 nOutBufferSize, ref Int32 lpBytesReturned, IntPtr lpOverlapped );
public static extern int DeviceIoControl( FileSafeHandle device, IoControlCode dwIoControlCode, IntPtr inBuffer, int inBufferSize, IntPtr outBuffer, uint outBufferSize, ref uint bytesReturned, IntPtr overlapped );
public static extern Boolean DeviceIoControl( FileSafeHandle hDevice, IoControlCode dwIoControlCode, ref GrowPartition lpInBuffer, int nInBufferSize, IntPtr lpOutBuffer, Int32 nOutBufferSize, ref Int32 lpBytesReturned, IntPtr lpOverlapped );
public static bool DeviceIoControl(SafeFileHandle fileHandle, IoControlCode ioControlCode) { uint bytesReturned; return(DeviceIoControl(fileHandle.DangerousGetHandle(), ioControlCode, IntPtr.Zero, 0, IntPtr.Zero, 0, out bytesReturned, IntPtr.Zero)); }
public static extern bool DeviceIoControl(IntPtr fileHandle, IoControlCode ioControlCode, IntPtr inBuffer, uint inBufferSize, IntPtr outBuffer, uint outBufferSize, out uint bytesReturned, IntPtr overlapped);