1

I'm trying to get a hardware id with a .net console app (target framewotk is net8.0-windows10.0.17763.0).

The code is rather trivial, just

var token = Windows.System.Profile.HardwareIdentification.GetPackageSpecificToken(null);

However I get

Unhandled exception. System.Runtime.InteropServices.COMException (0x80010117)
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|38_0(Int32 hr)
   at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)
   at ABI.Windows.System.Profile.IHardwareIdentificationStaticsMethods.GetPackageSpecificToken(IObjectReference _obj, IBuffer nonce)
   at Windows.System.Profile.HardwareIdentification.GetPackageSpecificToken(IBuffer nonce)
   at TPMGames.Program.Main(String[] args) in C:\Projects\TPMGames\TPMGames\Program.cs:line 7

The COMException code means Call context cannot be accessed after call completed..

I've tried [STAThread] but no change.

Somewhere in the doc it says that this is for store apps. Is there any way I can make use of it in a windows-only console app?

Or does it need a specific project type?

1 Answer 1

2

GetPackageSpecificToken() returns a unique hardware ID for the current application package. This works for packaged apps (sometimes called "store apps") only.

To get a hardware ID for an unpackaged application, use the SystemIdentification class instead. GetSystemIdForPublisher() returns a SystemIdentificationInfo class with an Id property. This ID is a collection of bytes that is not intended to be interpreted as a string.

The following snippet1 gets the ID and encodes it as a hex string:

var buffer = Windows.System.Profile.SystemIdentification.GetSystemIdForPublisher();
var id = buffer.Id;
var asHex = Windows.Security.Cryptography.CryptographicBuffer.EncodeToHexString(id);

The documentation for GetSystemIdForPublisher() leaves out a crucial detail: For unpackaged applications without an application manifest, the system will use a generic publisher, which is the same for all such applications. Raymond Chen explains this in his blog post How can I get a signature for a Windows system that will remain unchanged even if the user reinstalls Windows?[archived].


1 The code is borrowed from Raymond Chen's blog post linked above.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.