Reliably Retrieve MAC Address(es) with VB


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

When searching for a method to retrieve my own MAC address in VB 6.0, I found only some non-reliable samples that all used the ancient NetBIOS calls. It also seemed to me that all of the authors have just copied from a public available source—the basic code is contained in the MSDN documentation of the Netbios function.

Some of those examples worked more or less, but (as others noted, too) the NetBIOS call does not work reliably. A problem is the adapter enumeration/indexing that may cause several adapter entries existing in the system and the first one often not being the first Ethernet card. Even when retrieving all information blocks and scanning through them, it didn't work reliably on different systems—especially when the network cable is plugged out—sometimes even no adapter can be found, then.

Those problems made that kind of solution unusable. For example, if You use the MAC address of the network card to license your software, your customer won't be happy if he can't use it anymore when the network cable is unplugged!

So, I searched the MSDN for an alternative, and found the documentation of the iphlpapi.dll functions, but found them looking very clumsy to use from within VB 6.0.

When searching through CodeGuru, I found Khalid Shaikh's article, "Three ways to get your MAC address" (2002/04/15), where he described three ways to retrieve a MAC address and discusses the pros/cons of those methods. This article enforced me to focus on the iphlpapi.dll functions, especially GetAdaptersInfo (the MSDN article can be found here).

Also, J. Guo's article A New Method to Get MAC/NIC Statistical Information where he showed how to use GetIfTable (the MSDN article can be found here), and a code snippet from a colleague helped me to get some idea to implement my VB code.

During the implementation, I encountered some contradictions in the MSDN docs and some problems with VB's memory allocation (arrays of user-defined structures are not necessarely laid out contiguously due to internal padding/alignment), but finally got it to work with the tools of the .NET and VB6 debuggers—even with the network cable unplugged—on Windows 2000 and XP.

For testing and my own experience, I implemented both ways, using GetAdaptersInfo and using GetIfTable. The functions deliver different information, they especially differ in that GetAdaptersInfo returns information about physically present adapters and GetIfTable returns information about interfaces present—including the virtual loopback adapter (which has no MAC address, of course).


About the Author

Manfred Haug

Developer of networked measurement and control applications



  • mac address code

    Posted by Abhishek on 12/23/2012 12:22pm

    Thanks for the mac address code. I appreciate your research and hardwork behind this. It is working perfectly fine. May I know, if user can change the mac address then how can I protect my software. Is there any other code/address which is unique and unchangeable in any circumstances. Regards, Abhishek

    • Have u ever try this...?

      Posted by Eka on 01/25/2013 11:00am

      So.., first, I searched this forum to protect my software also. Sadly, MAC Address can be change easily. Thinking deeply, I'd rather go back to my old style, which is save my key file in system folder (whereever u like, mostly in Windows folders) and in windows style unique name, so user will see it as a windows system file... and also make it hidden as a system file...

  • Works in VBA

    Posted by matthew_of_cambridge on 08/18/2009 09:08am

    I imported the VB module into an Access VBA project and it worked first time!

  • Additional .AdapterType

    Posted by matthew_of_cambridge on 08/18/2009 09:06am

    Private Const MIB_TYPE_WIRELESS As Long = 71 AT least this is what my wireless network adapter reported. I would love to know where you can get a complete list of these codes... I tried MSDN.

  • Caution for copy-protection implementers

    Posted by yellowteeth on 01/24/2006 07:11am

    Note for those thinking of using a MAC address as the only key or primary key for a copy-protection mechanism. The MAC address can be easily changed by a user. For 2000/XP systems, try this: 1. Control Panel / System 2. 'Harware tab' 3. 'Device Manager' button 4. Expand' Network Adaptors' 5. Right-click on your network adaptor then choose 'Properties' 6. Click on the 'Advanced' tab 7. Select the 'Network Address' property 8. Define whatever MAC address you want Remember, on a network, MAC addresses only need to be unique within a collision domain.

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date