我有一个 vb6 代码,用于测试 PC 是否存在互联网连接。我利用检查谷歌 DNS 来检查它。它在 Windows XP 中运行良好。但在 Windows 8 中,无论是否连接互联网总是返回成功(互联网已连接)。我正在使用下面是编码的一部分
Private Function CheckForInternet(ByVal ServerIP As String, ByRef IsTimedOut As Boolean) A
s Boolean
On Error GoTo CheckForInternet_EH
Dim Reply As ICMP_ECHO_REPLY
Dim lngSuccess As Long
Dim strIPAddress As String
Dim a As String
Dim startTimer As Single
Dim EndTimer As Single
Const Time_out_in_ms As Integer = 1000
'Get the sockets ready.
If SocketsInitialize() Then
'Address to ping
strIPAddress = ServerIP
'Ping the IP that is passing the address and get a reply.
lngSuccess = ping(strIPAddress, Time_out_in_ms, Reply)
'Clean up the sockets.
SocketsCleanup
''Return Value
If lngSuccess = ICMP_SUCCESS Then
CheckForInternet = True
ElseIf lngSuccess = ICMP_STATUS_REQUEST_TIMED_OUT Then
IsTimedOut = True
End If
'Else
' 'Winsock error failure, initializing the sockets.
' Debug.Print WINSOCK_ERROR
End If
Exit Function
CheckForInternet_EH:
Call msglog(Err.Description & Space(10) & "CheckForInternet", False)
End Function
以下是ping程序
Public Function ping(ByVal sAddress As String, ByVal time_out As Long, Reply As ICMP_ECHO_REPLY) As Long
On Error GoTo ping_EH
Dim hIcmp As Long
Dim lAddress As Long
Dim lTimeOut As Long
Dim StringToSend As String
'Short string of data to send
StringToSend = "hello"
'ICMP (ping) timeout
lTimeOut = time_out ''ms
'Convert string address to a long representation.
lAddress = inet_addr(sAddress)
If (lAddress <> -1) And (lAddress <> 0) Then
'Create the handle for ICMP requests.
hIcmp = IcmpCreateFile()
If hIcmp Then
'Ping the destination IP address.
Call IcmpSendEcho(hIcmp, lAddress, StringToSend, Len(StringToSend), 0, Reply, Len(Reply), lTimeOut)
'Reply status
ping = Reply.Status
'Close the Icmp handle.
IcmpCloseHandle hIcmp
Else
'Debug.Print "failure opening icmp handle."
ping = -1
End If
Else
ping = -1
End If
Exit Function
ping_EH:
Call msglog(Err.Description & Space(10) & "ping", False)
End Function
它只是编码的一部分(我确实正确地传递了参数,例如 sAddress 和谷歌的 DNS 等)。现在我观察到,当 windows xp ping = Reply.Status 有互联网连接时,返回 0(表示成功)。在 Windows 8 中也是如此。但是当没有 Internet 连接时,Windows xp 返回 ping 值为 11003(这意味着没有 Internet 连接)。但在 Windows 8 中它仍然返回 0,表示成功。
所以我认为返回错误值的 IcmpSendEcho 函数有问题
我也定义了以下
Private Declare Function IcmpSendEcho Lib "icmp.dll" _
(ByVal IcmpHandle As Long, _
ByVal DestinationAddress As Long, _
ByVal RequestData As String, _
ByVal RequestSize As Long, _
ByVal RequestOptions As Long, _
ReplyBuffer As ICMP_ECHO_REPLY, _
ByVal ReplySize As Long, _
ByVal Timeout As Long) As Long
'This structure describes the options that will be included in the header of an IP packet.
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcetcpip/htm/cerefIP_OPTION_INFORMATION.asp
Private Type IP_OPTION_INFORMATION
Ttl As Byte
Tos As Byte
Flags As Byte
OptionsSize As Byte
OptionsData As Long
End Type
'This structure describes the data that is returned in response to an echo request.
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcesdkr/htm/_wcesdk_icmp_echo_reply.asp
Public Type ICMP_ECHO_REPLY
address As Long
Status As Long
RoundTripTime As Long
DataSize As Long
Reserved As Integer
ptrData As Long
Options As IP_OPTION_INFORMATION
Data As String * 250
End Type
提示:也在链接中IcmpSendEcho IP_OPTION_INFORMATION for 64 bit pc is different etc.. etc.. 在链接中,它被提及为“用于保存对回显请求的任何回复的缓冲区。返回后,缓冲区包含一个 ICMP_ECHO_REPLY 结构数组由回复的选项和数据决定。缓冲区应该足够大以容纳至少一个 ICMP_ECHO_REPLY 结构加上 RequestSize 字节的数据。所以我现在想知道如何声明 ICMP_ECHO_REPLY32 和 IP_OPTION_INFORMATION32 ?(我只使用了 ICMP_ECHO_REPLY 和 IP_OPTION_INFORMATION)所以请帮我解决问题