vb.net 第十八节 在本地电脑的地址解析协议(ARP :Address Resolution Protocol)表中创建和删除一个ARP

Posted VB.Net

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vb.net 第十八节 在本地电脑的地址解析协议(ARP :Address Resolution Protocol)表中创建和删除一个ARP相关的知识,希望对你有一定的参考价值。

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

注意:此段代码可能有问题,虽然无错误提示,但是创建后对应arp表的mac都是00-00-00-00-00-00,我在自己电脑上用 arp -s 创建出来的mac也是00-00-00-00-00-00。目前情况不明。

Imports System.Runtime.InteropServices

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

    '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 = 8

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


    'DWORD CreateIpNetEntry(PMIB_IPNETROW pArpEntry);
    'DWORD DeleteIpNetEntry(PMIB_IPNETROW pArpEntry);

    'Private Declare Function CreateIpNetEntry Lib "iphlpapi.dll" (pArpEntry As MIB_IPNETROW) As Long
    'Private Declare Function DeleteIpNetEntry Lib "iphlpapi.dll" (pArpEntry As MIB_IPNETROW) As Long
    'Private Declare Function inet_addr Lib "wsock32.dll" (ByVal cp As String) As Long

    Private Declare Function CreateIpNetEntry Lib "iphlpapi.dll" (ByRef pArpEntry As MIB_IPNETROW) As Integer
    Private Declare Function CreateIpNetEntry Lib "iphlpapi.dll" (ByVal pArpEntry As IntPtr) As Integer

    Private Declare Function DeleteIpNetEntry Lib "iphlpapi.dll" (ByVal pArpEntry As MIB_IPNETROW) As Integer
    Public Declare Function inet_addr Lib "wsock32.dll" (ByVal cp As String) As Integer

    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 btnAdd.Click
        Dim returnValue As Integer

        Dim strMac As String = txtMacAdd.Text.ToUpper
        Dim bteMac() As String = strMac.Split("-")

        Dim MI As New MIB_IPNETROW
        Dim PhysAddr(7) As Byte
        For i As Integer = 0 To 5
            PhysAddr(i) = Convert.ToByte(bteMac(i), 16)
        Next
        PhysAddr(6) = 0
        PhysAddr(7) = 0

        MI.dwIndex = Integer.Parse(txtIndexAdd.Text)
        MI.dwAddr = inet_addr(txtIPAdd.Text)
        MI.bPhysAddr = PhysAddr
        MI.dwPhysAddrLen = 6
        MI.dwType = 4

        returnValue = CreateIpNetEntry(MI)

        If returnValue = 0 Then
            lblInfo.Text = "增加 " & txtIPAdd.Text & " 成功"
            Exit Sub
            Else
            lblInfo.Text = "增加 " & txtIPAdd.Text & " 失败,错误代码:" & returnValue
        End If

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnDel.Click
        'A pointer to a MIB_IPNETROW structure. The information in this structure specifies the entry to delete. 
        'The caller must specify values for at least the dwIndex And dwAddr members of this structure.
        Dim MI As New MIB_IPNETROW

        MI.dwIndex = Integer.Parse(txtIndexDel.Text)
        MI.dwAddr = inet_addr(txtIPDel.Text)

        Dim returnValue As Integer
        returnValue = DeleteIpNetEntry(MI)

        If returnValue = 0 Then
            lblInfo.Text = "删除 " & txtIPDel.Text & " 成功"
        Else
            lblInfo.Text = "删除 " & txtIPDel.Text & " 失败,错误代码:" & returnValue
        End If

    End Sub

    '显示现有arp表
    Private Sub btnList_Click(sender As Object, e As EventArgs) Handles btnList.Click
        lvArp.Items.Clear()

        Dim pIpNetTable As IntPtr = IntPtr.Zero
        Dim pdwSize As Integer

        Dim returnValue As Integer
        returnValue = GetIpNetTable(pIpNetTable, pdwSize, True)

        Dim bufIpNetTable(pdwSize - 1) As Byte
        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))

            Dim lvi As New ListViewItem(MIBIPNETROW.dwIndex)
            lvi.SubItems.Add(IntToIP(MIBIPNETROW.dwAddr))
            lvi.SubItems.Add(BitConverter.ToString(MIBIPNETROW.bPhysAddr, 0, 6))
            Select Case MIBIPNETROW.dwType
                Case 1
                    lvi.SubItems.Add("其他条目")
                Case 2
                    lvi.SubItems.Add("无效条目")
                Case 3
                    lvi.SubItems.Add("动态条目")
                Case 4
                    lvi.SubItems.Add("静态条目")
                Case Else
                    lvi.SubItems.Add("其他")
            End Select
            lvArp.Items.Add(lvi)
        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 第十八节 在本地电脑的地址解析协议(ARP :Address Resolution Protocol)表中创建和删除一个ARP的主要内容,如果未能解决你的问题,请参考以下文章

第十八节:上下文管理协议

第十八节——spring练习之页面上用户列表的展示

第十八节 模型的集成

centos mysql 实战 第十八节课

第十八节20181216

UI第十八节——UITableView