Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 158
  • Last Modified:

How can my application detect if i am connected to the internet, and what method i use to connect.(Modem or LAN) ?

Dear experts,Hi.
I need a VB program that is able to detedct if i am connected to the internet or not and by Modem or LAN ?(IMMEDIATELY)
I 've searched lots of sites for that and found lots of codes for that . All of the codes owners have claimed their codes work well. but i 've tested all of them and each has some problems .
for example those ones that have used wininet, if you are not connected to the internet tells  that you are connected to the internet via LAN.
I dont want to use pinging method .

 
0
rosalinda
Asked:
rosalinda
1 Solution
 
mcoopCommented:
how about pinging a known reliable (external) address...
the result is the result
0
 
BelfryCommented:
but why don't you use InetIsOffline?
returns true if offline or not yet made a connect attempt, false if connected.
0
 
rosalindaAuthor Commented:
Dear mcoop
when i am disconnected,pinging causes to appear dialup connection dialogbox and i don't want that.
do you have any solution for this problem?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
rosalindaAuthor Commented:
Dear Belfry ,
Can you give me more details about using InetIsOffline?
How can i use it in my code?Please write for me.
0
 
rosalindaAuthor Commented:
Dear Belfry ,
Can you give me more details about using InetIsOffline?
How can i use it in my code?Please write for me.
0
 
vinnyd79Commented:
0
 
BelfryCommented:
Ok, first declare it

'assume it's declared in a form
Private Declare Function InetIsOffline Lib "url.dll" (ByVal dwFlags As Long) As Long

then use it somewhere, e.g:

Private Sub Form_Load()
dim bIsOnLine as Boolean
bIsOnLine=Not InetIsOffline()
if bIsOnLine then
 'tell the user that he's currently online
else
 'tell the user that he's not yet connected to the internet
end if
End Sub

:)
0
 
BelfryCommented:
oops, sorry, i missed a paramter for InetIsOffline
...
Dim bIsOnLine as Boolean
bIsOnLine=Not InetIsOffline(0)'the parameter must be 0
...
0
 
rosalindaAuthor Commented:
Dear Belfry ,
I don't know why but It always says "you are online".
0
 
FunkyMeisterCommented:
Two functions from my Internet Module (not done with it, why you can't download it yet).

Declares:

Private Declare Function InternetAttemptConnect Lib "wininet.dll" (ByVal dwReserved As Long) As Long
Private Declare Function InternetGetConnectedStateEx Lib "wininet.dll" Alias "InternetGetConnectedStateExA" (ByRef lpdwFlags As Long, ByVal lpszConnectionName As String, ByVal dwNameLen As Long, ByVal dwReserved As Long) As Long
Private Const INTERNET_CONNECTION_MODEM = &H1&
Private Const INTERNET_CONNECTION_LAN = &H2&
Private Const INTERNET_CONNECTION_PROXY = &H4&
Private Const INTERNET_RAS_INSTALLED = &H10&
Private Const INTERNET_CONNECTION_OFFLINE = &H20&
Private Const INTERNET_CONNECTION_CONFIGURED = &H40&


Functions:


Public Function canAttempt() As Boolean
canAttempt = IIf(InternetAttemptConnect(ByVal 0&) = 0, True, False)
End Function

Public Function Connection() As Boolean
   Dim dwFlags As Long
   Dim sNameBuf As String, msg As String
   Dim lPos As Long
   sNameBuf = String$(513, 0)
   If (InternetGetConnectedStateEx(dwFlags, sNameBuf, 512, 0&)) Then
      'Yes, you have active connection
      'Collect ExtraInfo:
      'lPos = InStr(sNameBuf, vbNullChar)
      'If lPos > 0 Then sConnectionName = Left$(sNameBuf, lPos - 1)
      'ConnectionInfo = dwFlags
      'Now ConnectionInfo variable contain OR'ed INTERNET_ flags (see constants above)
      'You can extract info you need with And keyford:
      If (dwFlags And INTERNET_CONNECTION_LAN) Then
       Connection = True
       'MsgBox "Your connection use LAN"
      ElseIf (dwFlags And INTERNET_CONNECTION_MODEM) Then
       Connection = True
       'MsgBox "Your connection use Modem"
      End If
   Else
      Connection = False
      'MsgBox = "Your computer is NOT connected to Internet"
   End If
End Function

Downside of this code, Network (lan) users can always connect.

The Connection() returns whether or not it CAN connect.

The canAttempt() returns whether or not it is able to connect (login to the internet), this is useful for modem users so you can tell IF they've denied dialing.

Best first to see if you CAN connect with the canAttempt() function and if you can, see if you ARE connected with the Connection() function (WILL return true IF a modem user has a default dialup and has enabled dial on demand).

Should be enough to help.
0
 
CleanupPingCommented:
rosalinda:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
0
 
DanRollinsCommented:
rosalinda, an EE Moderator will handle this for you.
Moderator, my recommended disposition is:

    Save as PAQ -- No Refund.

DanRollins -- EE database cleanup volunteer
0
 
PashaModCommented:
Per recommendation

PashaMod
CS Moderator
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now