How to read IP Address of 'actually used' lan nic?

amorosik

Member
Local time
Today, 03:29
Joined
Apr 18, 2020
Messages
550
Normallly, using this code, ALL pc ip addresses are returned
But wanting to know exactly the address of the wired or wireless network card that pc is using, how to do?
I ask this beacuae if the pc has two lan ports on the motherboard, a wifi card, and a virtual lan like those installed by HyperV, the result of the function is almost nevere the adress expected

Code:
Function GetLanIpAddress() As String
    Const STR_COMPUTER As String = "."
    Dim objWMI As Object
    Dim colAdapters As Object
    Dim objAdapter As Object
    Dim sIp As String
    
    ' Avvia WMI
    Set objWMI = GetObject("winmgmts:\\" & STR_COMPUTER & "\root\cimv2")
    ' Seleziona solo adattatori con IP abilitato e non wireless
    Set colAdapters = objWMI.ExecQuery( _
        "SELECT * FROM Win32_NetworkAdapterConfiguration " & _
        "WHERE IPEnabled = TRUE")
    
     For Each objAdapter In colAdapters
        If Not IsNull(objAdapter.IpAddress) Then
            Dim arrIP
            arrIP = objAdapter.IpAddress
            Debug.Print "IP ADDRESS = " & arrIP(i)
            End If
        Next
    
    For Each objAdapter In colAdapters
        If Not IsNull(objAdapter.IpAddress) Then
            'Dim arrIP
            arrIP = objAdapter.IpAddress
            Dim i As Integer
            For i = LBound(arrIP) To UBound(arrIP)
                ' Salta gli IPv6
                If InStr(arrIP(i), ":") = 0 Then
                    ' Se è un IP privato (LAN)
                    If Left(arrIP(i), 4) = "192." Or _
                       Left(arrIP(i), 4) = "10." Or _
                       Left(arrIP(i), 4) = "172." Then
                        GetLanIpAddress = arrIP(i)
                        Exit Function
                    End If
                End If
            Next i
        End If
    Next
    
    GetLanIpAddress = "" ' Nessun IP trovato
End Function
 
I see that now.

I used cmd window to output IPConfig info. I have 5 configurations and 4 are disconnected so I only get IP for my WiFi as only enabled objects are retrieved by the query into IPConfgSet. If I remove the filter criteria, I get 18 items. I also looked at DeviceManager and it lists 11 items under Network adapters.

I modified above code to get the IPConfig.Caption property (see complete list at https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-networkadapterconfiguration). The output is [00000013] Intel(R) Wireless-N 7260. That is definitely model of the adapter I installed last month. Perhaps you can use that property to identify the connection you want.

One curiosity this exercise revealed is the VBA (without filter criteria) lists a Qualcomm adapter that was replaced by the Intel adapter. The Qualcomm device is not listed in DeviceManager nor IPConfig cmd.
 
Last edited:
Can you describe the main purpose for determining the IP address? Perhaps if the goal was known, someone can offer an appropriate approach. I was thinking of maybe using an HTTP request to get this info.
 
When I had to do this (and at the time, didn't know about WMI), what I used was to shell a command to the CMD prompt:

Code:
ipconfig  > .\ipcfg.txt

Then I opened file ".\ipcfg.txt" (for reading) and did successive LINE INPUT statements looking for keywords. Most of the things were easy to recognize and most of the potential connections were "Not connected" and thus easy to disregard. If you do this once by hand, you can get all the keywords that you need in order to pick apart the inputs. This trick also works on MAC addresses using the ARP command. If you do this, remember to CLOSE and KILL the file when done with it.

Note that if you have a typical modern network I/F, the IPv4 address is your internal network's DHCP-assigned address, not the network IP, because the network IP will probably be for IPv6, and you might see several such addresses for different purposes.
 

Users who are viewing this thread

Back
Top Bottom