Test User's Internet Connection in VB

Discussion in 'Visual Basic [VB]' started by pradeep, Jan 15, 2007.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    As more and more of your applications become distributed across the Internet, you'll no doubt want to build in a way to determine if the current user is actually connected to the Web. Fortunately, the Windows API offers a quick and easy way to do so with the InternetGetConnectedState() function.

    This function uses the declaration syntax seen here:
    Code:
      Private Declare Function InternetGetConnectedState Lib "wininet" (ByRef dwflags As Long, _
      ByVal dwReserved As Long) As Long
    The function returns 1 if a connection exists and 0 if not. You can easily convert these values to their Boolean equivalents in VB. After the test, the dwflags parameter will indicate what type of connection the user has. You use bitwise comparisons to test for specific values. The dwflags constants are as follows:

    Code:
      Private Const CONNECT_LAN As Long = &H2
      Private Const CONNECT_MODEM As Long = &H1
      Private Const CONNECT_PROXY As Long = &H4
      Private Const CONNECT_OFFLINE As Long = &H20
      Private Const CONNECT_CONFIGURED As Long = &H40
    You can ignore the dwReserved parameter.

    To see how this function works, launch a new VB project, and drop a Command Button onto the default form. Right-click on the form and select View Code from the shortcut menu. When the IDE opens the Code window, enter the InternetGetConnectedState() function and constant declarations as shown above. Then, enter the following procedures:

    Code:
    Public Function IsWebConnected(Optional ByRef ConnType As String) As Boolean
          Dim dwflags As Long
          Dim WebTest As Boolean
          ConnType = ""
          WebTest = InternetGetConnectedState(dwflags, 0&)
          Select Case WebTest
              Case dwflags And CONNECT_LAN: ConnType = "LAN"
              Case dwflags And CONNECT_MODEM: ConnType = "Modem"
              Case dwflags And CONNECT_PROXY: ConnType = "Proxy"
              Case dwflags And CONNECT_OFFLINE: ConnType = "Offline"
              Case dwflags And CONNECT_CONFIGURED: ConnType = "Configured"
              Case dwflags And CONNECT_RAS: ConnType = "Remote"
          End Select
          IsWebConnected = WebTest
      End Function
      Private Sub Command1_Click()
          Dim msg As String
          If IsWebConnected(msg) Then
              msg = "You are connected to the Internet via: " & msg
          Else
              msg = "You are not connected to the Internet."
          End If
          
          MsgBox msg, vbOKOnly, "Internet Connection Status"
      End Sub
    Run this program and click the form's command button, the message box tells you if you're connected to the Internet and by what type of connection.
     

    Attached Files:

    shabbir likes this.
  2. su_jay300

    su_jay300 New Member

    Joined:
    Mar 8, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    dear friends

    this code is realy great i thanks to mr pradeep lots.

    sujay kumar
     
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Welcome sujay!

    Happy programming!
     
  4. jodarox

    jodarox New Member

    Joined:
    Nov 1, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Dear Sir,
    This code is great but can I make this connection for example to get data from our server for instance? thanks more power and regards.
     
  5. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    For that purpose you'll need to use Winsock!
     
  6. jodarox

    jodarox New Member

    Joined:
    Nov 1, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    winsock I never used that control yet can you make a simple code for me or how can i access that control and how can i use it? thanks for your reply and regards.
     
  7. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
  8. sreeja

    sreeja New Member

    Joined:
    Nov 13, 2007
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    An easy way is with Internet Get Connected State() function.As more and more of your applications become distributed across the Internet, you'll no doubt want to build in a way to determine if the current user is actually connected to the Web. Fortunately, the Windows API offers a quick and easy way to do so with the InternetGetConnectedState() function.
     
  9. baluviswanath

    baluviswanath New Member

    Joined:
    Sep 3, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi Pradeep,

    I tried your script, but would you be able to help me out with a VBS for testing the Internet access to a specifc URL using a proxy ? Like the http.open method along with response and setproxy functions.....

    Thanks....
     
  10. justezekiel

    justezekiel New Member

    Joined:
    Oct 11, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello,

    I'm kinda new in VB6.0 and I'm trying to make a program that can indicate whether a computer is connected to a LAN or not at all times. Can anyone help? Thanks!!!
     
  11. skp819

    skp819 New Member

    Joined:
    Dec 8, 2008
    Messages:
    89
    Likes Received:
    3
    Trophy Points:
    0
    Thats a really great. Thanks a lot for that...............
     
  12. phone00x

    phone00x New Member

    Joined:
    Jan 8, 2009
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    dear friend's
    i'am new programmer invisual basic. I like vb programming. i Think it's easy to connecting for Internet.
     
  13. asoft

    asoft New Member

    Joined:
    Feb 20, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello,

    Its Very Very Nice Code.

    Thanks.
     
  14. kaamiljj

    kaamiljj New Member

    Joined:
    Oct 16, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    thanks, that's what I was looking for.
     
  15. nimesh

    nimesh New Member

    Joined:
    Apr 13, 2009
    Messages:
    769
    Likes Received:
    20
    Trophy Points:
    0
    Occupation:
    Oracle Apps Admin
    Location:
    Mumbai
    Home Page:
    http://techiethakkar.blogspot.com
    nice article
    :2thumbsup
     
  16. aliciablock

    aliciablock New Member

    Joined:
    Feb 20, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    :(I have follow your instruction to run the program, but the vb 2005 complaint variable 'msg' is passed by reference before it has been assigned a value. a null reference exception could result at runtime, which this line of code
    If IsWebConnected(msg) Then
     
  17. krunall

    krunall New Member

    Joined:
    Mar 12, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    how to run this program??
     
  18. ihappentobe

    ihappentobe New Member

    Joined:
    Dec 3, 2013
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Helpfull, but:

    1) CONNECT_RAS is not declared
    2) How do you expect to decompose WebTest ("select case WebTest") if WebTest is boolean (only True or False)?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice