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