is the USB Instance ID on Windows unique for a device?

混江龙づ霸主 提交于 2019-12-08 13:27:15

问题


I'm trying to identify a USB device uniquely on a Windows system. Since serial number is optional (and empty on most devices), I'm looking at the Instance ID. The field that is like USB\VID_03F0&PID_094A\6&24b73976&0&2.

I already know that this ID changes if I plug the same device into different ports, but that is ok with me. The question is: If I take an identical device (mouse in my example) and plug it in, will it get the same or a different Instance ID ?

I've done some research and nobody seems to quite know what the parts behind VID and PID are. They do seem to identify the port and/or hub, but if I plug a different device into the same slot, I see not just different VIDs and PIDs, but also different garbage after that. For example, here's a keyboard plugged into the same port: USB\VID_046D&PID_C328&MI_00\7&3f9ff46&0&0000

Neither the Microsoft documentation nor the related questions here answer this question.

If I had two similar mice (or keyboards, or whatever), i.e. same manufacturer and model, would they get the same or different Instance IDs if I plug them into the same port?

Do I have any chance to uniquely identify a specific device (not just a model) ?


回答1:


If you look at what Windows does when you plug in a USB device using a USB protocol analyzer, you can see that it starts off by reading USB descriptors. These are binary pieces of data like the device descriptor (which has the vendor ID, product ID and revision), configuration descriptor (which says what interfaces the device supports and what endpoints it uses), string descriptors (like the manufacturer name, product name, and serial number), and other USB descriptors that were added in various extensions to the USB protocol.

I don't know exactly how Windows picks the device instance ID, but it's quite likely that the ID is a function of the bytes in those descriptors, the port you plugged the device into, and nothing else. It's very unlikely that Windows uses any advanced fingerprinting techniques to try to sniff out minute differences between different USB devices, because it seems like wasted effort. I speculate that the main point of the device instance ID is so that Windows can find a registry key (inside HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB) that indicates which drivers it decided to use the last time the device was plugged in, and what settings it was using for those drivers.

There is no universal correlation between the model of a device and its USB descriptors. You could have two similar looking keyboards that have totally different firmware and USB descriptors, just because the manufacturer chose to upgrade the firmware. You could have two different colored keyboards that use the same firmware and thus appear identical to the computer, since they would respond identically to all possible USB messages (that is not just a fantasy from Star Trek).




回答2:


If I take an identical device (mouse in my example) and plug it in, will it get the same or a different Instance ID?

It will get the same ID, unless the original device is still connected. There is just no way for the OS to tell the difference without a unique serial number string.




回答3:


About your first question (in the title):

is the USB Instance ID on Windows unique for a device?

Excerpt from the Microsoft Device Instance ID page:

A device instance ID is a system-supplied device identification string that uniquely identifies a device in the system.

and

A device instance ID is persistent across system restarts.

So, to answer your question:

The System Device ID uniquely identifies a device in a specific Windows system.

Beware of that: its task is to identify the device in this machine. Once you remove the device from the machine, well, the device is not part of this machine anymore.

So, right now, you cannot be sure that the Device Instance ID will be the same if you remove the device from the system, and plug it in again, BUT:
You can be sure that the Device Instance ID will be the same if you restart the system.

Let's go forward, examining your next questions.


About your second question:

If I take an identical device (mouse in my example) and plug it in, will it get the same or a different Instance ID ?

Let's look at the Microsoft Device Instance ID page again:

The format of this string consists of an instance ID concatenated to a device ID, as follows:

<device-ID>\<instance-specific-ID>

and

The following is an example of an instance ID ("1&08") concatenated to a device ID for a PCI device:

PCI\VEN_1000&DEV_0001&SUBSYS_00000000&REV_02\1&08

So, after the USB\VID_xxx&PID_xxx\ part, what you see is the Instance ID (little difference in the name vs System Device ID)

Let's look at the Microsoft Instance ID page:

An instance ID is a device identification string that distinguishes a device from other devices of the same type on a computer. An instance ID contains serial number information, if supported by the underlying bus, or some kind of location information

and

The UniqueID member of the DEVICE_CAPABILITIES structure for a device indicates if a bus-supplied instance ID is unique across the system, as follows:

  • If UniqueID is FALSE, the bus-supplied instance ID for a device is unique only to the device's bus. The Plug and Play (PnP) manager modifies the bus-supplied instance ID, and combines it with the corresponding device ID, to create a device instance ID that is unique in the system.
  • If UniqueID is TRUE, the device instance ID, formed from the bus-supplied device ID and instance ID, uniquely identifies a device in the system.

So, to answer your question:

  • If UniqueID is TRUE, the Device Instance ID will be the same, even if you move the device to a different USB port (and I add: this is what happens when a USB Device provides a Serial Number)
  • If UniqueID is FALSE, you cannot be sure that the bus-supplied Instance ID will be the same, and so you cannot be sure that the whole Device Instance ID will be the same. (However, the Instance ID should be the same across system restarts, and so if you have device1 and device2 which are identical, and swap them during a system restart, I assume that the Instance ID will be the same, and so the Device Instance ID will be the same too! Extending this, if device1 and device2 are the same device, you could simply remove device1 and replug it during a system restart, and the Device Instance ID should be the same!)

This is because the System Device ID's task is to identify devices in the system, not in the whole world (and so, devices detached from the system).

This answers your third question, too:

If I had two similar mice (or keyboards, or whatever), i.e. same manufacturer and model, would they get the same or different Instance IDs if I plug them into the same port?


About your last question:

Do I have any chance to uniquely identify a specific device (not just a model) ?

Yes, and (speaking of USB again) you can uniquely identify a specific device even in the whole world, IF the manufacturer provides a Serial Number on the USB bus, AND it guarantees you that the Serial Number is unique for that specific (VID,PID) pair. It's a very hard constraint, but for example consider a USB WiFi card (i have a Netgear one here):

  • It provides the USB Serial Number
  • The USB Serial Number is exactly its MAC address

Since MAC addresses are, by definition, unique, you can be sure that you can uniquely identify that device, even if it's plugged in a different machine.

However, you cannot identify uniquely all devices, or a specific device of your choosing. It must satisfy these requirements.


Additional tests

I tested the aforementioned Netgear USB WiFi card, and it has the same Device Instance ID, even when plugged in different ports, and even when plugged in different machines.
I tested a generic USB key which provides a USB Serial number, and its behavior was the same as the USB WiFi card.
In these cases, the Device Instance ID will be something like:

USB\VID_1221&PID_3234\00004700356

I tested two identical generic USB keyboards, and I plugged them (one at a time) in the same USB hub port. The Device Instance ID remained the same (and additionally, Windows didn't show the "Installing hardware" popup when I plugged in the second keyboard. The Device Instance ID was:

USB\VID_1C4F&PID_0002\7&15cdfaa&0&3

Then, I plugged one of the keyboards on a different USB port, and the Device Instance ID changed to:

USB\VID_1C4F&PID_0002\5&2eab04ab&0&1

More references

Microsoft page about USB Identifiers



来源:https://stackoverflow.com/questions/51513337/is-the-usb-instance-id-on-windows-unique-for-a-device

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