CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Checking Internet Connection

  1. #1
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,883

    Checking Internet Connection

    I have the following code in my system and use it to check if the Internet is connected.

    1) I load a form and run the CheckInternetConnection function with correct results - Internet is Connected or it is Not (Depending on the Network Cable being plugged into my computer or not.

    2) For testing purposes I start with the cable plugged in, but before proceeding to the next step (to transfer data), I pull out the cable.

    The system continues as though the Internet is connected EVEN THOUGH I RE-ISSUE the CheckInternetConnection immediately.

    HOWEVER - If I step through the CheckInternetConnection function in debug mode it responds correctly

    My question then is -


    "How can I check that an internet connection is present as this routine seems only to work in debug mode" ?

    Thanks


    Code:
    Public Declare Function InternetGetConnectedStateEx Lib "wininet.dll" (ByRef lpdwFlags As Long, ByVal lpszConnectionName As String, ByVal dwNameLen As Integer, ByVal dwReserved As Long) As Long
    
    
    
    Public Function CheckInternetConnection() As Boolean
        Dim aux As String * 255
        Dim R As Long
        R = InternetGetConnectedStateEx(R, aux, 254, 0)
        If R = 1 Then
            CheckInternetConnection = True
        Else
            CheckInternetConnection = False
        End If
    End Function
    
    
    If (CheckInternetConnection = False) Then
            'MsgBox "Internet Connection FAILED"
            MsgBox "Internet Connection DOES NOT Exist - Try Sending again after making Internet Connection", vbCritical, "Internet Connection Error"
            Unload Me
            Exit Sub
        End If
    Last edited by George1111; May 18th, 2011 at 06:50 AM.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Checking Internet Connection

    Use PING, and trap the results. Check to see if a MAJOR website is up, and use that...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,268

    Re: Checking Internet Connection

    George, I'm using this code :

    Code:
    Private Declare Function InternetGetConnectedState Lib "wininet" (ByRef dwflags As Long, _
    ByVal dwReserved As Long) As Long
    
    Private Const CONNECT_LAN As Long = &H2
    Private Const CONNECT_MODEM As Long = &H1
    Private Const CONNECT_PROXY As Long = &H4
    Private Const CONNECT_OFFLINE As Long = &H20
    Private Const CONNECT_CONFIGURED As Long = &H40
    
    Public Function IsWebConnected(Optional ByRef ConnType As String) As Boolean
    
    Dim dwflags As Long
    Dim WebTest As Boolean
    ConnType = ""
    WebTest = InternetGetConnectedState(dwflags, 0&)
    Select Case WebTest
    Case dwflags And CONNECT_LAN: ConnType = "LAN"
    Case dwflags And CONNECT_MODEM: ConnType = "Modem"
    Case dwflags And CONNECT_PROXY: ConnType = "Proxy"
    Case dwflags And CONNECT_OFFLINE: ConnType = "Offline"
    Case dwflags And CONNECT_CONFIGURED: ConnType = "Configured"
    End Select
    IsWebConnected = WebTest
    End Function
    
    Private Sub Command1_Click()
    
    Dim msg As String
    If IsWebConnected(msg) Then
    msg = "You are connected to the Internet via: " & msg
    Else
    msg = "You are not connected to the Internet."
    End If
    MsgBox msg, vbOKOnly, "Internet Connection Status"
    End Sub
    Found it here :

    http://www.go4expert.com/forums/showthread.php?t=2557

    And it tells me immediately when I plug out my cable, that it is not connected, when I plug it back it in, it tells me it is connected

    I hope it helps!
    [SIGPIC][/SIGPIC]
    All my Articles
    Hannes

  4. #4
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,883

    Unhappy Re: Checking Internet Connection

    Worked perfectly the first time I tried pulling out the cable

    Second time I pulled out the cable, it told me I was srtill connected via LAN

    ???
    Cheers Hans


    How do I programatically test PING

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Checking Internet Connection

    Two ways:
    Code:
    Option Explicit
    
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    
    Private Const SW_SHOWNORMAL As Long = 1
    Private Const SW_HIDE As Long = 0
    
    Private Sub Command1_Click()
        ShellExecute Me.hwnd, "Open", "C:\Windows\System32\CMD.exe", " /c ipconfig >> c:\Myip.txt", "c:\", SW_SHOWNORMAL
        Shell "notepad c:\Myip.txt", SW_SHOWNORMAL
    End Sub
    Although the ping code is PING xx.xx.xx.xx where x.x.x.x is the IP address or DOMAIN NAME that you want to test.

    Try it both ways (connected and not) and see the file sizes of both. If it's bigger than disconnected value (in LENGTH) it should be ONLNINE.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,268

    Re: Checking Internet Connection

    Quote Originally Posted by George1111 View Post
    Worked perfectly the first time I tried pulling out the cable

    Second time I pulled out the cable, it told me I was srtill connected via LAN

    ???
    Cheers Hans
    Hi George. That is bad news So sorry, I really hoped it would be your solution. I didn't test it much.

    Try the PING solution from David, that hsould work
    [SIGPIC][/SIGPIC]
    All my Articles
    Hannes

  7. #7
    Join Date
    May 2011
    Posts
    5

    Re: Checking Internet Connection

    Is there any other way except InternetGetConnectedState() and pinging? I can't use InternetGetConnectedState since its failing on some tests and ping takes too long to finish. Is there any other API that could get the state of your internet connection in just 1-2 seconds?

    PinoyAko
    Last edited by PinoyAko; May 30th, 2011 at 02:26 PM.

  8. #8
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,345

    Re: Checking Internet Connection

    In my programs I attempt to connect to the desired pc as usual. This will fail right away if there is no network. Once the connection is made detecting the network is a bit harder. My solution was to send a sinlge byte of data and get a response from the server. If I get the response I know the connection is live and it happens very quickly if I get an error or timeout condition then I know the connection has failed either due to network issues or server program issue.

    All in all I think it works pretty well.
    Always use [code][/code] tags when posting code.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)