views:

1272

answers:

4

I'm playing around with retrieving the MAC address from the NIC - there are a variety of ways to get it, this article covers the most common:

http://www.codeguru.com/Cpp/I-N/network/networkinformation/article.php/c5451

I'm currently using the GetAdaptersInfo method, which seems the most bulletproof, but if the MAC address has been set via the registry:

http://www.mydigitallife.info/2008/06/30/how-to-change-or-spoof-mac-address-in-windows-xp-vista-server-20032008-mac-os-x-unix-and-linux/

Then it reports the MAC address that it has been changed to. The only way I've found to actually get the true MAC is to remove the registry entry, restart the NIC, get the MAC via GetAdaptersInfo, then replace the registry entry, and restart the NIC. While it gets the job done, it's hardly transparent to the user.

Is there any other methods that anyone is familiar with, that will return the hardware MAC regardless of what the registry is set to? Ideally I'd like a solution that works on XP on up.

Thanks in advance!

A: 

You can check how this opensource tool does it: http://devices.natetrue.com/macshift/

ktulur
This utility just changes the MAC address, in the same manner as the second link I provided does - when it restores the original, it just removes the registry key, so it's effectively doing the same thing as what I described. The problem with that approach is that the registry change doesn't take effect until either a reboot or restart of the interface.
Mark
+1  A: 

My guess is that in the linked CodeGuru article, the Miniport solution is likely to overcome the problem you describe, albeit painful to implement. The reason I think this is that I have used the GetAdaptersInfo solution myself in the past, and noticed that the MAC address will change without reboot when an adapter is added, e.g. a Bluetooth adapter providing PAN services.

Perhaps rather than rebooting after changing the registry setting, you could try stopping and restarting the relevent network services. You could easily check this manually prior to looking for a programmatic solution.

(n.b. the above is all guess work. If you try it and it works, perhaps add a post for those trying to do the same in future).

Shane MacLaughlin
So it looks like the Miniport solution works, but has a high likelyhood of not being supported going forward. I found this link from the NDIS gurus over at PCUSA:http://www.pcausa.com/Utilities/macaddr2.htmIf you modify their example to query OID_802_3_PERMANENT_ADDRESS instead of OID_802_3_CURRENT_ADDRESS, it returns the hardware address.
Mark
A: 

Parse the output of ipconfig /all

codeape
ipconfig /all (at least on Vista) only displayed the MAC address from the registry.
Mark
+1  A: 

Here is how to do this in Delphi. You could convert this to C++ rather easily.

Or, download and use the Intel Network Detection Library. You can get the MAC address, and a whole lot more.

Mick