📄 cping.cls
字号:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "CPing"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' CPing.cls - http://www.allapi.net/apilist/example.php?example=Ping
Option Explicit
Const SOCKET_ERROR = 0
Private Type WSAdata
wVersion As Integer
wHighVersion As Integer
szDescription(0 To 255) As Byte
szSystemStatus(0 To 128) As Byte
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As Long
End Type
Private Type Hostent
h_name As Long
h_aliases As Long
h_addrtype As Integer
h_length As Integer
h_addr_list As Long
End Type
Private Type IP_OPTION_INFORMATION
TTL As Byte
Tos As Byte
Flags As Byte
OptionsSize As Long
OptionsData As String * 128
End Type
Private Type IP_ECHO_REPLY
Address(0 To 3) As Byte
Status As Long
RoundTripTime As Long
DataSize As Integer
Reserved As Integer
data As Long
Options As IP_OPTION_INFORMATION
End Type
Private Declare Function GetHostByName Lib "wsock32.dll" Alias "gethostbyname" (ByVal HostName As String) As Long
Private Declare Function WSAStartup Lib "wsock32.dll" (ByVal wVersionRequired&, lpWSAdata As WSAdata) As Long
Private Declare Function WSACleanup Lib "wsock32.dll" () As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
Private Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal HANDLE As Long) As Boolean
Private Declare Function IcmpSendEcho Lib "ICMP" (ByVal IcmpHandle As Long, ByVal DestAddress As Long, ByVal RequestData As String, ByVal RequestSize As Integer, RequestOptns As IP_OPTION_INFORMATION, ReplyBuffer As IP_ECHO_REPLY, ByVal ReplySize As Long, ByVal TimeOut As Long) As Boolean
' Return value: error status: 0=Ok
Public Function Ping( _
ByVal HostName As String, _
Optional ByVal TimeOut_ms As Long = 3000, _
Optional outIP As String, _
Optional outRoundTripTime_ms As Long, _
Optional outErrorDescription As String _
) As Integer
Dim retval_int As Integer
Dim lpWSAdata As WSAdata
retval_int = 0
outIP = 0
outRoundTripTime_ms = 0
outErrorDescription = ""
Call WSAStartup(&H101, lpWSAdata)
Dim hostent_ptr As Long
hostent_ptr = GetHostByName(HostName & String(64 - Len(HostName), 0))
If (hostent_ptr = 0) Then
retval_int = -1
outErrorDescription = "DNS error"
Else
Dim hFile As Long
Dim hHostent As Hostent
Dim AddrList As Long
Dim Address As Long
CopyMemory hHostent.h_name, ByVal hostent_ptr, Len(hHostent)
CopyMemory AddrList, ByVal hHostent.h_addr_list, 4
CopyMemory Address, ByVal AddrList, 4
hFile = IcmpCreateFile()
If hFile = 0 Then
retval_int = -2
outErrorDescription = "Unable to Create ICMP Handle"
Else
Dim EchoReply As IP_ECHO_REPLY
Dim OptInfo As IP_OPTION_INFORMATION
Dim replies_count As Long
OptInfo.TTL = 255
replies_count = IcmpSendEcho(hFile, Address, String(32, "A"), 32, OptInfo, EchoReply, Len(EchoReply) + 8, TimeOut_ms)
If (replies_count > 0) Then
retval_int = EchoReply.Status ' 0=Ok
outRoundTripTime_ms = EchoReply.RoundTripTime
outIP = CStr(EchoReply.Address(0)) & "." & CStr(EchoReply.Address(1)) & "." & CStr(EchoReply.Address(2)) & "." & CStr(EchoReply.Address(3))
Else
retval_int = -3
outErrorDescription = "Timeout"
End If
Call IcmpCloseHandle(hFile)
End If
End If
Call WSACleanup
Ping = retval_int
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -