public static UsbFilter Create(UsbFilterType type) { var result = new UsbFilter() { u32Magic = USBFILTER_MAGIC, enmType = type, aFields = new UsbFilterField[(int)UsbFilterIdx.END], achStrTab = new byte[256], }; for (var i = 0; i < (int)UsbFilterIdx.END; ++i) { result.aFields[i].enmMatch = UsbFilterMatch.IGNORE; } return(result); }
public async Task AddFilter(ExportedDevice device) { var filter = UsbFilter.Create(UsbFilterType.CAPTURE); filter.SetMatch(UsbFilterIdx.VENDOR_ID, UsbFilterMatch.NUM_EXACT, device.VendorId); filter.SetMatch(UsbFilterIdx.PRODUCT_ID, UsbFilterMatch.NUM_EXACT, device.ProductId); filter.SetMatch(UsbFilterIdx.DEVICE_REV, UsbFilterMatch.NUM_EXACT, device.BcdDevice); filter.SetMatch(UsbFilterIdx.DEVICE_CLASS, UsbFilterMatch.NUM_EXACT, device.DeviceClass); filter.SetMatch(UsbFilterIdx.DEVICE_SUB_CLASS, UsbFilterMatch.NUM_EXACT, device.DeviceSubClass); filter.SetMatch(UsbFilterIdx.DEVICE_PROTOCOL, UsbFilterMatch.NUM_EXACT, device.DeviceProtocol); filter.SetMatch(UsbFilterIdx.PORT, UsbFilterMatch.NUM_EXACT, (ushort)device.DevNum); var output = new byte[Marshal.SizeOf <UsbSupFltAddOut>()]; await UsbMonitor.IoControlAsync(VBoxUsb.IoControl.SUPUSBFLT_IOCTL_ADD_FILTER, StructToBytes(filter), output); var fltAddOut = BytesToStruct <UsbSupFltAddOut>(output, 0); if (fltAddOut.rc != 0 /* VINF_SUCCESS */) { throw new UnexpectedResultException($"SUPUSBFLT_IOCTL_ADD_FILTER failed with returnCode {fltAddOut.rc}"); } }