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 🙂
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 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
To use it just call it without any parameters, like below :
... If (CheckInternetConnection = True) MsgBox "We have an active Internet connection!" Else MsgBox "We don't have an active Internet connection!" End If ...
That’s all 🙂