2

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.

theGreenCabbage
  • 5,197
  • 19
  • 79
  • 169

1 Answers1

3

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.

theGreenCabbage
  • 5,197
  • 19
  • 79
  • 169
  • 2
    I am glad you have come to your own conclusion. You are correct, each physical device has a different MAC / physical address. Although your `returnMAC2` and `returnMAC1` methods do different things, this is why they are returning different results. Most likely, the `returnMAC2` method is retrieving the devices in order of the device ready state being set, when the machine starts up. The `returnMAC1` method is probably just grabbing them in order of the device ID or some physical arrangement. – Michael J. Gray Feb 11 '14 at 22:34
  • Thanks Michael. Would you see any difference in running the above scripts on a virtual machine? I'm currently going to set up Visual Studio on a Windows 8.1 trial build tomorrow to test this. – theGreenCabbage Feb 11 '14 at 22:46
  • Yeah, virtual machines get tricky. If you're running it on a VM, it might be connected to various virtual hubs, bridges, switches, and other weird stuff. That may skew what is considered your primary interface by the OS. I wrote a class that basically determines the most likely interface based on the data that has been transmitted, received, the interface type, making sure it's not a loopback or tunnel interface, and also by checking that it's in an up state. You could write something like that using `NetworkInterface` as you've been doing, if that's your goal. – Michael J. Gray Feb 11 '14 at 22:57
  • Hi Michael. Do you mind sharing said class? Specifically, would it be possible to retrieve the MAC address of the host VM while inside the guest VM through C# code? – theGreenCabbage Feb 14 '14 at 15:15
  • You can't retrieve the host VM's MAC address reliably, since they're on separate virtual interfaces. My class just picks the most likely to be primary one on a given system by doing what I said above. – Michael J. Gray Feb 15 '14 at 17:33