Ejemplo n.º 1
0
        ///  <summary>
        ///  Call HID functions that use Win32 API functions to locate a HID-class device
        ///  by its Vendor ID and Product ID. Open a handle to the device.
        ///  </summary>
        ///
        ///  <returns>
        ///   True if the device is detected, False if not detected.
        ///  </returns>

        private Boolean FindTheHid()
        {
            var    devicePathName   = new String[128];
            String myDevicePathName = "";

            try
            {
                _deviceHandleObtained = false;
                CloseCommunications();
                _myVendorId  = 0x16c0;
                _myProductId = 0x05df;
                // Get the HID-class GUID.
                Guid hidGuid = _myHid.GetHidGuid();

                //  Fill an array with the device path names of all attached HIDs.
                Boolean availableHids = _myDeviceManagement.FindDeviceFromGuid(hidGuid, ref devicePathName);

                //  If there is at least one HID, attempt to read the Vendor ID and Product ID
                //  of each device until there is a match or all devices have been examined.

                if (availableHids)
                {
                    Int32 memberIndex = 0;
                    do
                    {
                        // Open the handle without read/write access to enable getting information about any HID, even system keyboards and mice.
                        _hidHandle = _myHid.OpenHandle(devicePathName[memberIndex], false);

                        if (!_hidHandle.IsInvalid)
                        {
                            // The returned handle is valid,
                            // so find out if this is the device we're looking for.

                            //_myHid.DeviceAttributes.Size = Marshal.SizeOf(_myHid.DeviceAttributes);

                            Boolean success = _myHid.GetAttributes(_hidHandle, ref _myHid.DeviceAttributes);
                            if (success)
                            {
                                //Debug.WriteLine("  HIDD_ATTRIBUTES structure filled without error.");
                                //Debug.WriteLine("  Structure size: " + _myHid.DeviceAttributes.Size);
                                //Debug.WriteLine("  Vendor ID: " + Convert.ToString(_myHid.DeviceAttributes.VendorID, 16));
                                //Debug.WriteLine("  Product ID: " + Convert.ToString(_myHid.DeviceAttributes.ProductID, 16));
                                //Debug.WriteLine("  Version Number: " + Convert.ToString(_myHid.DeviceAttributes.VersionNumber, 16));

                                if ((_myHid.DeviceAttributes.VendorID == _myVendorId) && (_myHid.DeviceAttributes.ProductID == _myProductId))
                                {
                                    //Debug.WriteLine("  Handle obtained to my device");
                                    //  Display the information in form's list box.
                                    //InfoBox.Text += "\nHandle obtained to my device:";
                                    InfoBox.Text          = "  VID=" + Convert.ToString(_myHid.DeviceAttributes.VendorID, 16);
                                    InfoBox.Text         += "  PID=" + Convert.ToString(_myHid.DeviceAttributes.ProductID, 16);
                                    _deviceHandleObtained = true;

                                    myDevicePathName = devicePathName[memberIndex];
                                }
                                else
                                {
                                    //  It's not a match, so close the handle.

                                    _deviceHandleObtained = false;
                                    _hidHandle.Close();
                                }
                            }
                            else
                            {
                                //  There was a problem retrieving the information.

                                //Debug.WriteLine("  Error in filling HIDD_ATTRIBUTES structure.");
                                _deviceHandleObtained = false;
                                _hidHandle.Close();
                            }
                        }

                        //  Keep looking until we find the device or there are no devices left to examine.

                        memberIndex = memberIndex + 1;
                    }while (!((_deviceHandleObtained || (memberIndex == devicePathName.Length))));
                }

                if (_deviceHandleObtained)
                {
                    //  The device was detected.
                    //  Learn the capabilities of the device.

                    _myHid.Capabilities = _myHid.GetDeviceCapabilities(_hidHandle);

                    //  Find out if the device is a system mouse or keyboard.
                    _hidUsage = _myHid.GetHidUsage(_myHid.Capabilities);

                    //Close the handle and reopen it with read/write access.
                    _hidHandle.Close();
                    _hidHandle = _myHid.OpenHandle(myDevicePathName, true);
                    if (_hidHandle.IsInvalid)
                    {
                        InfoBox.Text += "The device is a system " + _hidUsage + ".";
                    }
                    else
                    {
                        if (_myHid.Capabilities.InputReportByteLength > 0)
                        {
                            //  Set the size of the Input report buffer.
                            var inputReportBuffer = new Byte[_myHid.Capabilities.InputReportByteLength];
                            _deviceData    = new FileStream(_hidHandle, FileAccess.Read | FileAccess.Write, inputReportBuffer.Length, false);
                            inputReportBuf = new Byte[_myHid.Capabilities.InputReportByteLength];
                        }

                        if (_myHid.Capabilities.OutputReportByteLength > 0)
                        {
                            Byte[] outputReportBuffer = null;
                        }
                        //  Flush any waiting reports in the input buffer. (optional)
                        _myHid.FlushQueue(_hidHandle);
                    }
                    ErrorBox.Text = "";
                }
                else
                {
                    ErrorBox.Text = "Device not found.";
                }
                return(_deviceHandleObtained);
            }
            catch (Exception ex)
            {
                DisplayException(Name, ex);
                throw;
            }
        }