|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Visual Basic Network Services InternetGetConnectedState: Determine Network Connection Type |
|
Posted: | Thursday June 10, 1999 |
Updated: | Monday December 26, 2011 |
Applies to: | VB4-32, VB5, VB6 |
Developed with: | VB6, Windows NT4 |
OS restrictions: | None |
Author: | Paul Marshal, msnews |
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. |
|
The InternetGetConnectedState API declared in the IE4 / IE5 wininet.dll retrieves the connected state of the local system. The API is simple to use, and returns TRUE if there is an Internet
connection, or FALSE otherwise.
The heart of the call is its dwFlags parameter. On returning from the call, dwFlags contains the current connection state. By And'ing these with the connection info desired, the system's current connection type and state are determined. The first illustration shows the results of five wrapper routines locating specific connection information while connected via a LAN (actually via a cable modem). The second illustration shows the values returned when that connection is broken. Note that the IsNetRASInstalled function will return true even without a connection; is simply reports the capability. The last text box in both illustrations show the return from the informational wrapper GetNetConnectString(). The code here is adapted from a demo posted to msnews by Paul Marshall. |
BAS Module Code |
None. |
|
Form Code |
To a form add a command button (Command1), and six text boxes (Text1-Text6). Set the Text6 Multiline property to True. 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 Declare Function InternetGetConnectedState Lib "wininet" _ (ByRef dwFlags As Long, _ ByVal dwReserved As Long) As Long 'Local system uses a modem to connect to the Internet. Private Const INTERNET_CONNECTION_MODEM As Long = &H1 'Local system uses a LAN to connect to the Internet. Private Const INTERNET_CONNECTION_LAN As Long = &H2 'Local system uses a proxy server to connect to the Internet. Private Const INTERNET_CONNECTION_PROXY As Long = &H4 'No longer used. Private Const INTERNET_CONNECTION_MODEM_BUSY As Long = &H8 Private Const INTERNET_RAS_INSTALLED As Long = &H10 Private Const INTERNET_CONNECTION_OFFLINE As Long = &H20 Private Const INTERNET_CONNECTION_CONFIGURED As Long = &H40 Private Sub Command1_Click() Text1.Text = IsNetConnectViaLAN() Text2.Text = IsNetConnectViaModem() Text3.Text = IsNetConnectViaProxy() Text4.Text = IsNetConnectOnline() Text5.Text = IsNetRASInstalled() Text6.Text = GetNetConnectString() End Sub Private Function IsNetConnectViaLAN() As Boolean Dim dwflags As Long 'pass an empty variable into which the API will 'return the flags associated with the connection Call InternetGetConnectedState(dwflags, 0&) 'return True if the flags indicate a LAN connection IsNetConnectViaLAN = dwflags And INTERNET_CONNECTION_LAN End Function Private Function IsNetConnectViaModem() As Boolean Dim dwflags As Long 'pass an empty variable into which the API will 'return the flags associated with the connection Call InternetGetConnectedState(dwflags, 0&) 'return True if the flags indicate a modem connection IsNetConnectViaModem = dwflags And INTERNET_CONNECTION_MODEM End Function Private Function IsNetConnectViaProxy() As Boolean Dim dwflags As Long 'pass an empty variable into which the API will 'return the flags associated with the connection Call InternetGetConnectedState(dwflags, 0&) 'return True if the flags indicate a proxy connection IsNetConnectViaProxy = dwflags And INTERNET_CONNECTION_PROXY End Function Private Function IsNetConnectOnline() As Boolean 'no flags needed here - the API returns True 'if there is a connection of any type IsNetConnectOnline = InternetGetConnectedState(0&, 0&) End Function Private Function IsNetRASInstalled() As Boolean Dim dwflags As Long 'pass an empty variable into which the API will 'return the flags associated with the connection Call InternetGetConnectedState(dwflags, 0&) 'return True if the flags include RAS installed IsNetRASInstalled = dwflags And INTERNET_RAS_INSTALLED End Function Private Function GetNetConnectString() As String Dim dwflags As Long Dim msg As String 'build a string for display If InternetGetConnectedState(dwflags, 0&) Then If dwflags And INTERNET_CONNECTION_CONFIGURED Then msg = msg & "You have a network connection configured." & vbCrLf End If If dwflags And INTERNET_CONNECTION_LAN Then msg = msg & "The local system connects to the Internet via a LAN" End If If dwflags And INTERNET_CONNECTION_PROXY Then msg = msg & ", and uses a proxy server. " Else msg = msg & "." End If If dwflags And INTERNET_CONNECTION_MODEM Then msg = msg & "The local system uses a modem to connect to the Internet. " End If If dwflags And INTERNET_CONNECTION_OFFLINE Then msg = msg & "The connection is currently offline. " End If If dwflags And INTERNET_CONNECTION_MODEM_BUSY Then msg = msg & "The local system's modem is busy with a non-Internet connection. " End If If dwflags And INTERNET_RAS_INSTALLED Then msg = msg & "Remote Access Services are installed on this system." End If Else msg = "Not connected to the internet now." End If GetNetConnectString = msg End Function |
Comments |
Save the project and run. Depending on your connections state and installed options, the appropriate messages and return values will be displayed. |
|
|
|
|||||
|
|||||
|
|||||
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |