示例#1
0
        private void DetectHIDDevices(bool throwOnFail)
        {
            if (_devicesOpen)
            {
                CloseDevices(false);
            }
            var success = BIUSB.DetectHID(out _numDevices, _deviceList, BIUSB.DT_HID);

            if (throwOnFail)
            {
                var lastError = Marshal.GetLastWin32Error();
                //check to see if a Win32 error was returned as a result of
                //the previous calls
                if (lastError != 0)
                {
                    //a Win32 API error occured, so throw it as a wrapped exception
                    Exception e = new Win32Exception(lastError);
                    throw new OperationFailedException(e.Message, e);
                }
            }
            if (!success && throwOnFail)
            {
                throw new OperationFailedException("Failure detecting devices or no devices found.");
            }
        }
示例#2
0
        private static int ReadInputData(ref DeviceParam device, out byte[] databuffer, bool throwOnFail)
        {
            databuffer = new byte[BIUSB.MAX_INPUTS];
            var result = BIUSB.ReadInputData(ref device, databuffer, 0);

            if (throwOnFail)
            {
                var lastError = Marshal.GetLastWin32Error();
                //check to see if a Win32 error was returned as a result of
                //the previous calls
                if (lastError != 0)
                {
                    //a Win32 API error occured, so throw it as a wrapped exception
                    Exception e = new Win32Exception(lastError);
                    throw new OperationFailedException(e.Message, e);
                }
                switch (result)
                {
                case BIUSB.DEV_TIMEOUT:
                    throw new TimeoutException("Device did not respond within 1 second.");

                case BIUSB.DEV_FAILED:
                    throw new OperationFailedException("Failure reading from device.");
                }
            }
            return(result);
        }
示例#3
0
        private void CloseDevices(bool throwOnFail)
        {
            var success = BIUSB.CloseDevices(_numDevices, _deviceList);

            if (throwOnFail)
            {
                var lastError = Marshal.GetLastWin32Error();
                //check to see if a Win32 error was returned as a result of
                //the previous calls
                if (lastError != 0)
                {
                    //a Win32 API error occured, so throw it as a wrapped exception
                    Exception e = new Win32Exception(lastError);
                    throw new OperationFailedException(e.Message, e);
                }
            }
            if (!success && throwOnFail)
            {
                throw new OperationFailedException("Failure closing devices or releasing memory.");
            }
            _devicesOpen &= !success;
        }