How to Uniquely Identify an Usb-Device

How to identify UNIQUELY a USB Device?

The closest equivalent of a "unique number" available in any USB device is the VID and PID, the vendor and product IDs. It is used to identify the device and install the proper driver for it. However, it is the same set of numbers for the same product, it is not good enough to identify the particular device you have in your hand. And thus useless to implement a license verification procedure, presumably what you are after.

Only certain kind of USB devices implement a serial number. You get it by accident from a flash drive due to the drive formatter generating a volume serial number. Useless as well, it can easily be changed. A USB wifi adapter could work, the manufacturer must generate a unique MAC address.

There's one class of USB devices that are perfect for this goal. The generic name is "dongle", they are made for this. When you buy one you also get software that you can link into your program that provides a tamper-proof way to verify the license number, another important part of a license verification procedure and usually the weak link. More about dongles in this Wikipedia article.

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

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

What can be used as a unique USB device identifier aside from serial number?

Use USB VID and PID. Not the best way, but atleast it is one. When you are able to use a unique ID, then use it too. You can use hashfunctions to combine them too.

Is there a way to uniquely identify a specific usb port on a host device?

Is there a way that the smartphone (the peripheral in this case) can identify the specific port on the host?

No, only the host knows which port a device is connected to. They all look the same from the phones point of view.

Is there a way it can identify the host device as a whole?
Basically, does the peripheral know anything about the host?

Not with USB per se, but you could implement some identification in a higher protocol.

Look at later implementations of the android debugging protocol for example: The host sends an ID to the device, and unless it matches in the device database the user will be asked: "Do you want PC (ID) to be able to debug this device?"

Uniquely identifying a pendrive

Please use the class USBSerialNumber

USBSerialNumber usb = new USBSerialNumber();
string serial = usb.getSerialNumberFromDriveLetter("f:\");
MessageBox.Show(serial);

Each pendrive has serial number which is unique.

You can use this class for this purpose.

Download it from following link:

http://www.cfdan.com/posts/Retrieving_Non-Volatile_USB_Serial_Number_Using_C_Sharp.cfm



Related Topics



Leave a reply



Submit