gyurisc
gyurisc

Reputation: 11492

How do I get the Vendor and Product strings in case of a HID device on Windows?

I need to get information about the idProduct and idVendor of a plugged in HID device on my Windows machine. How do I get the USB_DEVICE_DESCRIPTOR for a given HID device?

I searched the internet, but I only found examples of devices being queried using the WinUSB library and getting the USB_DEVICE_DESCRIPTOR. My understanding that I cannot use WinUSB for plugged in HID device.

What do I need to use for a HID device then?

Upvotes: 1

Views: 1981

Answers (1)

Jack Humbert
Jack Humbert

Reputation: 139

If you're using HidLibrary, you can get a device like this:

_device = HidDevices.Enumerate(VendorId, ProductId, UsagePage).FirstOrDefault();

if (_device != null) {
    _device.OpenDevice();
    string product = GetProductString(_device);
    string mfg = GetManufacturerString(_device);
}

With the latter two functions defined like this:

    private string GetProductString(HidDevice d) {
        byte[] bs;
        _device.ReadProduct(out bs);
        string ps = "";
        foreach (byte b in bs) {
            if (b > 0)
                ps += ((char)b).ToString();
        }
        return ps;
    }

    private string GetManufacturerString(HidDevice d) {
        byte[] bs;
        _device.ReadManufacturer(out bs);
        string ps = "";
        foreach (byte b in bs) {
            if (b > 0)
                ps += ((char)b).ToString();
        }
        return ps;
    }

Upvotes: 1

Related Questions