Exemple #1
0
 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;
     }
 }
Exemple #2
0
 protected unsafe override void Dispose(Boolean disposing)
 {
     if (!disposing)
     {
         return;
     }
     if (!hDev.IsInvalid && !hDev.IsClosed)
     {
         USBSUP_CLAIMDEV release = new USBSUP_CLAIMDEV()
         {
             bInterfaceNumber = bInterfaceNumber
         };
         SyncIoControl(hDev, SUPUSB_IOCTL_USB_RELEASE_DEVICE, &release, sizeof(USBSUP_CLAIMDEV), null, 0, false);
     }
     hDev.Close();
 }