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

☆樱花仙子☆ 提交于 2019-12-13 02:07:10

问题


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?


回答1:


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;
    }


来源:https://stackoverflow.com/questions/22611445/how-do-i-get-the-vendor-and-product-strings-in-case-of-a-hid-device-on-windows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!