[分享]纯VB实现ping(能否ping通)的功能,调用方便

三色 2009-06-03 03:38:35
加精
'调用:msgbox IPValid("192.168.1.1")


'模块代码,以下代码统一放入一个模块中mPing
'互联网搜索得到,略加整理
Private Const IP_STATUS_BASE = 11000
Private Const IP_SUCCESS = 0
Private Const IP_BUF_TOO_SMALL = (11000 + 1)
Private Const IP_DEST_NET_UNREACHABLE = (11000 + 2)
Private Const IP_DEST_HOST_UNREACHABLE = (11000 + 3)
Private Const IP_DEST_PROT_UNREACHABLE = (11000 + 4)
Private Const IP_DEST_PORT_UNREACHABLE = (11000 + 5)
Private Const IP_NO_RESOURCES = (11000 + 6)
Private Const IP_BAD_OPTION = (11000 + 7)
Private Const IP_HW_ERROR = (11000 + 8)
Private Const IP_PACKET_TOO_BIG = (11000 + 9)
Private Const IP_REQ_TIMED_OUT = (11000 + 10)
Private Const IP_BAD_REQ = (11000 + 11)
Private Const IP_BAD_ROUTE = (11000 + 12)
Private Const IP_TTL_EXPIRED_TRANSIT = (11000 + 13)
Private Const IP_TTL_EXPIRED_REASSEM = (11000 + 14)
Private Const IP_PARAM_PROBLEM = (11000 + 15)
Private Const IP_SOURCE_QUENCH = (11000 + 16)
Private Const IP_OPTION_TOO_BIG = (11000 + 17)
Private Const IP_BAD_DESTINATION = (11000 + 18)
Private Const IP_ADDR_DELETED = (11000 + 19)
Private Const IP_SPEC_MTU_CHANGE = (11000 + 20)
Private Const IP_MTU_CHANGE = (11000 + 21)
Private Const IP_UNLOAD = (11000 + 22)
Private Const IP_ADDR_ADDED = (11000 + 23)
Private Const IP_GENERAL_FAILURE = (11000 + 50)
Private Const MAX_IP_STATUS = 11000 + 50
Private Const IP_PENDING = (11000 + 255)
Private Const PING_TIMEOUT = 200
Private Const WS_VERSION_REQD = &H101
Private Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF&
Private Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
Private Const MIN_SOCKETS_REQD = 1
Private Const SOCKET_ERROR = -1
Private Const MAX_WSADescription = 256
Private Const MAX_WSASYSStatus = 128
Private Type ICMP_OPTIONS
Ttl As Byte
Tos As Byte
Flags As Byte
OptionsSize As Byte
OptionsData As Long
End Type
Dim ICMPOPT As ICMP_OPTIONS
Private Type ICMP_ECHO_REPLY
Address As Long
status As Long
RoundTripTime As Long
DataSize As Integer
Reserved As Integer
DataPointer As Long
Options As ICMP_OPTIONS
Data As String * 250
End Type
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
Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
Private Declare Function IcmpCloseHandle Lib "icmp.dll" _
(ByVal IcmpHandle As Long) _
As Long
Private Declare Function IcmpSendEcho Lib "icmp.dll" _
(ByVal IcmpHandle As Long, _
ByVal DestinationAddress As Long, _
ByVal RequestData As String, _
ByVal RequestSize As Integer, _
ByVal RequestOptions As Long, _
ReplyBuffer As ICMP_ECHO_REPLY, _
ByVal ReplySize As Long, _
ByVal Timeout As Long) _
As Long
Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
Private Declare Function WSAStartup Lib "WSOCK32.DLL" _
(ByVal wVersionRequired As Long, _
lpWSADATA As WSADATA) _
As Long
Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
...全文
5303 74 打赏 收藏 转发到动态 举报
写回复
用AI写文章
74 条回复
切换为时间正序
请发表友善的回复…
发表回复
oshi002 2011-10-15
  • 打赏
  • 举报
回复
哦,好长的代码,看也要看好久
红叶哥 2011-10-06
  • 打赏
  • 举报
回复
好貼,頂了
sanyue_yingfei 2011-09-23
  • 打赏
  • 举报
回复
不错,但是不是很明白
bestwaysz 2011-07-30
  • 打赏
  • 举报
回复
用这个连续PING一个地址有可能就会出现PING不通的情况,返回码11010
mezhe 2011-07-22
  • 打赏
  • 举报
回复
学习了。
panyulirong 2011-06-12
  • 打赏
  • 举报
回复
mark
seraphxylg 2010-06-04
  • 打赏
  • 举报
回复
還沒到看懂得級別.先訂下
颖哥儿 2010-06-02
  • 打赏
  • 举报
回复
呵呵,谢谢分享
太愚散人 2009-12-18
  • 打赏
  • 举报
回复
好贴
crazyxf 2009-09-15
  • 打赏
  • 举报
回复
Private Type HOSTENT
hname As Long
hAliases As Long
hAddrType As Integer
hLength As Integer
hAddrList As Long
End Type
Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal _
hostname$) As Long
Private Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As Any, _
ByVal hpvSource&, ByVal cbCopy&)
Public Function GetIPByName(name As String) 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
GetIPByName = ""
hostent_addr = gethostbyname(name)

If hostent_addr = 0 Then
Exit Function
End If

RtlMoveMemory host, hostent_addr, LenB(host)
RtlMoveMemory hostip_addr, host.hAddrList, 4

ReDim temp_ip_address(1 To host.hLength)
RtlMoveMemory temp_ip_address(1), 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)

GetIPByName = ip_address

End Function
利用这个就可以吧域名转换成ip,用楼主方法判断是否可以ping通,这样就完美了
crazyxf 2009-09-15
  • 打赏
  • 举报
回复
非常不错,可惜不支持域名,没关系我把域名解析放上来。
qiji2007 2009-08-22
  • 打赏
  • 举报
回复
放到模块里为什么还是private?

我测试了下 只弹出false 不知道为什么

菜鸟一头 ME
yishanlin 2009-06-30
  • 打赏
  • 举报
回复
学习了~~~~~~~~~~~~~~~~~``
红叶哥 2009-06-29
  • 打赏
  • 举报
回复
收藏
yao555beijing2008 2009-06-11
  • 打赏
  • 举报
回复
看不懂!但是还是顶下
lyre129 2009-06-05
  • 打赏
  • 举报
回复
学习了
q8899werty 2009-06-04
  • 打赏
  • 举报
回复
很好啊!厉害!
coffee5cat 2009-06-04
  • 打赏
  • 举报
回复
谢谢分享·~~
candy6887 2009-06-04
  • 打赏
  • 举报
回复
看不懂
xiaoku 2009-06-04
  • 打赏
  • 举报
回复
好多年没用 vb 了... 怀念...
加载更多回复(50)

7,781

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