Option Explicit '===================以下定义用于获得本机IP================== Private Const WSADescription_Len = 256 Private Const WSASYS_Status_Len = 128 Private Type WSA_DATA wVersion As Integer wHighVersion As Integer strDescription(WSADescription_Len + 1) As Byte strSystemStatus(WSASYS_Status_Len + 1) As Byte iMaxSockets As Integer iMaxUdpDg As Integer lpVendorInfo As Long End Type Private Type HOSTENT hname As Long hAliases As Long hAddrType As Integer hLength As Integer hAddrList As Long End Type Private Declare Function WSAStartup Lib "ws2_32.dll" (ByVal _ wVersionRequired&, lpWSAData As WSA_DATA) As Long Private Declare Function gethostbyname Lib "ws2_32.dll" (ByVal hostname$) As Long Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal length As Long) Private Declare Function WSACleanup Lib "ws2_32.dll" () As Long '===================以上定义用于获得本机IP==================
'=====================以下定义用于获得MAC==================== Private Declare Function SendARP Lib "iphlpapi" (ByVal dest As Long, ByVal host As Long, ByRef Mac As Any, ByRef length As Long) As Long Private Declare Function inet_addr Lib "ws2_32.dll" (ByVal cp As String) As Long '=====================以上定义用于获得MAC====================
'获得指定IP地址的MAC地址,用到全局变量hostIpStr为本机IP地址 '输入:IP为本网内的IP地址字符串,函数返回MAC地址,若出错返回空字符串 Function GetMac(IP As String) As String Dim ldest As Long, lhost As Long, Mac(5) As Byte, length As Long Dim i As Long, lR As Long, hostIpStr As String hostIpStr = GetMyIp GetMac = "" ' 若得不到MAC! If hostIpStr <> "" Then ldest = inet_addr(IP) '//目的地的IP转换为IP内码形式 lhost = inet_addr(hostIpStr) '//将本机IP转换为IP内码形式 length = 6 lR = SendARP(ldest, lhost, Mac(0), length) If length > 0 Then For i = 0 To length - 1 GetMac = GetMac & Right("00" & Hex(Mac(i)), 2) Next i End If End If End Function
'获得本机IP地址,函数返回值=本机IP地址,若出错返回空字符串 Function GetMyIp() As String Dim WSAD As WSA_DATA Dim lR As Long, MyIp As String Dim hostent_addr As Long Dim host As HOSTENT Dim hostip_addr As Long Dim temp_ip_address() As Byte Dim i As Integer Dim ip_address As String lR = WSAStartup(&H202, WSAD) If lR <> 0 Then 'WSANOERROR Then MsgBox "启动WSAStartup失败!" GetMyIp = "" Exit Function End If hostent_addr = gethostbyname("")
If hostent_addr = 0 Then GetMyIp = "" '注释:主机名不能被解释 Exit Function End If
ReDim temp_ip_address(1 To host.hLength) CopyMemory temp_ip_address(1), ByVal hostip_addr, host.hLength
For i = 1 To host.hLength ip_address = ip_address & temp_ip_address(i) & "." Next ip_address = Mid$(ip_address, 1, Len(ip_address) - 1) GetMyIp = ip_address WSACleanup End Function
Private Sub Form_Load() MsgBox GetMyIp MsgBox GetMac(GetMyIp) MsgBox GetMac("192.168.1.1") End Sub
囚我在心上
2024-11-26 08:04:00
Public Declare Function inet_addr Lib "wsock32.dll" (ByVal s As String) As Long Public Declare Function SendARP Lib "iphlpapi.dll" (ByVal DestIP As Long, ByVal SrcIP As Long, pMacAddr As Long, PhyAddrLen As Long) As Long Public Declare Sub CopyMemory1 Lib "kernel32" Alias "RtlMoveMemory" (dst As Any, src As Any, ByVal bcount As Long)
Public Function GetRemoteMACAddress(ByVal sRemoteIP As String) Dim dwRemoteIP As Long Dim pMacAddr As Long Dim bpMacAddr() As Byte Dim PhyAddrLen As Long Dim cnt As Long Dim tmp As String 'convert the string IP into 'an unsigned long value containing 'a suitable binary representation 'of the Internet address given dwRemoteIP = inet_addr(sRemoteIP) If dwRemoteIP <> 0 Then 'set PhyAddrLen to 6 PhyAddrLen = 6 'retrieve the remote MAC address On Error Resume Next If SendARP(dwRemoteIP, 0&, pMacAddr, PhyAddrLen) = 0 Then 'GetRemoteMACAddress = pMacAddr 'Exit Function 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) CopyMemory1 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 If Len(Hex$(bpMacAddr(cnt))) = 1 Then tmp = tmp & "0" & Hex$(bpMacAddr(cnt)) & "-" Else tmp = tmp & Hex$(bpMacAddr(cnt)) & "-" End If End If Next 'remove the trailing dash 'added above and return True If Len(tmp) > 0 Then 'sRemoteMacAddress = Left$(tmp, Len(tmp) - 1) GetRemoteMACAddress = Left$(tmp, Len(tmp) - 1) End If Exit Function Else GetRemoteMACAddress = False End If Else GetRemoteMACAddress = False End If 'SendARP
Else GetRemoteMACAddress = False End If 'dwRemoteIP End Function