Ejemplo n.º 1
0
        /// <summary>
        /// Connect to the USB device specified by the DeviceListItem.
        /// </summary>
        protected UsbDevice(DeviceListItem deviceListItem)
        {
            WinUsbDeviceHandles handles;

            try
            {
                handles = Winusb.connect(deviceListItem.guid, deviceListItem.deviceInstance);
            }
            catch (Exception exception)
            {
                if (exception is Win32Exception && ((Win32Exception)exception).NativeErrorCode == 5) // ERROR_ACCESS_DENIED
                {
                    throw new Exception("Access was denied when trying to connect to the device.  "
                                        + "Try closing all programs using the device.");
                }
                else
                {
                    throw new Exception("There was an error connecting to the device.", exception);
                }
            }

            device = new MyWinUsbDevice(handles, deviceListItem.serialNumber);
        }