vb.net 第十节 获取本机已探测的IP – 物理地址映射表 GetIpNetTable

Posted VB.Net

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vb.net 第十节 获取本机已探测的IP – 物理地址映射表 GetIpNetTable相关的知识,希望对你有一定的参考价值。

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

Imports System.Runtime.InteropServices

Public Class Form11
    'typedef struct _MIB_IPNETTABLE 
    '  DWORD dwNumEntries; 
    '  MIB_IPNETROW table[ANY_SIZE]; 
    ' MIB_IPNETTABLE, *PMIB_IPNETTABLE;

    'typedef struct _MIB_IPNETROW 
    '  DWORD dwIndex; 
    '  DWORD dwPhysAddrLen; 
    '  Byte bPhysAddr[MAXLEN_PHYSADDR]; 
    '  DWORD dwAddr; 
    '  DWORD dwType; 
    ' MIB_IPNETROW, *PMIB_IPNETROW;


    'Type MIB_IPNETTABLE
    ' DwNumEntries As Long				'当前包含MIB_IPNETROW类型的总数
    ' MIB_Table() As MIB_IPNETROW	'指向一个包含MIB_IPNETROW类型的指针
    'End Type

    'Type MIB_IPNETROW
    '    dwIndex As Long 			'指定适配器的索引
    '    dwPhysAddrLen As Long  	'bPhysAddrs字段内包含的物理接口的长度(字节),通常为6
    '    bPhysAddr(MAXLEN_PHYSADDR) As Byte	'字节数组,包含适配器的物理地址
    '    dwAddr As Long  			'IP地址
    '    dwType As Long  			'ARP接口的类型,以下值之一:
    '        常量名称	值	说明
    '        MIB_IPNET_TYPE_OTHER	1	其他条目
    '        MIB_IPNET_TYPE_INVALID	2	无效条目
    '        MIB_IPNET_TYPE_DYNAMIC	3	动态条目
    '        MIB_IPNET_TYPE_STATIC	4	静态条目
    'End Type
    Const MAXLEN_PHYSADDR = 7

    <StructLayout(LayoutKind.Sequential)>
    Public Structure MIB_IPNETTABLE
        Public DwNumEntries As Long             '当前包含MIB_IPNETROW类型的总数
        Public MIB_Table() As MIB_IPNETROW  '指向一个包含MIB_IPNETROW类型的指针
    End Structure

    <StructLayout(LayoutKind.Sequential)>
    Public Structure MIB_IPNETROW
        Public dwIndex As Integer          '指定适配器的索引
        Public dwPhysAddrLen As Integer    'bPhysAddrs字段内包含的物理接口的长度(字节),通常为6
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=MAXLEN_PHYSADDR)> Public bPhysAddr() As Byte   '字节数组,包含适配器的物理地址
        Public dwAddr As Integer           'IP地址
        Public dwType As Integer           'ARP接口的类型,以下值之一:
    End Structure

    'DWORD GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize,   BOOL bOrder);

    'Public Declare Function GetIpNetTable Lib "iphlpapi.dll" (pIpNetTable As MIB_IPNETTABLE, pdwSize As Long, bOrder As Long) As Long
    Public Declare Function GetIpNetTable Lib "iphlpapi.dll" (ByRef pIpNetTable As IntPtr, ByRef pdwSize As Integer, ByVal bOrder As Integer) As Integer
    Public Declare Function GetIpNetTable Lib "iphlpapi.dll" (ByVal pIpNetTable() As Byte, ByRef pdwSize As Integer, ByVal bOrder As Integer) As Integer


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

        Dim returnValue As Integer
        '第一次调用,返回122:ERROR_INSUFFICIENT_BUFFER,缓冲区大小不足
        'pdwSize参数返回实际需要的字节大小
        returnValue = GetIpNetTable(pIpNetTable, pdwSize, True)

        Dim bufIpNetTable(pdwSize - 1) As Byte
        '分配内存
        'ptrMibf = Marshal.AllocHGlobal(lenIft)

        '第二次调用获得数据
        returnValue = GetIpNetTable(bufIpNetTable, pdwSize, True)

        Dim bytesNumEntries(3) As Byte
        Array.Copy(bufIpNetTable, 0, bytesNumEntries, 0, 4)
        Dim size As Integer = Marshal.SizeOf(bytesNumEntries(0)) * bytesNumEntries.Length
        Dim ptrNumEntries As IntPtr = Marshal.AllocHGlobal(size)
        Marshal.Copy(bytesNumEntries, 0, ptrNumEntries, 4)
        Dim NumEntries As Integer
        NumEntries = Marshal.ReadInt32(ptrNumEntries)

        Dim MIBIPNETROW As New MIB_IPNETROW
        Dim sizeMIBIPNETROW As Integer = Marshal.SizeOf(MIBIPNETROW)

        For i As Integer = 0 To NumEntries - 1
            Dim ptrMIBIPNETROW As New IntPtr
            ptrMIBIPNETROW = Marshal.AllocHGlobal(sizeMIBIPNETROW)
            Marshal.Copy(bufIpNetTable, 4 + sizeMIBIPNETROW * i, ptrMIBIPNETROW, sizeMIBIPNETROW)
            MIBIPNETROW = Marshal.PtrToStructure(ptrMIBIPNETROW, GetType(MIB_IPNETROW))

            TextBox1.Text &= "No." & i & vbCrLf
            TextBox1.Text &= IntToIP(MIBIPNETROW.dwAddr) & vbCrLf
            TextBox1.Text &= BitConverter.ToString(MIBIPNETROW.bPhysAddr, 0, 6) & vbCrLf
        Next
    End Sub

    'Integer型IP转字符串
    Private Function IntToIP(ByVal intAddr As Integer) As String
        Dim bteIp() As Byte
        bteIp = BitConverter.GetBytes(intAddr)
        Dim ip As String = ""
        For i As Integer = 0 To bteIp.Length - 1
            ip &= bteIp(i).ToString & "."
        Next

        Return ip.Substring(0, ip.Length - 1)
    End Function
End Class

 

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

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

以上是关于vb.net 第十节 获取本机已探测的IP – 物理地址映射表 GetIpNetTable的主要内容,如果未能解决你的问题,请参考以下文章

vb.net 第十二节 获得本机系统网络接口适配器的列表 GetInterfaceInfo

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

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

vb.net 第十四节 返回本机网络接口数量 GetNumberOfInterfaces

0530 第十节课

vb.net 第十五节 设置本机默认的生存时间(time-to-live:TTL)值 SetIpTTL