|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Visual Basic Network Services IsNetworkAlive: Determine Network Connection State |
||
Posted: | Friday April 20, 2001 | |
Updated: | Monday December 26, 2011 | |
Applies to: | VB4-32, VB5, VB6 | |
Developed with: | VB6, Windows 2000 | |
OS restrictions: | None | |
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 |
Network or DUN connection, IE4 or greater. |
|
IsNetworkAlive
determines whether the local system is connected to a network and the type of network connection available when the return value is True. On
NT/2000 the valid connections returned are LAN or WAN, while on Win9x a third option is available indicating the connection is to AOL.
This demo shows five wrapper functions for IsNetworkAlive; three returning True or False if the type of connection being tested for is active, one that returns True if any connection is available, and one returning a descriptive string. IsNetworkAlive can be used by applications to determine whether there is network connectivity before proceeding with network operations. Applications such as directory service applications, e-mail clients, or Internet browsers can adapt to various types of network connectivity. For example, a printing operation can be deferred until the network connection is available. IsNetworkAlive is only available for TCP/IP connections. This function is available on Windows XP, 2000 (or Windows NT 4.0 with Internet Explorer 5 or later), and on Windows 95 or later with Internet Explorer 5 or later. |
BAS Module Code |
None: |
|
Form Code |
To a form add a command button (Command1), and five text boxes (Text1-Text6). Label as desired, and add the following form 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 NETWORK_ALIVE_LAN = &H1 'net card connection Private Const NETWORK_ALIVE_WAN = &H2 'RAS connection Private Const NETWORK_ALIVE_AOL = &H4 'AOL Private Declare Function IsNetworkAlive Lib "Sensapi" _ (lpdwFlags As Long) As Long Private Sub Command1_Click() Text1.Text = IsNetConnectionLAN() Text2.Text = IsNetConnectionRAS() Text3.Text = IsNetConnectionAOL() Text4.Text = IsNetConnectionAlive() Text5.Text = GetNetConnectionType() End Sub Private Function IsNetConnectionAlive() As Boolean Dim tmp As Long IsNetConnectionAlive = IsNetworkAlive(tmp) = 1 End Function Private Function IsNetConnectionLAN() As Boolean Dim tmp As Long If IsNetworkAlive(tmp) = 1 Then IsNetConnectionLAN = tmp = NETWORK_ALIVE_LAN End If End Function Private Function IsNetConnectionRAS() As Boolean Dim tmp As Long If IsNetworkAlive(tmp) = 1 Then IsNetConnectionRAS = tmp = NETWORK_ALIVE_WAN End If End Function Private Function IsNetConnectionAOL() As Boolean Dim tmp As Long If IsNetworkAlive(tmp) = 1 Then IsNetConnectionAOL = tmp = NETWORK_ALIVE_AOL End If End Function Private Function GetNetConnectionType() As String Dim tmp As Long If IsNetworkAlive(tmp) = 1 Then Select Case tmp Case NETWORK_ALIVE_LAN: GetNetConnectionType = _ "The system has one or more active LAN cards" Case NETWORK_ALIVE_WAN: GetNetConnectionType = _ "The system has one or more active RAS connections" Case NETWORK_ALIVE_AOL: GetNetConnectionType = _ "The system is connected to America Online" Case Else End Select Else GetNetConnectionType = _ "The system has no connection or an error occurred" End If End Function |
Comments |
Save the project and run. Depending on your current connection state and installed options, the appropriate messages and return values will be displayed. |
|
|
|
|||||
|
|||||
|
|||||
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |