CodeItBetter Programming Another VB Programming Blog

How to Monitor the connection to the internet

Posted on August 7, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'Internet - How to Monitor the connection to the internet
'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
Filed under: Internet Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.