Skip to content


Check if there is an active Internet connection

This is my first real post on “Visual Basic 6 Tips & Tricks” category :)

In many cases you need to check if the computer on which your VB6 application runs has an active Internet connection ( example : you have a small VB6 application that connects to a web server to retrieve the current weather ). To check this we need to use the InternetGetConnectedStateEx API function which will return 0 ( false ) if there is no connection or 1 ( true ) if there is an Internet connection.
The function itself is easy enough but if you have any questions feel free to ask :)

Visual Basic [Show Plain Code]:
  1. Private Declare Function InternetGetConnectedStateEx Lib "wininet.dll" (ByRef lpdwFlags As Long, ByVal lpszConnectionName As String, ByVal dwNameLen As Integer, ByVal dwReserved As Long) As Long
  2.  
  3. Public Function CheckInternetConnection() As Boolean
  4.     Dim aux As String * 255
  5.     Dim r As Long
  6.     r = InternetGetConnectedStateEx(r, aux, 254, 0)
  7.     If r = 1 Then
  8.         CheckInternetConnection = True
  9.     Else
  10.         CheckInternetConnection = False
  11.     End If
  12. End Function

To use it just call it without any parameters, like below :

Visual Basic [Show Plain Code]:
  1. If (CheckInternetConnection = True)
  2.     MsgBox "We have an active Internet connection!"
  3. Else
  4.     MsgBox "We don’t have an active Internet connection!"
  5. End If

That’s all :)

Posted in Visual Basic 6 Tips & Tricks.


4 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Nick Jones says

    Sorin=hello-I have sent you a few emails to your hotmail account-did you receive them ? Nick

  2. Kaustav Das Modak says

    everything’s working fine but when it shows connected even when the computer is disconnected from Internet !!!

  3. tF says

    This code will always return true when we’re on LAN and connected internet to modem, whether connection active or not.
    Thx.

  4. Boyd Prestwood says

    The Internet Connection Test works great for me. Using VB6, how can I connect to a time server in order to automatically set my system clock. Is there any source code available to illustrate this? It would be so appreciated. I am a hobbyist with intermediate programming skills in VB6, but know nothing about .NET. I have several projects that I used to teach myself VB6 and want to expand them. Thanks. (Boyd Prestwood)



Some HTML is OK

or, reply to this post via trackback.


Get Adobe Flash playerPlugin by wpburn.com wordpress themes