在Windows 8中检查互联网连接是否有错误

时间:2013-05-22 14:49:09

标签: vb6 icmp

我有一个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

它只是编码的一部分(我确实传递参数正确,如sogle与谷歌的DNS等)。现在我已经观察到,当在Windows中的互联网连接xp ping = Reply.Status返回0(这是为在Windows 8中也是如此。但是当互联网连接不存在时,windows xp将ping值返回11003(这意味着没有互联网连接)。但是在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中,64位pc的IP_OPTION_INFORMATION等等...等等。 在链接中,它被称为“一个缓冲区,用于保存对echo请求的任何回复。返回时,缓冲区包含一个ICMP_ECHO_REPLY结构数组,后跟响应的选项和数据。缓冲区应该足够大,至少可以容纳一个ICMP_ECHO_REPLY结构加上RequestSize字节数据。“ 所以我现在想要如何声明ICMP_ECHO_REPLY32和IP_OPTION_INFORMATION32?(我只使用了ICMP_ECHO_REPLY和IP_OPTION_INFORMATION) 所以请帮我解决问题

1 个答案:

答案 0 :(得分:1)

为了检查互联网连接,我做了两个简单的功能:

Option Explicit
Private Declare Function InternetCheckConnectionA Lib "wininet.dll" (ByVal lpszUrl As String, ByVal dwFlags As Long, ByVal dwReserved As Long) As Long

Private Const FLAG_ICC_FORCE_CONNECTION  As Long = &H1

Public Function IsInternetOn() As Boolean
    IsInternetOn = InternetCheckConnectionA("http://www.google.com/", FLAG_ICC_FORCE_CONNECTION, 0&)
End Function

第二个:

Option Explicit
Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpdwFlags As Long, ByVal dwReserved As Long) As Long

Private Function IsInternetOn() As Boolean
   IsInternetOn = InternetGetConnectedState(0&, 0&)
End Function

电话示例:

Msgbox IsInternetOn()