public static HIDDevice FindDevice(int nVid, int nPid, Type oType) { string str = $"vid_{nVid:x4}&pid_{nPid:x4}"; Guid hIDGuid = Win32Usb.HIDGuid; IntPtr lpDeviceInfoSet = Win32Usb.SetupDiGetClassDevs(ref hIDGuid, null, IntPtr.Zero, 0x12); try { Win32Usb.DeviceInterfaceData structure = new Win32Usb.DeviceInterfaceData(); structure.Size = Marshal.SizeOf(structure); for (int i = 0; Win32Usb.SetupDiEnumDeviceInterfaces(lpDeviceInfoSet, 0, ref hIDGuid, (uint)i, ref structure); i++) { string devicePath = GetDevicePath(lpDeviceInfoSet, ref structure); if (devicePath.IndexOf(str) >= 0) { HIDDevice device = (HIDDevice)Activator.CreateInstance(oType); device.Initialise(devicePath); return(device); } } } catch (Exception exception) { throw HIDDeviceException.GenerateError(exception.ToString()); } finally { Win32Usb.SetupDiDestroyDeviceInfoList(lpDeviceInfoSet); } return(null); }
// Token: 0x0600002B RID: 43 RVA: 0x00003FA4 File Offset: 0x000021A4 public static HIDDevice FindDevice(int nVid, int nPid, Type oType) { string empty = string.Empty; string value = string.Format("vid_{0:x4}&pid_{1:x4}", nVid, nPid); Guid hidguid = Win32Usb.HIDGuid; IntPtr intPtr = Win32Usb.SetupDiGetClassDevs(ref hidguid, null, IntPtr.Zero, 18u); try { Win32Usb.DeviceInterfaceData deviceInterfaceData = default(Win32Usb.DeviceInterfaceData); deviceInterfaceData.Size = Marshal.SizeOf(deviceInterfaceData); int num = 0; while (Win32Usb.SetupDiEnumDeviceInterfaces(intPtr, 0u, ref hidguid, (uint)num, ref deviceInterfaceData)) { string devicePath = HIDDevice.GetDevicePath(intPtr, ref deviceInterfaceData); if (devicePath.IndexOf(value) >= 0) { HIDDevice hiddevice = (HIDDevice)Activator.CreateInstance(oType); hiddevice.Initialise(devicePath); return(hiddevice); } num++; } } catch (Exception ex) { throw HIDDeviceException.GenerateError(ex.ToString()); } finally { Win32Usb.SetupDiDestroyDeviceInfoList(intPtr); } return(null); }
/// <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 = HIDGuid; //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. } // MessageBox.Show("请检查是否插入设备!"); } catch (Exception ex) { throw HIDDeviceException.GenerateError(ex.ToString()); //Console.WriteLine(ex.ToString()); } finally { // Before we go, we have to free up the InfoSet memory reserved by SetupDiGetClassDevs SetupDiDestroyDeviceInfoList(hInfoSet); } //string a = @"\\?\hid#vid_0483&pid_5750#7&2e4989f7&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}"; //HIDDevice oNewDevice1 = (HIDDevice)Activator.CreateInstance(oType); //oNewDevice1.Initialise(a); // initialise it with the device path //return oNewDevice1; return(null); // oops, didn't find our device }
/// <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 = @"#vid_" + nVid.ToString("x4") + "&pid_" + nPid.ToString("x4") + "#"; //string strSearch = string.Format("VID_{0:X4}&PID_{1:X4}", nVid, nPid); // first, build the path search string Guid gHid = HIDGuid; HidD_GetHidGuid(ref gHid); // next, get the GUID from Windows that it uses to represent the HID USB interface IntPtr hInfoSet = SetupDiGetClassDevs(ref gHid, IntPtr.Zero, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); // this gets a list of all HID devices currently connected to the computer (InfoSet) try { var oInterface = new DeviceInterfaceData(); // build up a device interface data block oInterface.Size = Marshal.SizeOf(oInterface); //oInterface.Size = (IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8; // 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, IntPtr.Zero, ref gHid, 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. } } catch (Exception ex) { throw HIDDeviceException.GenerateError(ex.ToString()); //Console.WriteLine(ex.ToString()); } 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 }
/// <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) { string strPath = string.Empty; string strSearch = string.Format("vid_{0:x4}&pid_{1:x4}", nVid, nPid); // first, build the path search string Guid gHid = HIDGuid; //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 = new HIDDevice(); 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. } } catch(Exception ex) { throw HIDDeviceException.GenerateError(ex.ToString()); //Console.WriteLine(ex.ToString()); } 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 }
/// <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 nProduct) { string strPath = string.Empty; string strSearch = string.Format("vid_{0:x4}&pid_{1:x4}", nVid, nPid); // first, build the path search string Guid gHid = HIDGuid; // next, get the GUID from Windows that it uses to represent the HID USB interface //HidD_GetHidGuid(out gHid); // this gets a list of all HID devices currently connected to the computer (InfoSet) IntPtr hInfoSet = SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); //IntPtr hInfoSet = SetupDiGetClassDevs(ref gHid, "USB", IntPtr.Zero, DIGCF_ALLCLASSES | DIGCF_PRESENT); 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; // this gets the device interface information for a device at index 'nIndex' in the memory block while (SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, (uint)nIndex, ref oInterface)) { 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 if (String.IsNullOrEmpty(nProduct)) { return(oNewDevice); // возвращяем первый попавшийся девайс, если нет предпочтения определённому производителю } else { string deviceProduct = null; byte[] arrBuff = new byte[255]; // читаем производителя с девайса HidD_GetProductString(oNewDevice.m_hHandle, arrBuff, arrBuff.Length); // переводим массив в строку foreach (char b in arrBuff) { // нет проверки на спец знаки\символы if (b != 0) { deviceProduct += b.ToString(); } } // от возникновения мелких ошибок написания в разных регистрах nProduct = nProduct.ToLower(); deviceProduct = deviceProduct.ToLower(); // ну и проверяем - тот ли производитель if (String.Equals(nProduct, deviceProduct)) { return(oNewDevice); } else { oNewDevice.Dispose(); // закрываем девайс, он нам не подходит } } } nIndex++; // if we get here, we didn't find our device. So move on to the next one. } } catch (Exception ex) { throw HIDDeviceException.GenerateError(ex.ToString()); //Console.WriteLine(ex.ToString()); } 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 }