Private Const NO_ERROR = 0 Private Declare Function inet_addr Lib "wsock32.dll" (ByVal s As String) As Long Private Declare Function SendARP Lib "iphlpapi.dll" (ByVal DestIP As Long, ByVal SrcIP As Long, pMacAddr As Long, PhyAddrLen As Long) As Long Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dst As Any, src As Any, ByVal bcount As Long)
Private Function GetRemoteMACAddress(ByVal sRemoteIP As String, strMACadd As String) As Boolean Dim dwRemoteIP As Long Dim pMacAddr As Long Dim bpMacAddr() As Byte Dim PhyAddrLen As Long Dim cnt As Long Dim tmp As String
dwRemoteIP = inet_addr(sRemoteIP)
If dwRemoteIP <> 0 Then 'set PhyAddrLen to 6 PhyAddrLen = 6 'retrieve the remote MAC address If SendARP(dwRemoteIP, 0&, pMacAddr, PhyAddrLen) = NO_ERROR Then If pMacAddr <> 0 And PhyAddrLen <> 0 Then 'returned value is a long pointer 'to the mac address, so copy data 'to a byte array ReDim bpMacAddr(0 To PhyAddrLen - 1) CopyMemory bpMacAddr(0), pMacAddr, ByVal PhyAddrLen 'loop through array to build string For cnt = 0 To PhyAddrLen - 1 If bpMacAddr(cnt) = 0 Then tmp = tmp & "00-" Else tmp = tmp & Hex$(bpMacAddr(cnt)) & "-" End If Next 'remove the trailing dash 'added above and return True If Len(tmp) > 0 Then strMACadd = Left$(tmp, Len(tmp) - 1) GetRemoteMACAddress = True End If Exit Function Else GetRemoteMACAddress = False End If Else GetRemoteMACAddress = False End If 'SendARP Else GetRemoteMACAddress = False End If 'dwRemoteIP
End Function在窗体上加一个Command,一个List :Private Sub Command1_Click() Dim strMACadd As String Dim strIP As String Dim i As Integer
For i = 0 To 255 DoEvents strIP = "192.168.1." & i strIP = CStr(strIP) If GetRemoteMACAddress(strIP, strMACadd) Then List1.AddItem strIP & " / " & strMACadd End If Next i MsgBox "扫描完毕!", vbOKOnly + vbExclamation End Sub
Private Declare Function inet_addr Lib "wsock32.dll" (ByVal s As String) As Long
Private Declare Function SendARP Lib "iphlpapi.dll" (ByVal DestIP As Long, ByVal SrcIP As Long, pMacAddr As Long, PhyAddrLen As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dst As Any, src As Any, ByVal bcount As Long)
Private Function GetRemoteMACAddress(ByVal sRemoteIP As String, strMACadd As String) As Boolean
Dim dwRemoteIP As Long
Dim pMacAddr As Long
Dim bpMacAddr() As Byte
Dim PhyAddrLen As Long
Dim cnt As Long
Dim tmp As String
dwRemoteIP = inet_addr(sRemoteIP)
If dwRemoteIP <> 0 Then
'set PhyAddrLen to 6
PhyAddrLen = 6
'retrieve the remote MAC address
If SendARP(dwRemoteIP, 0&, pMacAddr, PhyAddrLen) = NO_ERROR Then
If pMacAddr <> 0 And PhyAddrLen <> 0 Then
'returned value is a long pointer
'to the mac address, so copy data
'to a byte array
ReDim bpMacAddr(0 To PhyAddrLen - 1)
CopyMemory bpMacAddr(0), pMacAddr, ByVal PhyAddrLen
'loop through array to build string
For cnt = 0 To PhyAddrLen - 1
If bpMacAddr(cnt) = 0 Then
tmp = tmp & "00-"
Else
tmp = tmp & Hex$(bpMacAddr(cnt)) & "-"
End If
Next
'remove the trailing dash
'added above and return True
If Len(tmp) > 0 Then
strMACadd = Left$(tmp, Len(tmp) - 1)
GetRemoteMACAddress = True
End If
Exit Function
Else
GetRemoteMACAddress = False
End If
Else
GetRemoteMACAddress = False
End If 'SendARP
Else
GetRemoteMACAddress = False
End If 'dwRemoteIP
End Function在窗体上加一个Command,一个List :Private Sub Command1_Click()
Dim strMACadd As String
Dim strIP As String
Dim i As Integer
For i = 0 To 255
DoEvents
strIP = "192.168.1." & i
strIP = CStr(strIP)
If GetRemoteMACAddress(strIP, strMACadd) Then
List1.AddItem strIP & " / " & strMACadd
End If
Next i
MsgBox "扫描完毕!", vbOKOnly + vbExclamation
End Sub