windows Windows下检索USB键盘的序列号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/723449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:15:51  来源:igfitidea点击:

retrieving the serial number of a USB keyboard under Windows

windowshardwareusb

提问by zaphod

Many USB devices contain a unique serial number (which is actually a Unicode string) which the host can use in conjunction with the 16-bit vendor and product ID numbers to uniquely identify the device.

许多 USB 设备包含一个唯一的序列号(实际上是一个 Unicode 字符串),主机可以将其与 16 位供应商和产品 ID 号结合使用来唯一地标识设备。

I'm trying to figure out how to write a Windows application that would be able to display a list of all USB human interface devices attached to the system. The list would have one row for each HID, including system keyboards. There would be columns in the list for the vendor ID, product ID, and serial number.

我试图弄清楚如何编写一个 Windows 应用程序,该应用程序能够显示连接到系统的所有 USB 人机接口设备的列表。该列表将为每个 HID 包含一行,包括系统键盘。列表中会有供应商 ID、产品 ID 和序列号的列。

I can get a list of USB HIDs by calling SetupDiGetClassDevswith the GUID returned by HidD_GetHidGuidand looping through the result by repeatedly calling SetupDiEnumDeviceInterfaces. I can then call SetupDiGetDeviceInterfaceDetailto get the path to each device, which I can open with CreateFile, so long as I am careful to request neither read nor write permission, which would be denied for a system keyboard. From there I can get the vendor and product ID numbers by invoking HidD_GetAttributes.

我可以通过调用SetupDiGetClassDevs返回的 GUIDHidD_GetHidGuid并通过重复调用返回的结果来获取 USB HID 列表SetupDiEnumDeviceInterfaces。然后,我可以调用SetupDiGetDeviceInterfaceDetail以获取每个设备的路径,我可以使用 将其打开CreateFile,只要我小心地请求既不读取也不写入权限,这将被系统键盘拒绝。从那里我可以通过调用HidD_GetAttributes.

What I'm having trouble figuring out is how to retrieve the serial number string. When I search for solutions to this problem, I find a lot of information about how to get serial numbers for USB mass storage devices, but nothing that looks like it might apply to any other type of USB device. I would be happy to discover either a generic method or a HID-specific method of retrieving the serial number string.

我无法弄清楚如何检索序列号字符串。当我搜索这个问题的解决方案时,我找到了很多关于如何获取 USB 大容量存储设备的序列号的信息,但没有任何信息看起​​来可能适用于任何其他类型的 USB 设备。我很高兴发现检索序列号字符串的通用方法或特定于 HID 的方法。

I have a feeling that the Win32 port of libusbcould manage this without too much trouble, but unfortunately I need a solution that depends only on libraries that come with Windows, such as the setupapiand hidDLLs that contain the functions mentioned above.

我有一种感觉,Win32 端口libusb可以毫不费力地管理它,但不幸的是,我需要一个仅依赖于 Windows 附带的库的解决方案,例如包含上述函数的 DLLsetupapihidDLL。

Any suggestions would be very much appreciated!

任何建议将不胜感激!

采纳答案by Alphaneo

Have you tried the USBVIew source code that comes along with the DDK. The USBView tool displays serial number for any USB device, and the source is shipped with the DDK.

您是否尝试过DDK附带的USBVIew源代码。USBView 工具显示任何 USB 设备的序列号,源代码随 DDK 一起提供。

回答by zaphod

It turns out that HID.dlldefines a function called HidD_GetSerialNumberStringthat does exactly what I want, given the handle I got from CreateFileas described above. Just tried it out and it works great. There are also HidD_GetManufacturerStringand HidD_GetProductStringfunctions to retrieve the other string descriptors referred to in the device descriptor, and even a HidD_GetIndexedStringto get an arbitrary string descriptor given its index (presumably because the HID descriptor is allowed to contain string descriptor indices). I feel pretty silly now -- the answer was right there under my nose this whole time.

事实证明,鉴于我从上面获得的句柄,它HID.dll定义了一个函数HidD_GetSerialNumberString,它完全符合我的要求CreateFile。刚刚试了一下,效果很好。还有HidD_GetManufacturerStringHidD_GetProductString函数可以检索设备描述符中引用的其他字符串描述符,甚至HidD_GetIndexedString可以获取给定索引的任意字符串描述符(大概是因为允许 HID 描述符包含字符串描述符索引)。我现在觉得很傻——答案一直就在我眼皮底下。

Thank you all for taking the time to read and answer my question! I'm going to go ahead and accept Alphaneo's answer since it sounds quite promising, and in fact I was waiting for the DDK to download when I stumbled across this answer.

感谢大家花时间阅读并回答我的问题!我将继续接受 Alphaneo 的答案,因为它听起来很有希望,事实上,当我偶然发现这个答案时,我正在等待 DDK 下载。

回答by Philibert Perusse

Have you tried searching for the documentation of the HID definition of input records, output records and features records for Hid keyboards. This should show you the list of "things" you can get out/in of a keyboard through HID.

您是否尝试过搜索 HID 键盘输入记录、输出记录和功能记录的 HID 定义文档。这应该会向您显示可以通过 HID 离开/进入键盘的“事物”列表。

Also, I know it is possible to enumerate the HID record definition by software. I did something similar about 1 year ago, but I cannot remember the details at the top of my head. Doing so would allow you to see what the keyboard USB class is publishing as a standard interface.

另外,我知道可以通过软件枚举 HID 记录定义。大约 1 年前我做过类似的事情,但我不记得头顶上的细节。这样做将允许您查看键盘 USB 类作为标准接口发布的内容。

I hope it can get you a few pointers to find out what you are looking for. Sorry I could not be more precise!

我希望它可以为您提供一些指导,以找出您要查找的内容。对不起,我不能更准确!

回答by RS Conley

I recommend this book USB Complete. Chapter 4 Enumeration: How the Host Learns about Devices has the information you need.

我推荐这本书USB Complete。第 4 章枚举:主机如何了解设备有您需要的信息。

This pagehas many links to information and for you links to libraries and utilities you can use.

该页面有许多信息链接以及您可以使用的库和实用程序的链接。

回答by Srinivasa Raghavan

you can use GetVolumeInformationfor getting the serial number of any hardware attached.

您可以GetVolumeInformation用于获取任何附加硬件的序列号。