vb.net 第一节 获取本机网络适配器的信息 GetAdaptersInfo

Posted VB.Net

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vb.net 第一节 获取本机网络适配器的信息 GetAdaptersInfo相关的知识,希望对你有一定的参考价值。

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

第一节 获取本机网络适配器的信息 GetAdaptersInfo

Imports System.Runtime.InteropServices

Public Class Form3
    'DWORD GetAdaptersInfo(_Out_   PIP_ADAPTER_INFO pAdapterInfo, _Inout_ PULONG           pOutBufLen);
    '    Private Declare Function GetAdaptersInfo Lib "iphlpapi.dll" ( IpAdapterInfo As Any, pOutBufLen As Long) As Long

    '    Const MAX_ADAPTER_NAME_LENGTH = 260
    '    Const MAX_ADAPTER_ADDRESS_LENGTH = 8
    '    Const MAX_ADAPTER_DESCRIPTION_LENGTH = 132

    '    Const MIB_IF_TYPE_ETHERNET = 6
    '    Const MIB_IF_TYPE_TOKENRING = 9
    '    Const MIB_IF_TYPE_FDDI = 15
    '    Const MIB_IF_TYPE_PPP = 23
    '    Const MIB_IF_TYPE_LOOPBACK = 24
    '    Const MIB_IF_TYPE_SLIP = 28
    '    Const MIB_TCP_STATE_DELETE_TCB = 12


    'typedef struct _IP_ADDR_STRING 
    '  struct _IP_ADDR_STRING* Next;
    '  IP_ADDRESS_STRING IpAddress;
    '  IP_MASK_STRING IpMask;
    '  DWORD Context;
    ' IP_ADDR_STRING, *PIP_ADDR_STRING;


    'Type IP_ADDR_STRING
    '    Next As Long		'指向列表中下一个IP_ADDR_STRING类型。为空,则是最后一个地址
    '    IpAddress As String * 16	'点式十进制字串表示Ip地址
    '    IpMask As String * 16		'子网掩码
    '    Context As Long			'网络IP地址标识,符合AddIPAddress和DeleteIPAddress函数中的网络接口关联参数。
    'End Type


    'typedef struct _IP_ADAPTER_INFO 
    '  struct _IP_ADAPTER_INFO  *Next;
    '  DWORD                   ComboIndex;
    '  Char                    AdapterName[MAX_ADAPTER_NAME_LENGTH + 4];
    '  Char                    Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4];
    '  UINT                    AddressLength;
    '  Byte                    Address[MAX_ADAPTER_ADDRESS_LENGTH];
    '  DWORD                   Index;
    '  UINT                    Type;
    '  UINT                    DhcpEnabled;
    '  PIP_ADDR_STRING         CurrentIpAddress;
    '  IP_ADDR_STRING          IpAddressList;
    '  IP_ADDR_STRING          GatewayList;
    '  IP_ADDR_STRING          DhcpServer;
    '  BOOL                    HaveWins;
    '  IP_ADDR_STRING          PrimaryWinsServer;
    '  IP_ADDR_STRING          SecondaryWinsServer;
    '  time_t                  LeaseObtained;
    '  time_t                  LeaseExpires;
    ' IP_ADAPTER_INFO, *PIP_ADAPTER_INFO;

    'Type IP_ADAPTER_INFO
    '    Next As Long			'在适配器列表中指向下一个适配器
    '    ComboIndex As Long	'保留未用
    '    AdapterName As String * MAX_ADAPTER_NAME_LENGTH			'适配器名
    '    Description As String * MAX_ADAPTER_DESCRIPTION_LENGTH	    '    '对网卡的描述,实际上好象是驱动程序的名字
    '    AddressLength As Long		'适配器物理地址的长度 
    '    Address(MAX_ADAPTER_ADDRESS_LENGTH - 1) As Byte	        '物理地址,每个字节存放一个十六进制的数值
    '    Index As Long			'适配器索引号
    '    Type As Long			'适配器类型,包含以下类型的适配器(见下表):
    '    DhcpEnabled As Long		'适配器是否启用了动态主机配置协议(DHCP)
    '    CurrentIpAddress As Long	'保留(当前使用的IP地址?)
    '    IpAddressList As IP_ADDR_STRING		'绑定到此适配器的IP地址链表
    '    GatewayList As IP_ADDR_STRING		'默认网关地址链表
    '    DhcpServer As IP_ADDR_STRING	 	'DHCP服务器地址,DhcpEnabled=TRUE时有效
    '    HaveWins As Byte		'是否启用WINS(Windows Internet Name Service)
    '    PrimaryWinsServer As IP_ADDR_STRING		'主WINS地址
    '    SecondaryWinsServer As IP_ADDR_STRING		'辅WINS地址
    '    LeaseObtained As Long	'向DHCP服务器租用IP地址的时间,DhcpEnabled=TRUE时有效
    '    LeaseExpires As Long	'向DHCP服务器租用IP地址到期时间,DhcpEnabled=TRUE时有效
    'End Type

    Const MAX_ADAPTER_NAME_LENGTH = 256
    Const MAX_ADAPTER_ADDRESS_LENGTH = 8
    Const MAX_ADAPTER_DESCRIPTION_LENGTH = 128

    Const MIB_IF_TYPE_ETHERNET = 6
    Const MIB_IF_TYPE_TOKENRING = 9
    Const MIB_IF_TYPE_FDDI = 15
    Const MIB_IF_TYPE_PPP = 23
    Const MIB_IF_TYPE_LOOPBACK = 24
    Const MIB_IF_TYPE_SLIP = 28
    Const MIB_TCP_STATE_DELETE_TCB = 12


    <StructLayout(LayoutKind.Sequential)>
    Public Structure IP_ADDR_STRING
        Public [Next] As IntPtr     '指向列表中下一个IP_ADDR_STRING类型。为空,则是最后一个地址
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=16)> Public IpAddress() As Char     '点式十进制字串表示Ip地址
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=16)> Public IpMask() As Char       '子网掩码
        Public Context As Integer      '网络IP地址标识,符合AddIPAddress和DeleteIPAddress函数中的网络接口关联参数。
    End Structure

    <StructLayout(LayoutKind.Sequential)>
    Public Structure IP_ADAPTER_INFO
        Public [Next] As IntPtr           '在适配器列表中指向下一个适配器
        Public ComboIndex As Integer '保留未用
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=MAX_ADAPTER_NAME_LENGTH + 4)> Public AdapterName() As Char          '适配器名
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=MAX_ADAPTER_DESCRIPTION_LENGTH + 4)> Public Description() As Char      '    '对网卡的描述,实际上好象是驱动程序的名字
        Public AddressLength As Integer        '适配器物理地址的长度 
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=MAX_ADAPTER_ADDRESS_LENGTH)> Public Address() As Byte          '物理地址,每个字节存放一个十六进制的数值
        Public Index As Integer            '适配器索引号
        Public Type As Integer         '适配器类型,包含以下类型的适配器(见下表):
        Public DhcpEnabled As Integer        '适配器是否启用了动态主机配置协议(DHCP)
        '在vb6版本中使用的是Long,其实这里是个指针,在vb6下面没错,但是在64位系统下就有问题
        Public CurrentIpAddress As IntPtr  '保留(当前使用的IP地址?)
        Public IpAddressList As IP_ADDR_STRING      '绑定到此适配器的IP地址链表
        Public GatewayList As IP_ADDR_STRING     '默认网关地址链表
        Public DhcpServer As IP_ADDR_STRING     'DHCP服务器地址,DhcpEnabled=TRUE时有效
        Public HaveWins As Byte      '是否启用WINS(Windows Internet Name Service)
        Public PrimaryWinsServer As IP_ADDR_STRING      '主WINS地址
        Public SecondaryWinsServer As IP_ADDR_STRING        '辅WINS地址
        Public LeaseObtained As Integer    '向DHCP服务器租用IP地址的时间,DhcpEnabled=TRUE时有效
        Public LeaseExpires As Integer '向DHCP服务器租用IP地址到期时间,DhcpEnabled=TRUE时有效
    End Structure

    Private Declare Function GetAdaptersInfo Lib "iphlpapi.dll" (ByRef IpAdapterInfo As IntPtr, ByRef pOutBufLen As Integer) As Integer
    Private Declare Function GetAdaptersInfo Lib "iphlpapi.dll" (ByVal IpAdapterInfo() As Byte, ByRef pOutBufLen As Integer) As Integer


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim ptrIpAdapterInfo As IntPtr = IntPtr.Zero
        Dim lenOutBufLen As Integer

        Dim returnValue As Integer
        '第一次调用,返回111:ERROR_BUFFER_OVERFLOW,缓冲区溢出
        'pdwSize参数返回实际需要的字节大小
        returnValue = GetAdaptersInfo(ptrIpAdapterInfo, lenOutBufLen)
        Dim bufferIAI(lenOutBufLen - 1) As Byte
        returnValue = GetAdaptersInfo(bufferIAI, lenOutBufLen)


        Dim iai As New IP_ADAPTER_INFO
        Dim sizeIAI As Integer = Marshal.SizeOf(iai)
        Dim ptrIAI As New IntPtr
        ptrIAI = Marshal.AllocHGlobal(sizeIAI)

        Marshal.Copy(bufferIAI, 0, ptrIAI, sizeIAI)
        iai = Marshal.PtrToStructure(ptrIAI, GetType(IP_ADAPTER_INFO))

        Do While True
            Dim adpName As New String(iai.AdapterName)
            TextBox1.Text &= adpName.Replace(Chr(0), "") & vbCrLf
            Dim description As New String(iai.Description)
            TextBox1.Text &= description.Replace(Chr(0), "") & vbCrLf

            TextBox1.Text &= "适配器编号:" & iai.Index & vbCrLf
            '获得IP地址
            Dim iasIpAddress As IP_ADDR_STRING = iai.IpAddressList
            TextBox1.Text &= "IP地址:" & vbCrLf

            '获得IP地址列表
            Do While True
                Dim ipaddrIpAddress As New String(iasIpAddress.IpAddress)
                Dim ipmaskIpAddress As New String(iasIpAddress.IpMask)
                TextBox1.Text &= "  " & ipaddrIpAddress.Replace(Chr(0), "") & vbCrLf
                TextBox1.Text &= "  " & ipmaskIpAddress.Replace(Chr(0), "") & vbCrLf

                If iasIpAddress.Next = IntPtr.Zero Then
                    Exit Do
                End If
                iasIpAddress = Marshal.PtrToStructure(iasIpAddress.Next, GetType(IP_ADDR_STRING))
            Loop

            '获得网关地址
            Dim iasGateway As IP_ADDR_STRING = iai.GatewayList
            TextBox1.Text &= "网关地址:" & vbCrLf

            '获得网关地址列表
            Do While True
                Dim ipaddrGateway As New String(iasGateway.IpAddress)
                Dim ipmaskGateway As New String(iasGateway.IpMask)
                TextBox1.Text &= "  " & ipaddrGateway.Replace(Chr(0), "") & vbCrLf
                TextBox1.Text &= "  " & ipmaskGateway.Replace(Chr(0), "") & vbCrLf

                If iasGateway.Next = IntPtr.Zero Then
                    Exit Do
                End If
                iasGateway = Marshal.PtrToStructure(iasGateway.Next, GetType(IP_ADDR_STRING))
            Loop


            If iai.Next = IntPtr.Zero Then
                Exit Do
            End If
            iai = Marshal.PtrToStructure(iai.Next, GetType(IP_ADAPTER_INFO))
        Loop
    End Sub

End Class

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。

学习更多vb.net知识,请参看 vb.net 教程 目录

以上是关于vb.net 第一节 获取本机网络适配器的信息 GetAdaptersInfo的主要内容,如果未能解决你的问题,请参考以下文章

vb.net 第十一节 获取当前计算机的IP信息 GetIpStatistics

vb.net 第八节 获取本机网络参数 GetNetworkParams

vb.net 第三节 获取本机TCP 信息列表 GetTcpStatistics

vb.net 第五节 获取本机UDP信息列表 GetUdpStatistics

vb.net 第七节 获取本机网间控制报文协议信息 GetIcmpStatistics

在vb.net中使用Iphlpapi.dll获取网络信息