Visual Basic Internet Routines

InternetCheckConnection: Another Way to Check for Internet Connection
     
Posted:   Monday November 17, 2008
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows Vista
OS restrictions:   None
Author:   VBnet - Randy Birch, MSDN
     

Related:  

Netbios: Determine Local Adapter MAC Address
SendARP: Determine Local or Remote Adapter MAC Addresses
gethostbyname: Determine Network Host Name and IP Address
Netbios: Determine Local Adapter MAC Addresses through LANA Enumeration
gethostbyaddr: Obtain Host Name from IP Address
IcmpSendEcho: Ping a Machine by IP Address
IcmpSendEcho: Ping a Machine by Host Name

URLDownloadToFile: Obtain Machine's Public IP Behind Router
IcmpSendEcho: Perform a Tracert (Trace Route) in VB with Host Name Resolution
     
 Prerequisites
Network or DUN connection.

This code can be used to check whether a machine as an active connection to the internet and it returns instantaneously so there is no timeout issue as with other methods that access the internet.  It can not be used to determine whether there is an active connection to a LAN network in order to allow subsequent code to access a remote computer or server. 

InternetCheckConnection attempts to open a socket connection to the specified URL in the following order:

  • If lpszUrl is non-NULL, the host value is extracted from it and used to ping that specific host.
  • If lpszUrl is NULL and there is an entry in the internal server database for the nearest server, the host value is extracted from the entry and used to ping that server.

MS claims the only valid constant for the call's dwflags parameter is FLAG_ICC_FORCE_CONNECTION.

You can use Err.LastDllError if a return value other than 1 (connected) or 0 (failed) is returned; the underlying call to GetLastError will indicate if a connection cannot be made or if the sockets database is unconditionally offline.

 BAS Module Code
None.

 Form Code
To a form add a command button (Command1) together with the following code:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const FLAG_ICC_FORCE_CONNECTION = &H1

Private Declare Function InternetCheckConnection Lib "wininet.dll" _
   Alias "InternetCheckConnectionA" _
  (ByVal lpszUrl As String, _
   ByVal dwFlags As Long, _
   ByVal dwReserved As Long) As Long


Private Sub Form_Load()

   Command1.Caption = "Test Connection"
   
End Sub


Private Sub Command1_Click()
   
   Dim sUrl As String
   
   sUrl = "http://www.yahoo.com/"
   
   If CheckInetConnection(sUrl) Then
   
      MsgBox "Connection to " & sUrl & " can be made.", vbInformation
      
   Else
   
      MsgBox "Connection to " & sUrl & " is not possible at this time.", vbExclamation
   
   End If
    
End Sub


Private Function CheckInetConnection(sUrlOfInterest As String) As Boolean

   CheckInetConnection = InternetCheckConnection(sUrlOfInterest, FLAG_ICC_FORCE_CONNECTION, 0&)
   
End Function
 Comments

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter