使用 C++ 代码在 Windows Phone 8.1 中获取唯一 DeviceId

getting the Unique DeviceId in Windows Phone 8.1 using C++ code

我正在尝试使用 C++ 代码获取 windows phone 8.1 的唯一 44 位设备 ID。为此,我遵循了以下 link。

http://abundantcode.com/alternate-way-of-getting-the-unique-deviceid-in-windows-phone-8/#comment-85511

只有一行的短代码,

var UniqueID = Windows.Phone.System.Analytics.HostInformation.PublisherHostId;

但是我遇到了很多错误。

error C3083: 'Analytics': the symbol to the left of a '::' must be a type , 
error C3083: 'HostInformation': the symbol to the left of a '::'must be a `type  ,`
error C2039: 'PublisherHostId' : is not a member of'Windows::Phone::System'  ,  
error C2065: 'PublisherHostId' : undeclared identifier

我也定义了ID_CAP_IDENTIY_DEVICE属性,就是这里提到的

<Capability Name="ID_CAP_IDENTITY_DEVICE"/>

虽然我很迷茫,到底是对还是错。

对于Windows Phone 8.0,你可以使用PublisherHostId,但是对于Windows Phone 8.1原生代码项目你需要使用HardwareIdentification::GetPackageSpecificToken() 又名 ASHWID

这样您就可以更轻松地与 Windows 8.1 共享代码。此 ID 对于给定设备上的给定应用程序是不变的,但会因应用程序和设备而异。

这是一个适用于 Windows Phone 8.1 的示例:

using namespace Windows::System::Profile;
using namespace Windows::Security::Cryptography;
using namespace Platform;

auto token = HardwareIdentification::GetPackageSpecificToken(nullptr);
Array<byte>^ buffer = ref new Array<byte>(token->Id->Length);

// Nothing to do with crypto; just a helpful class 
// to convert IBuffer^ -> Array^
CryptographicBuffer::CopyToByteArray(token->Id, &buffer);

// Output the first four bytes of the key
char key[9];
sprintf_s(key, sizeof(key) / sizeof(key[0]), "%02X%02X%02X%02X", 
  buffer[0], buffer[1], buffer[2], buffer[3]);
OutputDebugStringA(key);