0

I've been working on USB HID Device in embedded system and C# for a while. I decided to use USBHid library in C#. I got the ideal result with this library. But I have a problem. While defining the USB in the USBHid library, the following code is sufficient in the project of the library that I found on the internet.

public UsbHidDevice Device;
Device = new UsbHidDevice(vvvv,pppp);

However, when I use the same library, it asks me for an expression in the following format.

public UsbHidDevice Device; string vidandpid = 
"\\hid#vid_0000&pid_0000&mi_00#a&0&000000000&1&0000#{eeof37d0-1963-47k4-aa41-74476db7uf49}";
Device = new UsbHidDevice(vidandpid);

I adapted this format for my own HID device, but without success. How should this string expression be? I am open to your views. Thank you from now.

How to find USB HID DevicePath?

Rand Random
  • 7,300
  • 10
  • 40
  • 88
  • Don’t add tags to question titles, that’s what tags are for. https://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles – Rand Random Jan 14 '23 at 10:51
  • You may consider looking at values for devices such as your keyboard to see the format. Open PowerShell and execute the following command: `Get-CimInstance -Namespace Root\Cimv2 -Query "Select * From Win32_PnPEntity WHERE PnPDeviceID like '%HID%'"`. One can also use `regedit` to view the registry keys/subkeys/values. In the registry look under subkeys in: `HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\HID` – Tu deschizi eu inchid Jan 14 '23 at 20:20
  • The following may also be helpful: [How do I write a backslash (\) in a string?](https://stackoverflow.com/questions/18532691/how-do-i-write-a-backslash-in-a-string) – Tu deschizi eu inchid Jan 14 '23 at 20:22

2 Answers2

1

I gave up on USBHID library and found solution with HidLibrary library. As far as I understand, HidLibrary falls short on some issues.

Here I am sharing the C# code that I linked with HidLibrary. Thank you for all the replies.

device = HidDevices.Enumerate(VendorID, ProductID).FirstOrDefault();
   if (device != null)
   {
       device.OpenDevice();
       device.Inserted += DeviceAttachedHandler; 
       device.Removed += DeviceRemovedHandler;
       device.MonitorDeviceEvents = true;
       device.ReadReport(myfunction);
   }
   else { RtBox_Feedback.AppendText("NOT DEVICE!"); }
0

\\hid#vid_0000&pid_0000&mi_00#a&0&000000000&1&0000#{eeof37d0-1963-47k4-aa41-74476db7uf49} - is a device interface for the {eeof37d0-1963-47k4-aa41-74476db7uf49} interface. It is almost always unique and semi-random for each device. It also may change if yore put device in another USB slot. This string may be used as path to open this "file" with CreateFile Win32 API and talk with device by means of interface-specific IOCTLs. More info here.

For HID devices Windows have another device interface GUID - GUID_DEVINTERFACE_HID - {4D1E55B2-F16F-11CF-88CB-001111000030}

You can use CM_Get_Device_Interface_ListW or SetupDiGetClassDevs/SetupDiEnumDeviceInterfaces/SetupDiGetDeviceInterfaceDetail APIs to enumerate device interfaces by interface GUID or by device Instance ID.

Mentioned "USBHid library in C#" may already have code that doing such enumeration and filtering by HID deivce VID/PID but I cannot say it for sure since you haven't added link to the code of this library. :)

DJm00n
  • 1,083
  • 5
  • 18