Checking Internet Connection in VB.NET

The Microsoft WinINet API provides a means for applications to interact with common Internet protocols, including FTP and HTTP. It offers a set of functions that facilitate network communication and data transfer.

In certain scenarios, it becomes necessary to determine if a computer has an active Internet connection before attempting to establish communication through a specific interface. This can be achieved using one of the WinINet functions designed for checking the status of an Internet connection on a computer.

InternetGetConnectedState

One such function is the InternetGetConnectedState function. When called, this function retrieves the connected state of the local system. By examining its return value, we can ascertain if at least one connection to the Internet is available. If the function returns TRUE, it signifies that the computer is connected to the Internet.

Private Declare Function InternetGetConnectedState Lib "wininet" _ (ByRef conn As Long, ByVal val As Long) As Boolean

Utilizing the InternetGetConnectedState function allows applications to programmatically determine the availability of an Internet connection before proceeding with any network-related operations. This information can be crucial in avoiding unnecessary communication attempts and providing a more seamless user experience.

Full Source VB.NET
Imports System.Runtime.InteropServices Public Class Form1 Private Declare Function InternetGetConnectedState Lib "wininet" (ByRef conn As Long, ByVal val As Long) As Boolean Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Out As Integer If InternetGetConnectedState(Out, 0) = True Then MsgBox("Connected !") Else MsgBox("Not Connected !") End If End Sub End Class