Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting On-Line

Status
Not open for further replies.

gpalmer711

IS-IT--Management
May 11, 2001
2,445
GB
Does any one know how to detect if you are online or not using VB6? This would be Via a Dial up Connection only not via LAN.

Thanks Greg Palmer

----------------------------------------
Any feed back is appreciated.

And remember if it doesn't work just flush it down the toilet. [Flush]
 
Try this. Let us know if it helps


'In a module
Option Compare Database

'For connection details
'Public Declare Function InternetGetConnectedState Lib "wininet" _
(ByRef dwFlags As Long, _
ByVal dwReserved As Long) As Long

'Local system uses a modem to connect to the Internet.
Public Const INTERNET_CONNECTION_MODEM As Long = &H1

'Local system uses a LAN to connect to the Internet.
Public Const INTERNET_CONNECTION_LAN As Long = &H2

'Local system uses a proxy server to connect to the Internet.
Public Const INTERNET_CONNECTION_PROXY As Long = &H4

'No longer used.
Public Const INTERNET_CONNECTION_MODEM_BUSY As Long = &H8

Public Const INTERNET_RAS_INSTALLED As Long = &H10
Public Const INTERNET_CONNECTION_OFFLINE As Long = &H20
Public Const INTERNET_CONNECTION_CONFIGURED As Long = &H40

Public Function IsNetConnectOnline() As Boolean
'no flags needed here - the API returns True
'if there is a connection of any type
IsNetConnectOnline = InternetGetConnectedState(0&, 0&)
End Function


'---------------------------
'on a button or whatever
If IsNetConnectOnline = False Then
MsgBox "Not online"
Else
MsgBox "Online"
End If
 
Thanks, I'll try it when I get home and let you know. Greg Palmer

----------------------------------------
Any feed back is appreciated.

And remember if it doesn't work just flush it down the toilet. [Flush]
 
>If IsNetConnectOnline = False Then
> MsgBox "Not online"
>Else
> MsgBox "Online"
>End If

Since IsNetConnectOnline is boolean, either:

Code:
If Not IsNetConnectOnline Then

Or


Code:
If IsNetConnectOnline Then
        MsgBox "Online"
Else
        MsgBox "Not online"
End If

will work. If <boolean> = False and If <boolean> = True are just performance hits. Not much of a hit in this case, but in a loop they can cost you. (And cost you a job, if you use them in an interview.)
 
-->If <boolean> = False and If <boolean> = True are just performance hits. Not much of a hit in this case, but in a loop they can cost you. (And cost you a job, if you use them in an interview.)

Running a timing test comparing
if (bool) against if (bool = true)
and
if (not bool) against if (bool = false)

Using the QueryPerformanceCounter, I timed how long it took to execute each line 500,000 times. I then ran the test 5000 times and averaged the times.

When running a program thru the IDE, the following chart shows the average amount of time taken to execute each line 500,000 times:
Code:
if (bool)          --  0.126413 seconds
if (bool = true)   --  0.166589
if (not bool)      --  0.138329
if (bool = false)  --  0.166806
So the &quot;cost&quot; of using bool = true cost you .04 seconds after 500K comparisons, and .03 second for the false case, when running program inside the IDE.

However, in a compiled program, the following results were obtained:
Code:
if (bool)          --  0.003905 seconds
if (bool = true)   --  0.003907
if (not bool)      --  0.003903
if (bool = false)  --  0.003902
I conclude there is no difference in a compiled program. Its a reasonable assumption that compiler optimizations remove any differenced. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Thanks CajunCenturion for your input, saves me the time of changing the code. I am however having problems getting it to work as I would like. I have setup a timer to check every second to see if I am connected (I will change the period when I am finished testing). The code always detects that I am connected correctly, however it is not so clever detecting that I have disconnected. On a Lan for example if I pull out the network cable it does not detect that I am no longer connected. I have to go into Network settings and disable the LAN(Win2000). I also have the same problem when connecting Via a modem. When I disconnect it still assumes that I am connected. I have pasted the code that I have used below. Any assistence would be appreciated.

Module Code
----------------------------------
Option Explicit

Public onlinetime As Long

Public Declare Function InternetGetConnectedState Lib &quot;wininet&quot; _
(ByRef dwflags As Long, _
ByVal dwReserved As Long) As Long

'Local system uses a modem to connect to the Internet.
Public Const INTERNET_CONNECTION_MODEM As Long = &H1

'Local system uses a LAN to connect to the Internet.
Public Const INTERNET_CONNECTION_LAN As Long = &H2

'Local system uses a proxy server to connect to the Internet.
Public Const INTERNET_CONNECTION_PROXY As Long = &H4

'No longer used.
Public Const INTERNET_CONNECTION_MODEM_BUSY As Long = &H8

Public Const INTERNET_RAS_INSTALLED As Long = &H10
Public Const INTERNET_CONNECTION_OFFLINE As Long = &H20
Public Const INTERNET_CONNECTION_CONFIGURED As Long = &H40


'InternetGetConnectedState wrapper functions
Public Function IsNetConnectViaLAN() As Boolean

Dim dwflags As Long

'pass an empty variable into which the API will
'return the flags associated with the connection
Call InternetGetConnectedState(dwflags, 0&)

'return True if the flags indicate a LAN connection
IsNetConnectViaLAN = dwflags And INTERNET_CONNECTION_LAN

End Function


Public Function IsNetConnectViaModem() As Boolean

Dim dwflags As Long

'pass an empty variable into which the API will
'return the flags associated with the connection
Call InternetGetConnectedState(dwflags, 0&)

'return True if the flags indicate a modem connection
IsNetConnectViaModem = dwflags And INTERNET_CONNECTION_MODEM

