// Establish a Request filter opportunistic lock private static IAsyncResult BeginFilter(AsyncCallback asyncCallback, Object state) { // See FSCTL_REQUEST_FILTER_OPLOCK in Platform SDK's WinIoCtl.h file DeviceControlCode RequestFilterOpLock = new DeviceControlCode(DeviceType.FileSystem, 23, DeviceMethod.Buffered, DeviceAccess.Any); return m_device.BeginControl(RequestFilterOpLock, null, asyncCallback, state); }
public static extern bool DeviceIoControl(SafeFileHandle driveHandle , DeviceControlCode ioControlCode , IntPtr lpInBuffer , uint inBufferSize , SafeHandle lpOutBuffer , uint outBufferSize , ref uint lpBytesReturned , IntPtr lpOverlapped);
public static extern bool DeviceIoControl( [In] SafeFileHandle hDevice, [In][MarshalAs(UnmanagedType.U4)] DeviceControlCode dwIoControlCode, [In, Optional][MarshalAs(UnmanagedType.LPArray)] byte[] lpInBuffer, [In][MarshalAs(UnmanagedType.U4)] uint nInBufferSize, [Out, Optional][MarshalAs(UnmanagedType.LPArray)] byte[] lpOutBuffer, [In][MarshalAs(UnmanagedType.U4)] uint nOutBufferSize, [Out, Optional][MarshalAs(UnmanagedType.U4)] out uint lpBytesReturned, [In, Out, Optional] IntPtr lpOverlapped);
internal static Tuple <T, byte[]> DeviceIoControlAction <T, U>(SafeFileHandle safeHandle, DeviceControlCode controlCode, U input, int outputSize = Win32Constants.SIZE_OUTPUT) where T : struct where U : struct { var inputSize = Marshal.SizeOf(input); var inputPtr = Marshal.AllocHGlobal(inputSize); Marshal.StructureToPtr(input, inputPtr, false); byte[] inputBuffer = new byte[inputSize]; byte[] outputBuffer = new byte[outputSize]; Marshal.Copy(inputPtr, inputBuffer, 0, inputSize); Marshal.FreeHGlobal(inputPtr); if (!NativeMethods.DeviceIoControl( safeHandle, controlCode, inputBuffer, (uint)inputBuffer.Length, outputBuffer, (uint)outputBuffer.Length, out uint bytesReturned, IntPtr.Zero)) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } var outputPtr = Marshal.AllocHGlobal(outputBuffer.Length); Marshal.Copy(outputBuffer, 0, outputPtr, outputBuffer.Length); T result = Marshal.PtrToStructure <T>(outputPtr); Marshal.FreeHGlobal(outputPtr); return(new Tuple <T, byte[]>(result, outputBuffer)); }
private static void LCDBrightnessForMsdnMagazine() { DeviceControlCode s_SetBrightness = new DeviceControlCode(DeviceType.Video, 0x127, DeviceMethod.Buffered, DeviceAccess.Any); using (DeviceIO lcd = new DeviceIO(@"\\.\LCD", FileAccess.ReadWrite, FileShare.ReadWrite, false)) { for (Int32 times = 0; times < 10; times++) { for (Int32 dim = 0; dim <= 100; dim += 20) { Win32DisplayBrightnessStructure db = new Win32DisplayBrightnessStructure(Win32DisplayBrightnessStructure.DisplayPowerFlags.ACDC, (Byte)((times % 2 == 0) ? dim : 100 - dim)); lcd.Control(s_SetBrightness, db); Thread.Sleep(150); } } } }