原创: CSDN 许仙 ; qq:19030300 主页:http://hot1kang1.126.com
'转载请保持信息的完整性 谢谢
'参考网络代码完成, 获取机器的MAC 网卡地址
VB获取机器的硬盘物理地址和硬盘型号 http://blog.csdn.net/hot1kang1/archive/2006/03/27/639735.aspx
'----------------
'GetMyMAC 获得本机器网卡 MAC地址 若无IP 返回空字符
' Print GetMyMAC
'GetRemoteMACAddress IP,strval 获取对应机器的网卡 MAC地址
'dim i
'GetRemoteMACAddress "192.168.0.1",i
'Print i
'
'GetIPAddress 返回给定机器名的Ip地址,机器名为空时返回本机Ip地址
' GetIPAddress 机器名
'----------------
Option Explicit
Private Declare Function gethostname _
Lib "wsock32.dll" (ByVal szHost As String, _
ByVal dwHostLen As Long) As Long
Private Declare Function gethostbyname _
Lib "wsock32.dll" (ByVal szHost As String) As Long
Private Declare Function WSAStartup _
Lib "wsock32.dll" (ByVal wVersionRequired As Long, _
lpWSADATA As WSADATA) As Long
Private Declare Function WSAGetLastError _
Lib "wsock32.dll" () As Long
Private Declare Function WSACleanup _
Lib "wsock32.dll" () As Long
Private Declare Sub CopyMemory _
Lib "kernel32" _
Alias "RtlMoveMemory" (hpvDest As Any, _
ByVal hpvSource As Long, _
ByVal cbCopy As Long)
' Socket错误常数和版本常数
Private Const SOCKET_ERROR As Long = -1
Private Const MAX_WSADescription = 256
Private Const MAX_WSASYSStatus = 128
Private Const ERROR_SUCCESS As Long = 0
Private Const WS_VERSION_REQD As Long = &H101
Private Const MIN_SOCKETS_REQD As Long = 1
Private Const WS_VERSION_MAJOR As Long = WS_VERSION_REQD / &H100 And &HFF&
Private Const WS_VERSION_MINOR As Long = WS_VERSION_REQD And &HFF&
' 存放主机信息的结构
Private Type HOSTENT
hName As Long ' 主机的正式名称
hAliases As Long ' 主机别名列表
hAddrType As Integer ' 主机地址类型
hLen As Integer ' 主机地址长度
hAddrList As Long ' 主机IP地址列表
End Type
' 存放Winsock版本等信息的结构
Private Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(0 To MAX_WSADescription) As Byte
szSystemStatus(0 To MAX_WSASYSStatus) As Byte
wMaxSockets As Integer
wMaxUDPDG As Integer
dwVendorInfo As Long
End Type
'---------------------------------
'MAC使用的API
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 CopyMemory1 _
Lib "kernel32" _
Alias "RtlMoveMemory" (dst As Any, _
src As Any, _
ByVal bcount As Long)
'---------------------------------
Public Function GetMyMAC() As String
Dim sMyIP$, sMyMAC$
sMyIP$ = GetIPAddress
If sMyIP = "" Then Exit Function
GetRemoteMACAddress sMyIP$, sMyMAC$
GetMyMAC = sMyMAC$
End Function
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)
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
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
'------------------------------
' 返回给定机器名的Ip地址,机器名为空时返回本机Ip地址
Public Function GetIPAddress(Optional sHost As String) As String
Dim sHostName As String * 256
Dim lpHost As Long
Dim HOST As HOSTENT
Dim dwIPAddr As Long
Dim tmpIPAddr() As Byte
Dim i As Integer
Dim sIPAddr As String
Dim werr As Long
' 如果无法初始化Socket则退出函数
If Not SocketsInitialize() Then
GetIPAddress = ""
Exit Function
End If
' 如果未指定主机名称,则取得本地主机名称并获取其IP
If sHost = "" Then
If gethostname(sHostName, 256) = SOCKET_ERROR Then
werr = WSAGetLastError()
GetIPAddress = ""
SocketsCleanup
Exit Function
End If
sHostName = Trim$(sHostName)
Else
sHostName = Trim$(sHost) & Chr$(0)
End If
' 获得指向主机信息结构的指针
lpHost = gethostbyname(sHostName)
' 如果指针为零,则错误退出
If lpHost = 0 Then
werr = WSAGetLastError()
GetIPAddress = ""
SocketsCleanup
Exit Function
End If
' 从指定内存取得数据
CopyMemory HOST, lpHost, Len(HOST)
CopyMemory dwIPAddr, HOST.hAddrList, 4
' 重新动态分配变量内存
ReDim tmpIPAddr(1 To HOST.hLen)
' 将主机地址存储到tmpIPAddr中
CopyMemory tmpIPAddr(1), dwIPAddr, HOST.hLen
' 获得最终的主机IP地址字符串
For i = 1 To HOST.hLen
sIPAddr = sIPAddr & tmpIPAddr(i) & "."
Next
' 返回
GetIPAddress = Mid$(sIPAddr, 1, Len(sIPAddr) - 1)
' 释放Socket库所占用的系统资源
SocketsCleanup
End Function
' 初始化Socket
Public Function SocketsInitialize(Optional sErr As String) As Boolean
Dim WSAD As WSADATA
Dim sLoByte As String
Dim sHiByte As String
' 初始化Winsock DLL,并判断版本是否满足要求
If WSAStartup(WS_VERSION_REQD, WSAD) <> ERROR_SUCCESS Then
sErr = "The 32-bit Windows Socket is not responding."
SocketsInitialize = False
Exit Function
End If
' 判断是否有支持足够的Socket
If WSAD.wMaxSockets < MIN_SOCKETS_REQD Then
sErr = "This application requires a minimum of " & CStr(MIN_SOCKETS_REQD) & " supported sockets."
SocketsInitialize = False
Exit Function
End If
' 判断Winsock的版本是否被32为Winsock支持
If LoByte(WSAD.wVersion) < WS_VERSION_MAJOR Or (LoByte(WSAD.wVersion) = WS_VERSION_MAJOR And HiByte(WSAD.wVersion) < WS_VERSION_MINOR) Then
sHiByte = CStr(HiByte(WSAD.wVersion))
sLoByte = CStr(LoByte(WSAD.wVersion))
sErr = "Sockets version " & sLoByte & "." & sHiByte & " is not supported by 32-bit Windows Sockets."
SocketsInitialize = False
Exit Function
End If
SocketsInitialize = True
End Function
' 释放Socket库所占用的系统资源
Public Sub SocketsCleanup()
If WSACleanup() <> ERROR_SUCCESS Then
End If
End Sub
' 获得一个整数的高字节位
Public Function HiByte(ByVal wParam As Integer)
HiByte = wParam / &H1 And &HFF&
End Function
' 获得一个整数的低字节位
Public Function LoByte(ByVal wParam As Integer)
LoByte = wParam And &HFF&
End Function
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/hot1kang1/archive/2006/03/27/639713.aspx