Home > How-To Library > Internet
How to Monitor the connection to the internet
**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * **************************************************************** 'Full code for monitoring connection to the internet. Includes function to be 'called by a timer which returns true or false depending on the current 'connection state. Const RAS95_MaxEntryName = 256 Const RAS95_MaxDeviceType = 16 Const RAS95_MaxDeviceName = 32 Private Type RASCONN95 dwSize As Long hRasCon As Long szEntryName(RAS95_MaxEntryName) As Byte szDeviceType(RAS95_MaxDeviceType) As Byte szDeviceName(RAS95_MaxDeviceName) As Byte End Type Private Type RASCONNSTATUS95 dwSize As Long RasConnState As Long dwError As Long szDeviceType(RAS95_MaxDeviceType) As Byte szDeviceName(RAS95_MaxDeviceName) As Byte End Type Private Declare Function RasEnumConnections Lib "RasApi32.dll" Alias "RasEnumConnectionsA" _ (lpRasCon As Any, lpcb As Long, lpcConnections As Long) As Long Private Declare Function RasGetConnectStatus Lib "RasApi32.dll" Alias "RasGetConnectStatusA" _ (ByVal hRasCon As Long, lpStatus As Any) As Long 'This function checks for a connection to the internet and returns true or false 'as to whether one is present Private Function CheckInternetConnection() As Boolean On Error GoTo ErrHandler Dim TRasCon(255) As RASCONN95 Dim lg As Long Dim lpcon As Long Dim RetVal As Long Dim Tstatus As RASCONNSTATUS95 TRasCon(0).dwSize = 412 lg = 256 * TRasCon(0).dwSize RetVal = RasEnumConnections(TRasCon(0), lg, lpcon) If RetVal <> 0 Then ' MsgBox "ERROR" Exit Function End If Tstatus.dwSize = 160 RetVal = RasGetConnectStatus(TRasCon(0).hRasCon, Tstatus) If Tstatus.RasConnState = &H2000 Then CheckInternetConnection = True Else CheckInternetConnection = False End If ExitCheckConnection: Exit Function ErrHandler: CheckInternetConnection = False Resume ExitCheckConnection End Function 'Set the timer interval to however many seconds you would like between checks * 1000 Private Sub Timer1_Timer() If CheckInternetConnection = True Then 'Do whatever you need to do when connected Else 'Do whatever you need to do when disconnected End If End Sub
If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.