コード例 #1
0
        /// <summary>
        /// Helper method to return the device path given a DeviceInterfaceData structure and an InfoSet handle.
        /// Used in 'FindDevice' so check that method out to see how to get an InfoSet handle and a DeviceInterfaceData.
        /// </summary>
        /// <param name="hInfoSet">Handle to the InfoSet</param>
        /// <param name="oInterface">DeviceInterfaceData structure</param>
        /// <returns>The device path or null if there was some problem</returns>
//      private static string GetDevicePath(IntPtr hInfoSet, ref DeviceInterfaceData oInterface)
//      {
//          uint nRequiredSize = 0;
//          // Get the device interface details
//          if (!SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, IntPtr.Zero, 0, ref nRequiredSize, IntPtr.Zero))
//          {
//              DeviceInterfaceDetailData oDetail = new DeviceInterfaceDetailData();
//              oDetail.Size = 5;	// hardcoded to 5! Sorry, but this works and trying more future proof versions by setting the size to the struct sizeof failed miserably. If you manage to sort it, mail me! Thx
//              if (SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref oDetail, nRequiredSize, ref nRequiredSize, IntPtr.Zero))
//              {
//                  return oDetail.DevicePath;
//              }
//          }
//          return null;
//      }
        #endregion

        #region Public static
        /// <summary>
        /// Finds a device given its PID and VID
        /// </summary>
        /// <param name="nVid">Vendor id for device (VID)</param>
        /// <param name="nPid">Product id for device (PID)</param>
        /// <param name="oType">Type of device class to create</param>
        /// <returns>A new device class of the given type or null</returns>
        public static HIDDevice FindDevice(int nVid, int nPid, Type oType)
        {
            string strPath   = string.Empty;
            string strSearch = string.Format("vid_{0:x4}&pid_{1:x4}", nVid, nPid);             // first, build the path search string
            Guid   gHid;

            HidD_GetHidGuid(out gHid);                                                                                 // next, get the GUID from Windows that it uses to represent the HID USB interface
            IntPtr hInfoSet = SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); // this gets a list of all HID devices currently connected to the computer (InfoSet)

            try
            {
                DeviceInterfaceData oInterface = new DeviceInterfaceData();     // build up a device interface data block
                oInterface.Size = Marshal.SizeOf(oInterface);
                // Now iterate through the InfoSet memory block assigned within Windows in the call to SetupDiGetClassDevs
                // to get device details for each device connected
                int nIndex = 0;
                while (SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, (uint)nIndex, ref oInterface)) // this gets the device interface information for a device at index 'nIndex' in the memory block
                {
                    string strDevicePath = GetDevicePath(hInfoSet, ref oInterface);                      // get the device path (see helper method 'GetDevicePath')
                    //if (strDevicePath.IndexOf(strSearch) >= 0)	// do a string search, if we find the VID/PID string then we found our device!
                    {
                        HIDDevice oNewDevice = (HIDDevice)Activator.CreateInstance(oType); // create an instance of the class for this device
                        oNewDevice.Initialise(strDevicePath);                              // initialise it with the device path
                        return(oNewDevice);                                                // and return it
                    }
                    nIndex++;                                                              // if we get here, we didn't find our device. So move on to the next one.
                }
            }
            finally
            {
                // Before we go, we have to free up the InfoSet memory reserved by SetupDiGetClassDevs
                SetupDiDestroyDeviceInfoList(hInfoSet);
            }
            return(null);        // oops, didn't find our device
        }
コード例 #2
0
ファイル: Report.cs プロジェクト: wangf0228GitHub/WFLib
 /// <summary>
 /// Construction. Setup the buffer with the correct output report length dictated by the device
 /// </summary>
 /// <param name="oDev">Creating device</param>
 public OutputReport(HIDDevice oDev) : base(oDev)
 {
     SetBuffer(new byte[oDev.OutputReportLength]);
 }
コード例 #3
0
ファイル: Report.cs プロジェクト: wangf0228GitHub/WFLib
 /// <summary>
 /// Construction. Do nothing
 /// </summary>
 /// <param name="oDev">Creating device</param>
 public InputReport(HIDDevice oDev) : base(oDev)
 {
 }
コード例 #4
0
ファイル: Report.cs プロジェクト: wangf0228GitHub/WFLib
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="oDev">Constructing device</param>
 public Report(HIDDevice oDev)
 {
     // Do nothing
 }