注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

浩瀚星云

上善若水 厚德载物<学习版>

 
 
 

日志

 
 

获取网络中计算机网卡的MAC地址  

2008-06-08 09:46:14|  分类: VB程序设计 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
'Author: SERRANA
'Author's email:
'Date Submitted: 7/30/2005
'Compatibility: VB 6,VB 5

'Task: Retrieve a remote MAC Address

'Declarations No declarations

'Code:
Option Explicit

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 Sub Form_Load()

Text1.Text = "192.168.1.101"
Text2.Text = ""
Command1.Caption = "Get Remote Mac Address"

End Sub

Private Sub Command1_Click()

Dim sRemoteMacAddress As String

If Len(Text1.Text) > 0 Then

If GetRemoteMACAddress(Text1.Text, sRemoteMacAddress) Then
Text2.Text = sRemoteMacAddress
Else
Text2.Text = "(SendARP call failed)"
End If

End If

End Sub

Private Function GetRemoteMACAddress(ByVal sRemoteIP As String, _
sRemoteMacAddress 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

'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
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
sRemoteMacAddress = 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
  评论这张
 
阅读(206)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017