amorosik
Member
- Local time
- Today, 18:28
- Joined
- Apr 18, 2020
- Messages
- 551
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
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