Microsoft KB Archive/821770

= How to determine the connection state of your local system and how to initiate or end an Internet connection by using Visual Basic .NET or Visual Basic 2005 =

Article ID: 821770

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



IN THIS TASK

 * SUMMARY
 * Step-by-Step Example
 * Verify That It Works
 * REFERENCES



SUMMARY
This step-by-step article describes how to determine the connected state of your local system by using the InternetGetConnectedState function that the Microsoft Windows Internet (WinINet) API provides. This article also describes how to initiate an Internet connection and how to end an Internet connection by using the InternetDial WinINet API function and by using the InternetHangUp WinINet API function.

The WinINet API is a set of functions that enables applications to interact with Gopher, with File Transfer Protocol (FTP), and with Hypertext Transfer Protocol (HTTP) to access many resources on the Internet. The InternetGetConnectedState function returns True or False based on whether a connection exists. After a call to the InternetGetConnectedState function, the lpdwFlags parameter contains a predefined value that provides more information about the connection. For example, the value that IpdwFlags contains may indicate that the connection is a local area network (LAN) connection, a modem connection, or another type of connection. You can infer whether connection exists from the return value of the InternetGetConnectedState function.

When the InternetGetconnectionState function call returns True, a network connection exists. However, this does not guarantee that you have access to data or that you have access to Web sites across the network.

The InternetDial function initiates a connection to the Internet by using a modem. The lpszConnectoid parameter specifies the name of the dial-up connection that the InternetDial function uses. You can use the InternetHangUp WinINet API function to instruct the modem to end the connection that the dwConnection parameter specifies.

back to the top

Step-by-Step Example
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. Under Project types, click Visual Basic Projects.

Note In Visual Studio 2005, click Visual Basic under Project Types. Under Templates, click Windows Application.

By default, Form1 is created. Right-click Form1, and then click View Code.  Add the following declaration statements to the Form1 class: Private Declare Function InternetGetConnectedState Lib &quot;wininet.dll&quot; (ByRef lpdwFlags As Int32, _ ByVal dwReserved As Int32) As Boolean

Private Declare Function InternetDial Lib &quot;Wininet.dll&quot; (ByVal hwndParent As IntPtr, _ ByVal lpszConnectoid As String, ByVal dwFlags As Int32, ByRef lpdwConnection As Int32, _ ByVal dwReserved As Int32) As Int32

Private Declare Function InternetHangUp Lib &quot;Wininet.dll&quot; _ (ByVal lpdwConnection As Int32, ByVal dwReserved As Int32) As Int32

Private Enum Flags As Integer 'Local system uses a LAN to connect to the Internet. INTERNET_CONNECTION_LAN = &H2 'Local system uses a modem to connect to the Internet. INTERNET_CONNECTION_MODEM = &H1 'Local system uses a proxy server to connect to the Internet. INTERNET_CONNECTION_PROXY = &H4 'Local system has RAS installed. INTERNET_RAS_INSTALLED = &H10 End Enum

'Declaration Used For InternetDialUp. Private Enum DialUpOptions As Integer INTERNET_DIAL_UNATTENDED = &H8000 INTERNET_DIAL_SHOW_OFFLINE = &H4000 INTERNET_DIAL_FORCE_PROMPT = &H2000 End Enum

Private Const ERROR_SUCCESS = &H0 Private Const ERROR_INVALID_PARAMETER = &H87

Private mlConnection As Int32  On the View menu, click Designer.</li> Add a Button control to Form1.</li> Right-click Button1, and then click Properties.</li> In the Properties window, modify the Text property of the Button1 control to Detect Connection .</li>  Double-click Detect Connection, and then add the following code in the Button1_Click event handler: Dim lngFlags As Long

If InternetGetConnectedState(lngFlags, 0) Then 'connected. If lngFlags And Flags.INTERNET_CONNECTION_LAN Then 'LAN connection. MsgBox(&quot;LAN connection.&quot;) ElseIf lngFlags And Flags.INTERNET_CONNECTION_MODEM Then 'Modem connection. MsgBox(&quot;Modem connection.&quot;) ElseIf lngFlags And Flags.INTERNET_CONNECTION_PROXY Then 'Proxy connection. MsgBox(&quot;Proxy connection.&quot;) End If Else 'not connected. MsgBox(&quot;Not connected.&quot;) End If </li> Add another Button control to Form1.</li> Right-click Button2, and then click Properties.</li> In the Properties window, change the Text property of the Button2 control to Dial Up .</li>  Double-click Dial Up, and then add the following code in the Button2_Click event handler: Dim DResult As Int32 DResult = InternetDial(Me.Handle, &quot;My Connection&quot;, DialUpOptions.INTERNET_DIAL_FORCE_PROMPT, mlConnection, 0)

If (DResult = ERROR_SUCCESS) Then MessageBox.Show(&quot;Dial Up Successful&quot;, &quot;Dial-Up Connection&quot;) Else MessageBox.Show(&quot;UnSuccessFull Error Code&quot; & DResult, &quot;Dial-Up Connection&quot;) End If Note Replace   with the name of the dial-up connection on your computer. </li> Add another Button control to Form1.</li> Right-click Button3, and then click Properties.</li> In the Properties window, modify the Text property of the Button3 control to Hang Up .</li>  Double-click Hang Up, and then add the following code in the Button3_Click event handler: Dim Result As Int32

If Not (mlConnection = 0) Then Result = InternetHangUp(mlConnection, 0&) If Result = 0 Then MessageBox.Show(&quot;Hang up successful&quot;, &quot;Hang Up Connection&quot;) Else MessageBox.Show(&quot;Hang up NOT successful&quot;, &quot;Hang Up Connection&quot;) End If Else MessageBox.Show(&quot;You must dial a connection first!&quot;, &quot;Hang Up Connection&quot;) End If </li> On the File menu, click Save All to save the project.</li> On the Debug menu, click Start to run the application.</li></ol>

back to the top

Verify That It Works

 * 1) Run the application that you created in the &quot;Step-by-Step Example&quot; section of this article.

Form1 is displayed.
 * 1) Click Detect Connection, and then notice the current connected state of your local computer.
 * 2) Click Dial Up.

The dial-up connection dialog box is displayed.
 * 1) Click Hang Up.

The Internet connection that you established in step 3 ends.

back to the top

<div class="references_section">