check if internet is connected or not
add this in module
Code:
Option Explicit
Public Declare Function RasEnumConnections Lib "RasApi32.dll" Alias "RasEnumConnectionsA" (lpRasCon As Any, lpcb As Long, lpcConnections As Long) As Long
Public Declare Function RasGetConnectStatus Lib "RasApi32.dll" Alias "RasGetConnectStatusA" (ByVal hRasCon As Long, lpStatus As Any) As Long
Public Const RAS95_MaxEntryName = 256
Public Const RAS95_MaxDeviceType = 16
Public Const RAS95_MaxDeviceName = 32
Public 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
Public Type RASCONNSTATUS95
dwSize As Long
RasConnState As Long
dwError As Long
szDeviceType(RAS95_MaxDeviceType) As Byte
szDeviceName(RAS95_MaxDeviceName) As Byte
End Type
Public Function IsConnected() As Boolean
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 " & Err & " Has Occured!", vbOKOnly + vbCritical, "Error!"
Exit Function
End If
Tstatus.dwSize = 160
RetVal = RasGetConnectStatus(TRasCon(0).hRasCon, Tstatus)
If Tstatus.RasConnState = &H2000 Then
IsConnected = True
Else
IsConnected = False
End If
End Function
Code:
Private Sub Command2_Click()
Text1.Text = IsConnected
End Sub
Re: check if internet is connected or not
What operating systems does the code work under?
Re: check if internet is connected or not
i have windows xp sp2 not tested on others
Re: check if internet is connected or not
The magic number &H2000 should really be &H2000& since it is a long/Enum. It is RASCS_Connected, which is set to RASCS_DONE, which is a constant &H2000&.
Magic numbers should be avoided.
Worse yet, this code is useless unless you are using Dial-Up Networking (Remote Access Service). And it only checks your first dial connection (you might have several and it ignores the others).
Re: check if internet is connected or not
with respect il like to say it isnt usless like i am on dial up its usefull for me, so there are multi million people out there also will have same connection as me and will use this i tested this and works as i wanted
Re: check if internet is connected or not
Here's one I found somewhere. Don't know if it's useful or not
Code:
Public gWebConnection As Boolean
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
Private Sub Command1_Click()
If (CheckInternetConnection = True) Then
MsgBox "Your connected"
Else
MsgBox "Your not connected"
End If
End Sub