Results 1 to 9 of 9

Thread: Check for internet connection gives error in windows 8

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    248

    Check for internet connection gives error in windows 8

    I Have a vb6 code which is used to test whether internet connection is there or not for a PC.I make use of checking google DNS for it.It works fine in Windows XP.But in Windows 8 if internet is connected or not it always returns success(Internet is connected). I am making use of Below is part of coding
    Code:
    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
    And below is ping procedure
    Code:
    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
    It is only a part of coding(I do pass parameters properly such as sAddress with DNS of google etc).Now i have observed that when internet connection is there in windows xp ping = Reply.Status returns 0 (which is for success).Same is the case in Windows 8 also.But when internet connection is not there windows xp returns ping value as 11003(which means no internet connection).But in windows 8 it still returns 0 which is for success.

    So i think it is problem with IcmpSendEcho function which returns wrong value

    i have defined following also

    Code:
    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


    Hint:also in link IcmpSendEcho IP_OPTION_INFORMATION for 64 bit pc is different etc.. etc..

    So please help me to solve the problem

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Check for internet connection gives error in windows 8

    Have you tried the InternetCheckConnection function? Here's an example.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Check for internet connection gives error in windows 8

    Quote Originally Posted by winman View Post
    So i think it is problem with IcmpSendEcho function which returns wrong value
    Well, you're not checking the return value from the Function. I suggest you do something like
    Code:
    Dim lngReturn As Long
    lngReturn =  IcmpSendEcho(hIcmp, lAddress, StringToSend, Len(StringToSend), 0, Reply, Len(Reply), lTimeOut)
    If lngReturn = 0 Then
        MsgBox "Error calling IcmpSendEcho, Return Code " & Err.LastDllError & " Hex: ( " & Hex(Err.LastDllError) & ")"
    Else
        '
        ' Rest of your code
        '
    End If

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    248

    Re: Check for internet connection gives error in windows 8

    Hi Doogle
    When internet connection is not there in Windows XP:
    The return code from IcmpSendEcho is 11003 (IP_DEST_HOST_UNREACHABLE) which means The destination host was unreachable.
    When internet connection is not there in Windows 8:
    The return code from IcmpSendEcho is 1231.
    I don't know what does it mean? Also am i using ICMP_ECHO_REPLY properly?(i saw that for 64 bit there exists ICMP_ECHO_REPLY32 etc etc in link IcmpSendEcho )Can u please help me to solve this?

  5. #5
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Check for internet connection gives error in windows 8

    Error code 1231 is 'ERROR_NETWORK_UNREACHABLE' (See: http://msdn.microsoft.com/en-us/libr...v=vs.85).aspx), which I guess is what you want.

    Presumably if the connection is established, both XP and W8 return a non-zero value from IcmpSendEcho (?)

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    248

    Re: Check for internet connection gives error in windows 8

    The link you have mentioned does not exists. Also i am checking return of ping(ping = Reply.Status) to check for internet connection is not there or not.

  7. #7
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Check for internet connection gives error in windows 8

    OOps - try this and navigate to the appropriate section.

    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    It looks as if you're better off checking the Return from the API call instead. (On the grounds that, if the API call failed, the contents of the Structure you're looking at are probably invalid)

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    248

    Re: Check for internet connection gives error in windows 8

    Ok . But in windows 8 when i internet connection is there or not ping = Reply.Status always returns 0.

    In windows xp ping = Reply.Status returns 0 if internet connection is there and 11003 when internet connection is not there.
    So why does windows 8 always return 0? That is the problem for me because i am checking for reply.status


    I also have doubts with some lines in the link IcmpSendecho
    " On a 64-bit platform, The buffer should be large enough to hold at least one ICMP_ECHO_REPLY32 structure plus RequestSize bytes of data.
    This buffer should also be large enough to also hold 8 more bytes of data (the size of an ICMP error message)."
    What does it mean? Is there any changes needed for my code as windows 8 is 64 bit.
    Please help
    Last edited by winman; May 23rd, 2013 at 02:25 AM.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    248

    Re: Check for internet connection gives error in windows 8

    How i should declare ICMP_ECHO_REPLY32 structure?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width