|
-
Nov 28th, 2001, 09:52 AM
#1
Thread Starter
Frenzied Member
is it connected to the internet?
i want to find out if the computer that my program is running,
is connected to the internet!
i tried this code that i found here,
but it shows that i'm always online, which is false,
cuz i unplug my connection
here's the code:
VB Code:
Private Const INTERNET_OPEN_TYPE_DIRECT = 1
Private Const INTERNET_FLAG_NO_CACHE_WRITE = &H4000000
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 Declare Function InternetCloseHandle Lib "wininet.dll" _
(ByVal hEnumHandle As Long) As Long
Public Function InternetConnectOpen() As Boolean
Dim hInternet As Long
InternetConnectOpen = False
hInternet = InternetOpen("Vb FTP Transfer", INTERNET_OPEN_TYPE_DIRECT, _
vbNullString, vbNullString, INTERNET_FLAG_NO_CACHE_WRITE)
If hInternet > 0 Then InternetConnectOpen = True
Call InternetCloseHandle(hInternet)
hInternet = 0
End Function
Private Sub Command1_Click()
If InternetConnectOpen Then
MsgBox "online"
Else
MsgBox "offline"
End If
End Sub
-
Nov 28th, 2001, 09:57 AM
#2
-= B u g S l a y e r =-
this might work:
VB Code:
Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpSFlags As Long, ByVal dwReserved As Long) As Long
If InternetGetConnectedState(0, 0) Then
MsgBox "Online"
Else
MsgBox "Offline"
End If
-
Nov 28th, 2001, 09:59 AM
#3
Thread Starter
Frenzied Member
sorry Peet,
that the one i tried before the other one,
i had found one here once, but i can't find it on my pc
damm!
tx
-
Nov 28th, 2001, 10:03 AM
#4
Frenzied Member
i researched this in DEPTH (because i needed this to work sooo bad)what i came up with was
all functions and coding i could write or find only worked for dialups
they worked fine if you were on a dialup
but failed if you were on high speed (like @home)
so here is thefinal solution ic ame up with to work for both
you have to PING a site using vb
and determine if they are connected based on that ping
*usually you dont want to ping someone elses server cus they will get mad, so you must ping a computer thats yours, and that you know will be online always*
-
Nov 28th, 2001, 10:06 AM
#5
Thread Starter
Frenzied Member
tx Kovan i'll try this,
but i found the code that i had ,
it's work on lan, but when your not connected, it takes a few seconds...
this code was made to put in a module, NO form!!
VB Code:
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 Declare Function InternetOpen _
Lib "wininet.dll" Alias "InternetOpenW" _
(ByVal lpszAgent As Long, _
ByVal dwAccessType As Long, _
ByVal lpszProxyName As Long, _
ByVal lpszProxyBypass As Long, _
ByVal dwFlags As Long) As Long
Private Declare Function InternetOpenUrl _
Lib "wininet.dll" Alias "InternetOpenUrlW" _
(ByVal hInet As Long, _
ByVal lpszUrl As Long, _
ByVal lpszHeaders As Long, _
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
Public Function TestConnection(stSite As String) As Boolean
Dim hInet As Long
Dim hUrl As Long
Dim lFlags As Long
Dim url As Variant
hInet = InternetOpen(StrPtr(App.Title), INTERNET_OPEN_TYPE_PRECONFIG, 0&, 0&, 0&)
If hInet Then
lFlags = INTERNET_FLAG_KEEP_CONNECTION Or _
INTERNET_FLAG_NO_CACHE_WRITE Or _
INTERNET_FLAG_RELOAD
hUrl = InternetOpenUrl(hInet, StrPtr(stSite), 0&, 0, lFlags, 0)
If hUrl Then
TestConnection = True
Call InternetCloseHandle(hUrl)
hUrl = 0
End If
End If
Call InternetCloseHandle(hInet)
hInet = 0
End Function
Sub Main()
If TestConnection("http://www.vbforums.com") Then
MsgBox "online"
Else
MsgBox "offline"
End If
End
End Sub
-
Nov 28th, 2001, 10:08 AM
#6
Frenzied Member
do this
test it with reguarl lan
then unplug your lan connection
and test it agian
it will still return your online
-
Nov 28th, 2001, 10:10 AM
#7
Thread Starter
Frenzied Member
does do it for you,
for me it says that i'm offline,
i tried it, the only thing is that it take a few second when your offline
to detect it!!
-
Nov 28th, 2001, 10:13 AM
#8
Frenzied Member
but your using a site to test the connection with (which in essense your "pinging" that site 
which is what i said i think
hehe
you still need server or a site to test it with..
-
Nov 28th, 2001, 10:17 AM
#9
Thread Starter
Frenzied Member
Originally posted by kovan
you still need server or a site to test it with..
i know, but i can use my company website!!
i think that will do,
BUT does your pinging thing long when you not connected??
cuz you see what i mean with the code that i post,
when your not connected it is long!!
-
Nov 28th, 2001, 10:34 AM
#10
Frenzied Member
yes it does
cus when it pings it *if the site is online* you get a quick response
if the ping is not successful, then it takes longer to return because each ping does 3 tries i blieve
and if the first is successful it returns true
if the 1st fails, goes to 2nd, and 3rd..
but if that site your pinging is down
you will never get the correct results
-
Nov 28th, 2001, 10:36 AM
#11
Thread Starter
Frenzied Member
yeah your right your right for the site down!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|