vb 搜索局域网计算机,vb6.0如何获得局域网所有IP地址?

满意答案

dcebd7a0de6265b6ccae5ead692f1eab.png

zhangzha哇呀

2015.12.05

dcebd7a0de6265b6ccae5ead692f1eab.png

采纳率:55%    等级:12

已帮助:7101人

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

CopyMemory host, ByVal hostent_addr, LenB(host)

CopyMemory hostip_addr, ByVal host.hAddrList, 4

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

00分享举报

  • 0
    点赞
  • 0
    收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:数字20 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值