internal unsafe VBoxUSB(USBRegistry devreg) { this.Registry = devreg; hDev = Kernel32.CreateFile(devreg.DevicePath, Kernel32.GENERIC_READ | Kernel32.GENERIC_WRITE, Kernel32.FILE_SHARE_WRITE | Kernel32.FILE_SHARE_READ, IntPtr.Zero, Kernel32.OPEN_EXISTING, Kernel32.FILE_ATTRIBUTE_SYSTEM | Kernel32.FILE_FLAG_OVERLAPPED, IntPtr.Zero); if (hDev.IsInvalid) { throw new Win32Exception(); } try { USBSUP_VERSION version = new USBSUP_VERSION(); SyncIoControl(hDev, SUPUSB_IOCTL_GET_VERSION, null, 0, &version, sizeof(USBSUP_VERSION)); if (version.u32Major != USBDRV_MAJOR_VERSION || version.u32Minor < USBDRV_MINOR_VERSION) { throw new InvalidOperationException("Unsupported USBDRV version"); } USBSUP_CLAIMDEV claim = new USBSUP_CLAIMDEV() { bInterfaceNumber = 0 }; SyncIoControl(hDev, SUPUSB_IOCTL_USB_CLAIM_DEVICE, &claim, sizeof(USBSUP_CLAIMDEV), &claim, sizeof(USBSUP_CLAIMDEV)); if (claim.fClaimed == 0) { throw new InvalidOperationException("Claim failed"); } } catch { hDev.Close(); throw; } }
unsafe static void InitMonitor() { if (hMonitor != null && !hMonitor.IsClosed && !hMonitor.IsInvalid) { return; } hMonitor = Kernel32.CreateFile(USBMON_DEVICE_NAME, Kernel32.GENERIC_READ | Kernel32.GENERIC_WRITE, Kernel32.FILE_SHARE_READ | Kernel32.FILE_SHARE_WRITE, IntPtr.Zero, Kernel32.OPEN_EXISTING, Kernel32.FILE_ATTRIBUTE_SYSTEM, IntPtr.Zero); if (hMonitor.IsInvalid) { throw new Win32Exception(); } try { USBSUP_VERSION Version = new USBSUP_VERSION(); SyncIoControl(hMonitor, SUPUSBFLT_IOCTL_GET_VERSION, null, 0, &Version, sizeof(USBSUP_VERSION)); if (Version.u32Major != USBMON_MAJOR_VERSION || Version.u32Minor < USBMON_MINOR_VERSION) { throw new InvalidOperationException("Unsupported USBMON version"); } } catch { hMonitor.Close(); } }