Question

I am currently using two methods on my Windows 7 Bootcamp (running on a Macbook Pro) to retrieve a MAC address. I am currently investigating some issues with people running my C#-written application on a virtualized machine, where they were unable to get authenticated from my MAC address authentication mechanism. I don't have a VM build on hand, so I chose the closest alternative - my boot camp.

The first method retrieves a MAC address based on the fact that the physical card is connected to the PCI interface.

public static string returnMAC1()
{
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select MACAddress, PNPDeviceID FROM Win32_NetworkAdapter WHERE MACAddress IS NOT NULL AND PNPDEVICEID IS NOT NULL");
    ManagementObjectCollection mObject = searcher.Get();

    foreach (ManagementObject obj in mObject)
    {
        string pnp = obj["PNPDeviceID"].ToString();
        if (pnp.Contains("PCI\\"))
        {
            string mac = obj["MACAddress"].ToString();
            mac = mac.Replace(":", string.Empty);
            return mac;
        }
    }
    return "Nothing happened...";
}

The second method is a standard method from the MSDN documentation:

public static string returnMAC2()
{
    string mac = string.Empty;
    foreach (System.Net.NetworkInformation.NetworkInterface nic in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
    {
        if (nic.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up)
        {
            macAddresses += nic.GetPhysicalAddress().ToString();
            break;
        }
    }
    return mac;
}

Using both methods, I get two entirely different results:

Here is what gets printed in my two methods:

enter image description here

When I check out the MAC address from the Network and Sharing Center -> Wireless Network Connection, it corresponds to method 2's result.

enter image description here

Does the fact that I get two entirely different results have to do with the fact that method #1 is giving me the result of not the wireless card's MAC, but the actual PCI card's MAC? That seems to be the most simple of an explanation, but possible.

Was it helpful?

Solution

It looks like my hypothesis is correct.

I took a LAN port (not sure why I didn't do this earlier) and plugged it into the MAC. Ran the script again, and this time checked the Local Area Connection's address, and this time it displayed method 1's address.

Lessons learned:

  1. Local Area Connection MAC =/= Wireless Network Connection. The MAC addresses used by the wireless and the LAN adapter are different.

  2. Different C# MAC address retrieval methods retrieve MAC addresses from different hardware connections. In our case, two different methods that are supposed to give you the same results retrieve one MAC from the wireless port, and one from the LAN port.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top