Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the mac address in WinRT (Windows 8) programmatically?

I am looking for an API in WinRT to access the mac address.

like image 976
lohith Avatar asked Feb 18 '23 20:02

lohith


1 Answers

You can't retrieve the MAC Address per say, but you do can retrieve hardware specific information to identify a machine if that's what you're trying to do.

Here's a complete msdn article discussing the subject: Guidance on using the App Specific Hardware ID (ASHWID) to implement per-device app logic (Windows)

Be careful to use just the information you need and not the complete id, as it might change based on information that are useless to you (such as the Dock Station bytes for instance).

Here's a code sample of a computed device id based on a few bytes (CPU id, size of memory, serial number of the disk device and bios):

string deviceSerial = string.Empty;
// http://msdn.microsoft.com/en-us/library/windows/apps/jj553431
Windows.System.Profile.HardwareToken hardwareToken = Windows.System.Profile.HardwareIdentification.GetPackageSpecificToken(null);
using (DataReader dataReader = DataReader.FromBuffer(hardwareToken.Id))
{
    int offset = 0;
    while (offset < hardwareToken.Id.Length)
    {
        byte[] hardwareEntry = new byte[4];
        dataReader.ReadBytes(hardwareEntry);

        // CPU ID of the processor || Size of the memory || Serial number of the disk device || BIOS
        if ((hardwareEntry[0] == 1 || hardwareEntry[0] == 2 || hardwareEntry[0] == 3 || hardwareEntry[0] == 9) && hardwareEntry[1] == 0)
        {
            if (!string.IsNullOrEmpty(deviceSerial))
            {
                deviceSerial += "|";
            }
            deviceSerial += string.Format("{0}.{1}", hardwareEntry[2], hardwareEntry[3]);
        }
        offset += 4;
    }
}

Debug.WriteLine("deviceSerial=" + deviceSerial);
like image 179
canderso Avatar answered May 01 '23 09:05

canderso