close
以下為取得IP位址MAC的主內容 使用了 sendarp的 API
已經將 sendarp的 API 包裝成類別來引用了
可以取得內部網路內的IP MAC
//----------------------------------
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim jk_mac As New Class_mac
Dim mac_mumber As String = "mac_number"
Dim check As Boolean
If check = False Then
'check = jk_mac.GetRemoteMACAddress("192.168.1.206", mac_mumber) 'test OK
check = jk_mac.GetRemoteMACAddress("140.121.99.120", mac_mumber) 'tset not OK
End If
If check = True Then
MsgBox(mac_mumber)
End If
End Sub
End Class
//----------------------------------
以下為包裝好的要使用的類別
Imports System.Runtime.InteropServices
Public Class Class_mac
Private Const No_ERROR = 0
_
Private Shared Function inet_addr(ByVal s As String) As Integer
End Function
_
Private Shared Function SendARP(ByVal DestIp As Integer, _
ByVal ScrIP As Integer, _
ByRef pMacAddr As Long, _
ByRef PhyAddrLen As Long) As Integer
End Function
_
Private Shared Sub CopyMemory(ByVal dst As Byte(), ByRef src As Long, ByVal bcount As Integer)
End Sub
Public Function GetRemoteMACAddress(ByVal sRemoteIP As String, ByRef sRemoteMacAddress As String) As Boolean
Dim dwRemoteIp As Integer
Dim pMacAddr As Long = 0
Dim bpMacAddr() As Byte
Dim PhyAddrLen As Long = 0
Dim cnt As Long
Dim tmp As String = ""
dwRemoteIp = inet_addr(sRemoteIP)
If dwRemoteIp <> 0 Then
PhyAddrLen = 6
If SendARP(dwRemoteIp, 0&, pMacAddr, PhyAddrLen) = No_ERROR Then
If pMacAddr <> 0 And PhyAddrLen <> 0 Then
ReDim bpMacAddr(0 To PhyAddrLen - 1)
CopyMemory(bpMacAddr, pMacAddr, PhyAddrLen)
For cnt = 0 To PhyAddrLen - 1
tmp &= Hex(bpMacAddr(cnt) + 256).Substring(1, 2) & "-"
Next
If Len(tmp) > 0 Then
sRemoteMacAddress = tmp.Substring(0, tmp.Length - 1)
GetRemoteMACAddress = True
End If
Exit Function
Else
GetRemoteMACAddress = False
End If
Else
GetRemoteMACAddress = False
End If
Else
GetRemoteMACAddress = False
End If
End Function
End Class
已經將 sendarp的 API 包裝成類別來引用了
可以取得內部網路內的IP MAC
//----------------------------------
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim jk_mac As New Class_mac
Dim mac_mumber As String = "mac_number"
Dim check As Boolean
If check = False Then
'check = jk_mac.GetRemoteMACAddress("192.168.1.206", mac_mumber) 'test OK
check = jk_mac.GetRemoteMACAddress("140.121.99.120", mac_mumber) 'tset not OK
End If
If check = True Then
MsgBox(mac_mumber)
End If
End Sub
End Class
//----------------------------------
以下為包裝好的要使用的類別
Imports System.Runtime.InteropServices
Public Class Class_mac
Private Const No_ERROR = 0
_
Private Shared Function inet_addr(ByVal s As String) As Integer
End Function
_
Private Shared Function SendARP(ByVal DestIp As Integer, _
ByVal ScrIP As Integer, _
ByRef pMacAddr As Long, _
ByRef PhyAddrLen As Long) As Integer
End Function
_
Private Shared Sub CopyMemory(ByVal dst As Byte(), ByRef src As Long, ByVal bcount As Integer)
End Sub
Public Function GetRemoteMACAddress(ByVal sRemoteIP As String, ByRef sRemoteMacAddress As String) As Boolean
Dim dwRemoteIp As Integer
Dim pMacAddr As Long = 0
Dim bpMacAddr() As Byte
Dim PhyAddrLen As Long = 0
Dim cnt As Long
Dim tmp As String = ""
dwRemoteIp = inet_addr(sRemoteIP)
If dwRemoteIp <> 0 Then
PhyAddrLen = 6
If SendARP(dwRemoteIp, 0&, pMacAddr, PhyAddrLen) = No_ERROR Then
If pMacAddr <> 0 And PhyAddrLen <> 0 Then
ReDim bpMacAddr(0 To PhyAddrLen - 1)
CopyMemory(bpMacAddr, pMacAddr, PhyAddrLen)
For cnt = 0 To PhyAddrLen - 1
tmp &= Hex(bpMacAddr(cnt) + 256).Substring(1, 2) & "-"
Next
If Len(tmp) > 0 Then
sRemoteMacAddress = tmp.Substring(0, tmp.Length - 1)
GetRemoteMACAddress = True
End If
Exit Function
Else
GetRemoteMACAddress = False
End If
Else
GetRemoteMACAddress = False
End If
Else
GetRemoteMACAddress = False
End If
End Function
End Class
全站熱搜
留言列表