Reliably Retrieve MAC Address(es) with VB
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).
Screenshot

Comments
mac address code
Posted by Abhishek on 12/23/2012 04:22amThanks 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
-
ReplyHave u ever try this...?
Posted by Eka on 01/25/2013 03:00amSo.., 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...
ReplyWorks in VBA
Posted by matthew_of_cambridge on 08/18/2009 02:08amI imported the VB module into an Access VBA project and it worked first time!
ReplyAdditional .AdapterType
Posted by matthew_of_cambridge on 08/18/2009 02:06amPrivate 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.
ReplyCaution for copy-protection implementers
Posted by yellowteeth on 01/23/2006 11:11pmNote 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.
Reply