之前要做个东西,写的一段,用的是 Win32_NetworkAdapterConfiguration,网上的基本都是错误的,很少有这方面的资料,就自己写了一段,很简单的东西,希望对其他人有帮助。

Win32_NetworkAdapterConfiguration 更多参数及用法,请查阅:http://msdn.microsoft.com/en-us/library/aa394217.aspx

Public Function GetGateWayIP() As String
    '获取本机网卡的有效IP、MAC、网卡、网关等信息
    On Error GoTo over
    Dim objWMI As Object, colIP As Object, IP As Object, I As Long
    Set objWMI = GetObject("winmgmts://./root/cimv2")
    Set colIP = objWMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") '只获取有效IP
    'Win32_NetworkAdapterConfiguration 更多参数及用法,查阅:http://msdn.microsoft.com/en-us/library/aa394217.aspx
    For Each IP In colIP
        If Not IsNull(IP.IPAddress) Then
            For I = LBound(IP.IPAddress) To UBound(IP.IPAddress)
                'MsgBox "网关:" & IP.DefaultIPGateway(I) & vbCrLf & "地址:" & IP.IPAddress(I) & vbCrLf & "网卡:" & IP.Description(I) & vbCrLf & "Mac:" & IP.Macaddress(I)
                GetGateWayIP = IP.DefaultIPGateway(I)
                Exit For                                                        '只取第一个
            Next
        End If
    Next
    Exit Function
over:
    GetGateWayIP = ""
End Function