How to know if the device instance id is unique?

2.5k Views Asked by At

I am developing a C# windows application using .NET Framework v3.5.

Every USB device should have a unique instance id that distinguishes a it from other devices of the same type on a computer.

In case that the device InstanceID is not unique, the PnP manager modifies the InstanceID, and combines it with the corresponding DeviceTypeID, to create a device instance ID that is unique in the system.

I need to find if the USB Device Instance Id is unique system-wide.

I can use IRP_MN_QUERY_CAPABILITIES to retrieve the device capabilities, and then check the UniqueID member of the DEVICE_CAPABILITIES to indicate if a instance ID is unique across the system.

My questions are:

  1. How can I use IRP_MN_QUERY_CAPABILITIES in c#
  2. Is there a C++ sample code on how to use this query?
  3. Is there any other way to know if the instance id is not unique?
2

There are 2 best solutions below

1
On BEST ANSWER

I found a solution, I used SetupDiGetDeviceRegistryProperty and asked for the DEVICE_CAPABILITIES structure

 private static bool IsDeviceIdUnique(IntPtr usbDev, NativeMethods.SP_DEVINFO_DATA devInfoData,string deviceId)
    {
        uint bufferSize;
        uint propertyRegDataType;
        var buffer = new byte[WinConstants.MAX_PATH];
        NativeMethods.SetupDiGetDeviceRegistryProperty(usbDev, ref devInfoData, WinConstants.SPDRP_CAPABILITIES, out propertyRegDataType, buffer, WinConstants.MAX_PATH, out bufferSize);
        var capabilities = BitConverter.ToUInt32(buffer, 0);
        return (capabilities & WinConstants.CM_DEVCAP_UNIQUEID) == WinConstants.CM_DEVCAP_UNIQUEID;
    }
0
On

Device Instance ID is not unique because there is no standard world wide. And I have worked on USB flash drives. However the device instance could show the company's name, and that distinguishes a device from one company from another. The only unique data that I know of is the long Device Serial number, retrieved from a random number generator. If you're using Windows, you can learn about WMI and its ability to access USB devices.