-
Mar 7th, 2016, 08:44 PM
#1
Thread Starter
Frenzied Member
I just found this really cool code sample for getting the MAC address.
I have been looking for something like this for a while now, and somebody on stackoverflow.com had this piece of code that gets the MAC address of the default network card, and also the current IP address assigned to that network card, and then displays these 2 pieces of info in message boxes. A slight modification of this code could easily turn it into a function that returns the MAC address as the function's return value, instead of popping up message boxes. This would be very useful for designing copyprotection that is locked to hardware, by using an activation key that is tied to the computer's main network card's MAC address.
Note that even though I'm posting this code here, I'm not the one who figured it out (I don't even know exactly how it works). That credit goes to Jerome Teisseire on stackoverflow.com. I'm just posting it here for the sake of archiving it (so it will be present on more than just one website) and also helping to redistribute it to others who might come to vbforums.com looking for how to do this. More places this code is on the net, the more likely somebody who's looking to figure out how to do it will be able to find it. And the nice thing is it's only a few lines of code, not some huge thing with dozens of API calls.
Code:
Dim myWMI As Object, myObj As Object, Itm
Set myWMI = GetObject("winmgmts:\\.\root\cimv2")
Set myObj = myWMI.ExecQuery("SELECT * FROM " & _
"Win32_NetworkAdapterConfiguration " & _
"WHERE IPEnabled = True")
For Each Itm In myObj
MsgBox (Itm.IPAddress(0))
MsgBox (Itm.MACAddress)
Exit For
Next
-
Mar 8th, 2016, 02:26 AM
#2
Re: I just found this really cool code sample for getting the MAC address.
It's the Windows Management Instrumentation, and provides extremely detailed information about the system, or other computers on the network (the . refers to the local machine). If you want to explore it more, \Windows\System32\wbem\wbemtest.exe (WMI is MS's WBEM implementation) provides a means to enumerate available info. Connect to \root\cimv2, don't worry about filling out any other info if you're just getting data available to your logon on the local machine. You can then use enumerate classes (leave blank to see all) and then enter a class name in enum instances to see all the properties for that category.
-
Mar 8th, 2016, 01:15 PM
#3
Thread Starter
Frenzied Member
Re: I just found this really cool code sample for getting the MAC address.
Originally Posted by fafalone
It's the Windows Management Instrumentation, and provides extremely detailed information about the system, or other computers on the network (the . refers to the local machine). If you want to explore it more, \Windows\System32\wbem\wbemtest.exe (WMI is MS's WBEM implementation) provides a means to enumerate available info. Connect to \root\cimv2, don't worry about filling out any other info if you're just getting data available to your logon on the local machine. You can then use enumerate classes (leave blank to see all) and then enter a class name in enum instances to see all the properties for that category.
The one thing I noticed about this is that it should have returned at least 2 network devices. I have one Ethernet cable port on my PC, and one wireless network device. Only one is currently in use, but 2 network devices means there should be 2 MAC addresses. However only one device gets returned, despite the use of the "For Each itm In myObj" line of code to go through every returned device. Why is it only finding one physical network card in my PC? I have 2 (one for wireless, and one for wired). And every other piece of software that allows me to view my devices actually shows I have 4 (2 physical, and 2 virtual, the 2 virtual ones being called Wireless 2 and Wireless 3).
-
Mar 8th, 2016, 01:25 PM
#4
Re: I just found this really cool code sample for getting the MAC address.
WMI is a heavyweight service with an API designed only for use in admin scripts. You don't want to use it in applications because it can be disabled or even not installed at all.
There are API calls for getting this information if you need it and they're far more reliable.
-
Mar 9th, 2016, 12:54 AM
#5
Re: I just found this really cool code sample for getting the MAC address.
Originally Posted by Ben321
The one thing I noticed about this is that it should have returned at least 2 network devices. I have one Ethernet cable port on my PC, and one wireless network device. Only one is currently in use, but 2 network devices means there should be 2 MAC addresses. However only one device gets returned, despite the use of the "For Each itm In myObj" line of code to go through every returned device. Why is it only finding one physical network card in my PC? I have 2 (one for wireless, and one for wired). And every other piece of software that allows me to view my devices actually shows I have 4 (2 physical, and 2 virtual, the 2 virtual ones being called Wireless 2 and Wireless 3).
Perhaps the IPEnabled=True restriction?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|