[RESOLVED] Check if have internet VB6
Hello again. Please, I need you to help me on this one, if you can. I have a button, and what I want to do is that when I press that button, the program check if the machine has any connection to the internet. If so, to display a msgbox saying (it has), and vieversa.
Thanks you
Re: Check if have internet VB6
Code:
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
Dim sConnType As String * 255
Private Sub Command1_Click()
If (CheckInternetConnection = True) Then
MsgBox "We have an active Internet connection!"
Else
MsgBox "We don't have an active Internet connection!"
End If
End Sub
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
Re: Check if have internet VB6
internetconnectedstate will return true if there is a valid LAN connection, regardless of whether there is a WAN connection, so it is not a reliable test
ideally you need to ping your isp server, or download a small file from somewhere, for a more reliable test
Re: Check if have internet VB6
Yes you are right pete... Ok try this (I like your ping idea... I tried it with Yahoo and it worked :))
Code:
Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" _
(ByVal hInet As Long, ByVal lpszUrl As String, ByVal lpszHeaders As String, _
ByVal dwHeadersLength As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet.dll" _
(ByVal hInet As Long) As Long
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _
(ByVal lpszAgent As String, ByVal dwAccessType As Long, ByVal lpszProxyName As _
String, ByVal lpszProxyBypass As String, ByVal dwFlags As Long) As Long
Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Private Const INTERNET_FLAG_RELOAD = &H80000000
Private Const INTERNET_FLAG_KEEP_CONNECTION = &H400000
Private Const INTERNET_FLAG_NO_CACHE_WRITE = &H4000000
Private Sub Command1_Click()
Dim hInet As Long, hUrl As Long, Flags As Long, url As Variant
hInet = InternetOpen(App.Title, INTERNET_OPEN_TYPE_PRECONFIG, _
vbNullString, vbNullString, 0&)
If hInet Then
Flags = INTERNET_FLAG_KEEP_CONNECTION Or INTERNET_FLAG_NO_CACHE_WRITE _
Or INTERNET_FLAG_RELOAD
hUrl = InternetOpenUrl(hInet, "http://www.yahoo.com", _
vbNullString, 0, Flags, 0)
If hUrl Then
MsgBox "Your computer is connected to Internet", _
vbInformation, "Checking connection"
Call InternetCloseHandle(hUrl)
Else
MsgBox "Your computer is not connected to Internet", _
vbInformation, "Checking connection"
End If
End If
Call InternetCloseHandle(hInet)
End Sub
Re: Check if have internet VB6
Well, really thanks you on this one guys. I have tested it with my internet on and it worked, later I changed the ping url to iwegweiugbwi (or something) and it said i wasn't connected.
The only problem is if yahoo server turn inactive... jeje
Thanks you
Re: [RESOLVED] Check if have internet VB6
Quote:
The only problem is if yahoo server turn inactive... jeje
that is why i suggested pinging your isp server, if it is inactive then you probably have no connection anyway
presumably if your internet is disconnected then internetopen will return 0, without actually going to any site