Internet ConnectionStatus and NetworkConnection Type
This code will check for the avaliability of the following network connection
1.Internet connection status
2.Lan connection type
'In a Class module
Public Class CheckInternetConnectionType
'**************************************
' Name: Check internet connection status and LanConnection type
' This code makes API calls to check for an internet conection and
Public Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpdwFlags As Integer, ByVal dwReserved As Integer) As Integer
Public Declare Function IsNetworkAlive Lib "SENSAPI.dll" (ByRef lpdwFlags As Long) As Long
Private Const NETWORK_ALIVE_LAN As Integer = &H1
Private Const NETWORK_ALIVE_WAN As Integer = &H2
'Function to check for the internet connected or not
Public Function IsConnected() As Boolean
'Returns true if there is any internet connection.
IsConnected = InternetGetConnectedState(0, 0)
End Function
'Function to check the lan Connection type
Public Function ConnectionType() As String
Dim ret As Long
If IsNetworkAlive(ret) > 0 Then
'If the Network connection is found then it will return a value a greater than zero
ConnectionType = IIf(ret = NETWORK_ALIVE_LAN, "Connected Via LAN", "Connected Via WAN")
Else
ConnectionType = "Not connected to Lan"
End If
End Function
'Usage
' In a Form add a button and in the click event code this
Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
Private CheckConn As New CheckInternetConnectionType()
'Prints true if connected to Internet ,False when not connected
console.WriteLine(CheckInternetConnectionType.IsConnected)
'Prints Lan or Wan
console.WriteLine(CheckInternetConnectionType.ConnectionType)
End Sub
End Class
