Visual Basic Network Services

IsDestinationReachable: Determine Network QOC Info
     
Posted:   Tuesday March 13, 2001
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows 2000
OS restrictions:   Windows XP, Windows 2000, or Windows 95, 98 or NT with IE5 or later
Author:   VBnet - Randy Birch
     

Related:  

InternetGetConnectedState: Determine Network Connection Type
IsNetworkAlive: Determine Network Connection State
IsDestinationReachable: Determine Network QOC Info
gethostbyaddr: Obtain Host Name from IP Address
IcmpSendEcho: Ping a Machine by IP Address
IcmpSendEcho: Ping a Machine by Host Name

IsNetDrive: Determining the Connection State of a Mapped Drive

     
 Prerequisites
Windows XP, Windows 2000, or Windows 95, 98 or NT with IE5 or later.

The IsDestinationReachable API determines if the specified destination can be reached, and provides Quality of Connection (QOC) information for the destination.

IsDestinationReachable would be used by client applications to determine the QOC information before proceeding with network operations. For standalone computers that are directly connected to the network through a network card or remote access server (RAS), this function generates minimal network traffic with RPC calls to the nearest router. For computers that are part of a network where the destination can be reached using RAS or a network gateway, this function pings to the destination to generate accurate QOC information. (This function is only available for TCP/IP connections.)

The destination address is passed as a string, and can be an IP address, a UNC name, or an URL. The lpQOCInfo member contains the QOCINFO structure that receives the Quality of Connection (QOC) information; if the QOC information is not desired, 0& (null) can be passed instead (you will need to amend the declare to change the second parameter to As Any to pass the null).

The call returns True (1) if the destination can be reached using the network facilities currently active on the machine, or false if not.

Note: The demo uses the VB6 FormatNumber function, which is not available in VB4-32 or VB5. Users of these versions should use Format$() instead, i.e. Format$(value, "###,###,###).

 BAS Module Code
None.

 Form Code
To a form add a command button (Command1), a combo (Combo1) and a text box which has been set as the first member of a control array (Text1(0)). Add a set of labels in a label array (Label1(0) - Label1(4) and set the captions as shown (starting at the top with 0). The Load event will load and position the required result textboxes. Add 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 Type QOCINFO
   dwSize As Long
   dwFlags As Long
   dwInSpeed As Long
   dwOutSpeed As Long
End Type

Private Const NETWORK_ALIVE_LAN As Long = &H1 'active LAN cards
Private Const NETWORK_ALIVE_WAN As Long = &H2 'active RAS connections
Private Const NETWORK_ALIVE_AOL As Long = &H4 'connected to AOL (Win95/98 only)
                                              
Private Declare Function IsDestinationReachable Lib "sensapi.dll" _
   Alias "IsDestinationReachableA" _
  (ByVal lpszDestination As String, _
   lpQOCInfo As QOCINFO) As Long


Private Sub Form_Load()
  
   Dim c As Integer
   Dim TotalRequired As Integer
   
   TotalRequired = 4
   
  'create an array of textboxes. The
  'array is 0-based, so the first control is
  'index 0, and the last is TotalRequired -1
   For c = 0 To TotalRequired - 1
    
      'since array element(0) already exists,
      'don't try to load it
       If c > 0 Then Load Text1(c)
       
      'position the newly-created control
      'and its label, if present
       Text1(c).Move 1600, 600 + (c * 300), 3615
       Label1(c).Move 200, 300 + (c * 300)
       
      'clear and show
       Text1(c).Text = ""
       Text1(c).Visible = True
       
    Next
    
   'oops - one more label! 
    Label1(4).Move 200, 300 + (4 * 300)
    
   
   With Combo1
      .Clear
      .Move 1600, 260, 3615
      .AddItem "209.68.48.119"  'mvps IP
      .AddItem "www.mvps.org"   'mvps dns
      .AddItem "64.58.76.177"   'yahoo IP
      .AddItem "www.yahoo.com"  'yahoo dns
      .AddItem "192.168.0.1"    'local router
      .AddItem "192.168.0.100"  'machine on lan
      .AddItem "www.gov.on.ca"  'gov of Ontario internet
   End With
   
   With Command1
      .Caption = "Is Destination Reachable?"
      .Move 2880, 2040, 2415, 375
      .Default = True
   End With
   
   Me.Move Me.Left, Me.Top, 5640, 2985
   Me.Caption = "VBnet Network Destination Reachable Test"   
      
End Sub


Private Sub Combo1_Click()

   Text1(0).Text = ""
   Text1(1).Text = ""
   Text1(2).Text = ""
   Text1(3).Text = ""
   
End Sub


Private Sub Command1_Click()

  'Notes: - This function is only available for
  '         TCP/IP connections.
  '       - You can supply a NULL pointer (0&) if
  '         the QOC information is not required (and
  '         change the declare's parameter to As Any).

   Dim result As Long
   Dim tmp As String
   Dim qoc As QOCINFO

   With qoc
      .dwSize = Len(qoc)
   End With
   
   result = IsDestinationReachable((Combo1.Text), qoc)
   
   With qoc
      Select Case .dwFlags
         Case NETWORK_ALIVE_LAN: tmp = "computer has one or more active LAN cards"
         Case NETWORK_ALIVE_WAN: tmp = "computer has one or more active RAS connections"
         Case NETWORK_ALIVE_AOL: tmp = "computer is connected to the America Online network"
         Case Else
      End Select
      
      Text1(0).Text = Format$(result, "Yes/No")
      Text1(1).Text = FormatNumber(.dwInSpeed, 0) & " bytes per second"
      Text1(2).Text = FormatNumber(.dwOutSpeed, 0) & " bytes per second"
      Text1(3).Text = tmp
      
   End With
   
End Sub
 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