What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleSee if the computer is connected to the Internet in Visual Basic 6
DescriptionThis example shows how to see if the computer is connected to the Internet in Visual Basic 6. This example uses the wininet function InternetGetConnectedState to see if it is connected.
KeywordsInternetGetConnectedState, Internet, connected, connection, wininet, Visual Basic 6
CategoriesInternet, Software Engineering
The InternetGetConnectedState function returns 1 if an Internet connection is present or 0 if it is not. The program simply checks the return value and sets a label accordingly.
Private Declare Function InternetGetConnectedState Lib _
    "wininet" (ByRef dwflags As Long, ByVal dwReserved As _
    Long) As Long

Private Sub Form_Load()
    If InternetGetConnectedState(0, 0) = 1 Then
        lblResult.Caption = "Connected"
        lblResult.Caption = "Not Connected"
    End If
End Sub
Joe Sova adds:

This is a complex topic as there are many ways of doing this but none are without side effects.

  • The registry value "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\RemoteAccess" is True if Online but only for modems.
  • InternetGetConnectedState works great if you're not on a Local Area Network (LAN) but if you are on a LAN this will always return True whether you are online or not.
  • A direct ping to a URL is very effective but is very, very slow if there is no connection. Unable to get the "No Connection Timeout" to activate after 5 seconds instead of the default 30-45 seconds!
  • Downloading a webpage is probably the most effective but it is quite awkward.

    Thanks Joe!

    I think the problem is the disconnected philosophy of the Internet. The assumption is that a network link may disappear at any time. It makes the network robust but makes it hard to know whether the network is gone or just slow.

    Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.