Share via


Terminating a Connection

Other versions of this page are also available for the following:

Windows Mobile Not SupportedWindows Embedded CE Supported

8/28/2008

When a RAS application starts a connection operation, the RasDial function receives a RASCONN connection handle to identify the connection. If the returned handle is not NULL, the client must eventually call the RasHangUp function to end the connection. If an error occurs during the connection operation, the client must call the RasHangUp function even though the connection was never established.

A client application might require a connection to end even though it does not have the handle returned by RasDial. For example, the application that called RasDial might have exited when the connection was successfully established. In this case, the disconnecting application can use RasEnumConnections to get all the current connections. For each connection, RasEnumConnections returns a RASCONN structure containing the RASCONN connection handle and the phone-book entry name or phone number specified when the connection operation was started. This data can be used to display a list of connections from which a user can select the connection to end.

If the application exits after calling RasHangUp, there may not be enough time for the modem to reset. To provide the modem time to reset, pause for a few seconds while the application exits.

Before an application terminates one or more connections, it is useful to enumerate the existing connections and hang up. The RasEnumConnections retrieves the current connections.

To terminate a connection

  1. Call RasEnumConnections to find all RAS connections.

  2. Call RasHangUp to terminate the connection.

Terminating a Connection Example

The following code example shows how to terminate a connection.

DWORD CloseRasConnections ()
{
  int index;                 // An integer index
  TCHAR szError[100];        // Buffer for error codes 
  DWORD dwError,             // Error code from a function call 
        dwRasConnSize,       // Size of RasConn in bytes
        dwNumConnections;    // Number of connections found 
  RASCONN RasConn[20];       // Buffer for connection state data 
                             // Assume the maximum number of entries is 
                             // 20. 

  // Assume no more than 20 connections.
  RasConn[0].dwSize = sizeof (RASCONN);
  dwRasConnSize = 20 * sizeof (RASCONN);

  // Find all connections.
  if (dwError = RasEnumConnections (RasConn, &dwRasConnSize, 
                                    &dwNumConnections))
  {
    wsprintf (szError, TEXT("RasEnumConnections Error: %ld"), dwError);
    return dwError;
  }

  // If there are no connections, return zero.
  if (!dwNumConnections)
  {
    wsprintf (szError, TEXT("No open RAS connections"));
    return 0;
  }

  // Terminate all of the remote access connections.
  for (index = 0; index < (int)dwNumConnections; ++index)
  {
    if (dwError = RasHangUp (RasConn[index].hrasconn))
    {
      wsprintf (szError, TEXT("RasHangUp Error: %ld"), dwError);
      return dwError;
    }
  }

  return 0;
}

See Also

Concepts

Sample Application