Detecting Network Adapters

From HashVB
Jump to: navigation, search
 This article is based on Visual Basic 6. Find other Visual Basic 6 articles.

To detect the network adapters, we use WMI as follows:

(NOTE: you need to add a reference to the Microsoft WMI Scripting V1.1 Library in order to use this code. If you don't want to do this, you can change the declarations of all the objects to the type Object and it will still work)

 '-- Objects we need from WMI
 Dim objWMIService As SWbemServices
 Dim colNetAdapters As SWbemObjectSet
 Dim objNetAdapter As SWbemObject
 '-- Get the WMI Service object
 Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
 '-- Fill the object set with the network adapter configs that have IPEnabled = True
 Set colNetAdapters = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 '-- Loop through the collection and add each adapter to the listview
 For Each objNetAdapter In colNetAdapters
     Debug.Print objNetAdapter.Caption, objNetAdapter.IPAddress(0)
 '-- Clean up
 Set colNetAdapters = Nothing
 Set objWMIService = Nothing
 Set objNetAdapter = Nothing

And that's it :)