Using Graphics.Drawimage() to Draw Image with Transparency/Alpha Channel

How do I get a Unique Identifier for a Device within Windows 10 Universal?

That is the complete solution for Windows Desktop:

  • Add the Extension reference "Windows Desktop Extensions for the UWP" like Peter Torr - MSFT mentioned.

Use this Code to get the HardwareId:

using System;
using Windows.Security.ExchangeActiveSyncProvisioning;
using Windows.System.Profile;

namespace Tobit.Software.Device
{
public sealed class DeviceInfo
{
private static DeviceInfo _Instance;
public static DeviceInfo Instance
{
get {
if (_Instance == null)
_Instance = new DeviceInfo();
return _Instance; }

}

public string Id { get; private set; }
public string Model { get; private set; }
public string Manufracturer { get; private set; }
public string Name { get; private set; }
public static string OSName { get; set; }

private DeviceInfo()
{
Id = GetId();
var deviceInformation = new EasClientDeviceInformation();
Model = deviceInformation.SystemProductName;
Manufracturer = deviceInformation.SystemManufacturer;
Name = deviceInformation.FriendlyName;
OSName = deviceInformation.OperatingSystem;
}

private static string GetId()
{
if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.System.Profile.HardwareIdentification"))
{
var token = HardwareIdentification.GetPackageSpecificToken(null);
var hardwareId = token.Id;
var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(hardwareId);

byte[] bytes = new byte[hardwareId.Length];
dataReader.ReadBytes(bytes);

return BitConverter.ToString(bytes).Replace("-", "");
}

throw new Exception("NO API FOR DEVICE ID PRESENT!");
}
}
}

How to get unique Device IDs on Windows 10 Anniversary Update

Updated Nov 3rd 2017 (new Registry value, below)

The Windows 10 Anniversary Update introduced the new SystemIdentification type which does exactly what you want. It has several benefits over the old ASHWID:

  • It is available on all Windows 10 platforms

    • Note: The ASHWID is now also available on all platforms, but still has the other drawbacks listed above
  • It returns a stable value (even on PCs) that will not change due to hardware upgrades or re-installations of the OS
  • It returns a value that is the same for all apps by the same publisher, allowing correlation across your portfolio of apps
  • It can also return a value that is the same across all apps, for a specific user, if you have the userSystemInfo Restricted Capability

    • Note: This is most useful for Enterprise scenarios; you are unlikely to have an app approved for the Windows Store that uses this feature without a very good justification, since it represents a privacy concern

There is one minor drawback to the API: it won't work on some old PCs, since it requires either UEFI or a TPM. Most PCs built in the last 5+ years should have this hardware, and all other non-PC devices (phone, Xbox, HoloLens, etc.) have the correct hardware. If you find a PC that doesn't have the hardware, you will need to fall back to the ASHWID or some other mechanism.

Update Nov 3 2017

Starting with the Windows Fall Creator's Update (aka 1709 or RS3 or Universal API Contract 5) there is a new Registry identification source which provides a relatively stable ID in case the user doesn't have appropriate hardware. It will change if the user does a fresh re-install of the OS (not an upgrade, but a new install) or if the user changes the registry, but otherwise has the same benefits as Uefi or Tmp.

End update Nov 3 2017

Using the API is simple; there is no need for complex parsing or accounting for drift on the back-end:

using Windows.System.Profile;

IBuffer GetSystemId()
{
// This sample gets the publisher ID which is the same for all apps
// by this publisher on this device.
// Use GetSystemIdForUser if you have the userSystemId capability
// and need the same ID across all apps for this user (not
// really applicable for apps in the Windows Store)
var systemId = SystemIdentification.GetSystemIdForPublisher();

// Make sure this device can generate the IDs
if (systemId.Source != SystemIdentificationSource.None)
{
// The Id property has a buffer with the unique ID
return systemId.Id;
}

// This is a very old PC without the correct hardware. Use
// another mechanism to generate an ID (or perhaps just give
// up due to the small number of people that won't have the ID;
// depends on your business needs).
return GetIdFromAshwidOrSomethingElse();
}

As noted in the question, this ID should only be used for purposes of correlation in a back-end service (eg, for telemetry, advertising, usage metrics, etc.). It should never be used to create anonymous user accounts, to identify or track users, to encrypt user data, etc. This is because different users can share the same device, or the same user can roam across different devices, so the ID doesn't map 1:1 with a user or their data.

This API is available in the Universal API Contract v3, and can be found in the Windows Universal SDK version 10.0.14393.0 (remember that if you're doing multi-version apps and want to light-up usage of this API, you should not do runtime version check; instead you should just query the OS to see if the API is available).

get unique id of the device in windows 10 store app c#

The ASHWID provides a strong binding between the app/packag, which is not affected by OS re-installs and version updates for an app. But sometimes there are also other reasons can affect it, it is difficult to know which causes the change of the ASHWID.

Therefore, if you want to get the unique id to distinguish the device, maybe you could use SystemIdentification.GetSystemIdForPublisher method, the identifier returned by this method is specific to the app publisher on the current device. In other words, all apps by the same publisher will get the same value for this ID (for all users).

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

Can I uniquely identify a device in Windows 10/Windows 8/WinRT

Such an ability has just been added in Windows 8 RTM:

Windows.System.Profile.HardwareIdentification.GetPackageSpecificToken(null)


Related Topics



Leave a reply



Submit