各位高手好,小弟先有理了!小弟最近想通过vb实现在局域网中通过mac地址查找到该机的ip地址,找了不少资料,知道似乎要用rarp协议。但小弟编程资质驽钝,率试皆败。编程截至日期将近,却无头绪,真是万分焦急。还望有高手们指点迷津。若能帮小弟找到源程序,真是感激涕零!
解决方案 »
- 特急,在线,请教各位仁兄,如何在VB中实现对SQLServer数据文件、日志文件的备份及恢复
- 哪位GG给介绍一下,BEA的weblogic,和IBM的websphere,这两东西是语言还是平台呀,用什么语言呢,主要用途呢
- VB读取导出的新闻列表TXT文本,分离出新闻标题和内容,如何操作?
- 鼠标事件
- 关于水晶报表?请知道的回答我几句?
- 如何让richtextbox支持语法高亮显示
- ::::::::用VB对XML这样操作,能行吗?请高手指教,在线等候,急 急 急 !!!!!
- 用savepicture如何定义保存的图象的长度和宽度!
- 如何在 picturebox 中显示*.jpg 图片?
- 紧急紧急!!!
- 高手:Word 中这样的代码如何实现(无法通过录制宏得到)
- 怎样用VB模拟散体(方格)从容器底部孔口放出过程?
晚上整一个上去。请楼主去看看,或者给我网站上留言.
谢谢
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
End TypePrivate 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
'类型声明
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)
'函数声明
Private Function GetIPAddress(Optional sHost As String) As String
'返回给定机器名的IP地址,机器名为空时返回本机IP地址
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 LongIf Not SocketsInitialize() Then '如果初始化失败
GetIPAddress = ""
Exit Function
End IfIf sHost = "" Then '如果计算机名为空,则显示本机IP
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 IflpHost = gethostbyname(sHostName)
'调用gethostbyname函数
If lpHost = 0 Then '如果出错
werr = WSAGetLastError()
GetIPAddress = ""
SocketsCleanup
Exit Function
End IfCopyMemory HOST, lpHost, Len(HOST)
CopyMemory dwIPAddr, HOST.hAddrList, 4
ReDim tmpIPAddr(1 To HOST.hLen)
CopyMemory tmpIPAddr(1), dwIPAddr, HOST.hLenFor i = 1 To HOST.hLen
sIPAddr = sIPAddr & tmpIPAddr(i) & "."
Next
'将IP地址存储在sIPaddr中
GetIPAddress = Mid$(sIPAddr, 1, Len(sIPAddr) - 1)
SocketsCleanupEnd FunctionPrivate Function SocketsInitialize(Optional sErr As String) As Boolean
Dim WSAD As WSADATA, sLoByte As String, sHiByte As String
If WSAStartup(WS_VERSION_REQD, WSAD) <> ERROR_SUCCESS Then
sErr = "The 32-bit Windows Socket is not responding."
SocketsInitialize = False
Exit Function
End IfIf 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
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 FunctionPrivate Sub SocketsCleanup()
If WSACleanup() <> ERROR_SUCCESS Then
App.LogEvent "Socket error occurred in Cleanup.", vbLogEventTypeError
End If
End SubPrivate Function HiByte(ByVal wParam As Integer)
HiByte = wParam \ &H1 And &HFF&
End FunctionPrivate Function LoByte(ByVal wParam As Integer)
LoByte = wParam And &HFF&
End FunctionPrivate Sub Command1_Click()
On Error Resume Next
Screen.MousePointer = vbHourglass
lblIP.Caption = GetIPAddress(TxtCmpName.Text)
If lblIP.Caption = "" Then
MsgBox "你输入的机器名不存在或还没连到本局域网内,请重新输入!", vbExclamation, "系统提示"
TxtCmpName.SetFocus
End IfScreen.MousePointer = vbDefaultEnd Sub
----------------------------
以上是我自己写的通过机器名获取局域网内机器的IP
Private Const NCBNAMSZ = 16
Private Const HEAP_ZERO_MEMORY = &H8
Private Const HEAP_GENERATE_EXCEPTIONS = &H4
Private Const NCBRESET = &H32
'类型声明
Private Type NCB
ncb_command As Byte 'Integer
ncb_retcode As Byte 'Integer
ncb_lsn As Byte 'Integer
ncb_num As Byte 'Integer
ncb_buffer As Long 'String
ncb_length As Integer
ncb_callname As String * NCBNAMSZ
ncb_name As String * NCBNAMSZ
ncb_rto As Byte 'Integer
ncb_sto As Byte 'Integer
ncb_post As Long
ncb_lana_num As Byte 'Integer
ncb_cmd_cplt As Byte 'Integer
ncb_reserve(9) As Byte 'Reserved,must be 0
ncb_event As Long
End Type
Private Type ADAPTER_STATUS
adapter_address(5) As Byte 'As String *6
rev_major As Byte 'Integer
reserved0 As Byte 'Integer
adapter_type As Byte 'Integer
rev_minor As Byte 'Integer
duration As Integer
frmr_recv As Integer
frmr_xmit As Integer
iframe_recv_err As Integer
xmit_aborts As Integer
xmit_success As Long
recv_success As Long
iframe_xmit_err As Integer
recv_buff_unavail As Integer
t1_timeouts As Integer
ti_timeouts As Integer
Reserved1 As Long
free_ncbs As Integer
max_cfg_ncbs As Integer
max_ncbs As Integer
xmit_buf_unavail As Integer
max_dgram_size As Integer
pending_sess As Integer
max_cfg_sess As Integer
max_sess As Integer
max_sess_pkt_size As Integer
name_count As Integer
End Type
Private Type NAME_BUFFER
name As String * NCBNAMSZ
name_num As Integer
name_flags As Integer
End Type
Private Type ASTAT
adapt As ADAPTER_STATUS
NameBuff(30) As NAME_BUFFER
End Type
'函数声明
Private Declare Function Netbios Lib "netapi32.dll" (pncb As NCB) As Byte
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
Private Declare Function GetProcessHeap Lib "kernel32" () As Long
Private Declare Function HeapAlloc Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, lpMem As Long) As Long
Dim txt(0 To 5) As StringPrivate Sub Command1_Click()
Dim myNcb As NCB
Dim bRet As Byte
'在使网卡能够接受其他NCB命令之前,必须先复原RESET
myNcb.ncb_command = NCBRESET
bRet = Netbios(myNcb)
myNcb.ncb_command = NCBASTAT
myNcb.ncb_lana_num = 0
myNcb.ncb_callname = "* "
Dim myASTAT As ASTAT
Dim pASTAT As Long
myNcb.ncb_length = Len(myASTAT)
' Debug.Print Err.LastDllError
pASTAT = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS Or HEAP_ZERO_MEMORY, myNcb.ncb_length)
If pASTAT = 0 Then
' Debug.Print "memory allcoation failed!"
Exit Sub
End If myNcb.ncb_buffer = pASTAT
bRet = Netbios(myNcb)
' Debug.Print Err.LastDllError
CopyMemory myASTAT, myNcb.ncb_buffer, Len(myASTAT)
For i = 0 To 5
txt(i) = Hex(myASTAT.adapt.adapter_address(i))
Select Case txt(i)
Case 0
txt(i) = "0" & txt(i)
Case 1
txt(i) = "0" & txt(i)
Case 2
txt(i) = "0" & txt(i)
Case 3
txt(i) = "0" & txt(i)
Case 4
txt(i) = "0" & txt(i)
Case 5
txt(i) = "0" & txt(i)
Case 6
txt(i) = "0" & txt(i)
Case 7
txt(i) = "0" & txt(i)
Case 8
txt(i) = "0" & txt(i)
Case 9
txt(i) = "0" & txt(i)
Case A
txt(i) = "0" & txt(i)
Case B
txt(i) = "0" & txt(i)
Case C
txt(i) = "0" & txt(i)
Case D
txt(i) = "0" & txt(i)
Case E
txt(i) = "0" & txt(i)
Case F
txt(i) = "0" & txt(i)
End Select
Next i
Text1.Text = txt(0) & " " & txt(1) & " " & txt(2) & " " & txt(3) & " " & txt(4) & " " & txt(5)
' Text1.Text = Hex(myASTAT.adapt.adapter_address(0)) & " " & _
Hex(myASTAT.adapt.adapter_address(1)) & " " & _
Hex(myASTAT.adapt.adapter_address(2)) & " " & _
Hex(myASTAT.adapt.adapter_address(3)) & " " & _
Hex(myASTAT.adapt.adapter_address(4)) & " " & _
Hex(myASTAT.adapt.adapter_address(5))
HeapFree GetProcessHeap(), 0, pASTAT
End Sub
---------------
获取本机MAC
----------------------
你的代码怎么用啊? 具体指点一下嘛
-----------------
等等
里面放一个texbox 控件,名称为TxtCmpName (输入机器名)
一个label 控件,名称为lblIP (显示ip地址用)
一个按钮控件
---------------
很好用的^_^