End Function


Public Function IsNetConnectViaProxy() As Boolean

Dim dwflags As Long

'pass an empty variable into which the API will
'return the flags associated with the connection
Call InternetGetConnectedState(dwflags, 0&)

'return True if the flags indicate a proxy connection
IsNetConnectViaProxy = dwflags And INTERNET_CONNECTION_PROXY

End Function


Public Function IsNetConnectOnline() As Boolean

'no flags needed here - the API returns True
'if there is a connection of any type
IsNetConnectOnline = InternetGetConnectedState(0&, 0&)

End Function


Public Function IsNetRASInstalled() As Boolean

Dim dwflags As Long

'pass an empty variable into which the API will
'return the flags associated with the connection
Call InternetGetConnectedState(dwflags, 0&)

'return True if the flags include RAS installed
IsNetRASInstalled = dwflags And INTERNET_RAS_INSTALLED

End Function


Public Function GetNetConnectString() As String

Dim dwflags As Long
Dim msg As String

'build a string for display
If InternetGetConnectedState(dwflags, 0&) Then

If dwflags And INTERNET_CONNECTION_CONFIGURED Then
msg = msg & &quot;You have a network connection configured.&quot; & vbCrLf
End If

If dwflags And INTERNET_CONNECTION_LAN Then
msg = msg & &quot;The local system connects to the Internet via a LAN&quot;
End If

If dwflags And INTERNET_CONNECTION_PROXY Then
msg = msg & &quot;, and uses a proxy server. &quot;
Else: msg = msg & &quot;.&quot;
End If

If dwflags And INTERNET_CONNECTION_MODEM Then
msg = msg & &quot;The local system uses a modem to connect to the Internet. &quot;
End If

If dwflags And INTERNET_CONNECTION_OFFLINE Then
msg = msg & &quot;The connection is currently offline. &quot;
End If

If dwflags And INTERNET_CONNECTION_MODEM_BUSY Then
msg = msg & &quot;The local system's modem is busy with a non-Internet connection. &quot;
End If

If dwflags And INTERNET_RAS_INSTALLED Then
msg = msg & &quot;Remote Access Services are installed on this system.&quot;
End If

Else

msg = &quot;Not connected to the internet now.&quot;

End If

GetNetConnectString = msg

End Function


Form Code
------------------------------------------

Private Sub Form_Load()
onlinetime = 0
End Sub

Private Sub Form_Unload(Cancel As Integer)
MsgBox &quot;You were connected for &quot; & onlinetime & &quot; Seconds. Which Cost £&quot; & txtCost.Text, vbOKOnly, &quot;Online Time&quot;
End Sub

Private Sub tmrCheckOffline_Timer()
If IsNetConnectViaModem = False Then
tmrCheckOnline.Enabled = True
tmrOnlineTime.Enabled = False
tmrCheckOffline.Enabled = False
MsgBox &quot;You have been connected for &quot; & onlinetime & &quot; Seconds. Which Cost £&quot; & txtCost.Text, vbOKOnly, &quot;Online Time&quot;
End If
End Sub

Private Sub tmrCheckOnline_Timer()
If IsNetConnectViaModem = True Then
tmrCheckOffline.Enabled = True
tmrOnlineTime.Enabled = True
tmrCheckOnline.Enabled = False
End If
End Sub

Private Sub tmrOnlineTime_Timer()
onlinetime = onlinetime + 1
txtOnlineTime = onlinetime
'If onlinetime >= 60 Then
txtCost = onlinetime * 0.016666 * 0.02 '/ 60
End Sub
Greg Palmer

----------------------------------------
Any feed back is appreciated.

And remember if it doesn't work just flush it down the toilet. [Flush]
 
>And cost you a job, if you use them in an interview

Which planet does that happen on?
 
Lol &quot;LOL&quot; strongm Greg Palmer

----------------------------------------
Any feed back is appreciated.

And remember if it doesn't work just flush it down the toilet. [Flush]
 
If a potential employer didn't want to hire me because I said if (bool = true) instead of just if (bool), then I would be releived. I wouldn't want to work for that person.
------
Perhaps there problem is with the wrapper functions (although it doesn't appear that you're using them). The functions are designed to return Booleans, and the assignment to function return is the result of a boolean expression. If intrinsic coercion is taking place, then it wouldn't matter, but I'm not a fan of relying on that.
Code:
Public Function IsNetConnectViaLAN() As Boolean

   Dim dwflags As Long

   Call InternetGetConnectedState(dwflags, 0&)

   IsNetConnectViaLAN = ((dwflags And INTERNET_CONNECTION_LAN) = INTERNET_CONNECTION_LAN)
    
End Function

You could also replace all of the wrappers with a single parameter driven wrapper.
Code:
Public Function IsNetConnectVia(ConnectMode as Long) As Boolean

   Dim dwflags As Long
   
   Call InternetGetConnectedState(dwflags, 0&)

   IsNetConnectVia = ((dwflags And ConnectMode) = ConnectMode)
     
End Function

And you can call the function like this:
Code:
If (IsNetConnectVia(INTERNET_CONNECTION_CONFIGURED)) then
   msg = msg & &quot;You have a network connection configured.&quot; & vbCrLf
End If
If (IsNetConnectVia(INTERNET_CONNECTION_LAN)) Then
   msg = msg & &quot;The local system connects to the Internet via a LAN&quot;
End If

In a similar vein, I would apply the same change to the conditionals in the If statement series in the GetNetConnectString Function
Code:
 If ((dwflags And INTERNET_CONNECTION_LAN) = INTERNET_CONNECTION_LAN) Then

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top