代码之家  ›  专栏  ›  技术社区  ›  Volker

窗户。系统。轮廓。硬件标识。GetPackageSpecificToken抛出异常

  •  1
  • Volker  · 技术社区  · 6 月前

    我正试图通过.net控制台应用程序获取硬件id(目标framewotk是 net8.0-windows10.0.17763.0 ).

    代码相当琐碎,只是

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

    无论我得到什么

    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
    

    COMException代码表示 Call context cannot be accessed after call completed. .

    [STAThread] 但没有变化。

    文档中的某个地方说这是针对商店应用程序的。有什么方法可以在仅限windows的控制台应用程序中使用它吗?

    1 回复  |  直到 6 月前
        1
  •  2
  •   IInspectable    6 月前

    GetPackageSpecificToken() 返回当前应用程序包的唯一硬件ID。这仅适用于打包应用程序(有时称为“商店应用程序”)。

    要获取未打包应用程序的硬件ID,请使用 SystemIdentification 相反,类。 GetSystemIdForPublisher() 返回a SystemIdentificationInfo 类与a Id 财产。此ID是字节的集合,不打算被解释为字符串。

    以下代码片段 1. 获取ID并将其编码为十六进制字符串:

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

    文件 GetSystemIdForPublisher() 遗漏了一个关键细节:对于没有应用程序清单的未打包应用程序,系统将使用通用发布器,这对所有此类应用程序都是一样的。Raymond Chen在他的博客文章中解释了这一点 How can I get a signature for a Windows system that will remain unchanged even if the user reinstalls Windows? [ archived ] .


    1. 该代码借用了Raymond Chen的博客文章。