Disconnect Dialup Connection | CodeGuru

Disconnect Dialup Connection

Environment: ******–> Problem: Using the WinInet class there4s a slight problem when disconnecting from the Internet because shutting down der Internet session doesn4t close the dial up connection. Unfortunaltely CInternetSession::Close() wont give any possibility of getting the phone line free. Solution: The RAS Api has got a method called RasHangUp() which does that job but […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 30, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: ******–>

Problem:

Using the WinInet class there4s a slight problem when disconnecting from the Internet because shutting down der Internet session doesn4t close the
dial up connection. Unfortunaltely CInternetSession::Close() wont give any possibility of getting the phone line free.

Solution:



The RAS Api has got a method called RasHangUp() which does that
job but it needs an RAS handle to do so. There a two ways to get hold
of this handle; one is to establish the whole RAS session which is rather
complicated and quite unnecessary. The easier one is shown in the
code sample below. It uses RasEnumConnections() to get the desired
handle; which also works if the connection wasn4t made using RAS
directly.


Needed:



In order to get the code below going it is necessary to


  1. have RAS installed on the system ( RASAPI32.DLL ). This is
    automatically the case when you`ve got a dial up network installed

  2. inlude RAS.H

  3. link RASAPI32.LIB


Advertisement

Source Code:

// Quit a dial up connection  - rasapi32.lib und #include "ras.h" needed
bool DisconnectRas()
{
   bool bOk = false;
   RASCONN ras[20];
   DWORD dSize,dNumber,dCount;

   ras[0].dwSize = sizeof(RASCONN);
   dSize = sizeof( ras );

   // Get active RAS - Connection
   if( RasEnumConnections( ras, &dSize, &dNumber ) == 0 )
   {
      bOk = true;

      for( dCount = 0; dCount < dNumber; dCount++ )
      {
         // Hang up that connection
         if( RasHangUp(ras[dCount].hrasconn) != 0 )
         {
            bOk = false;
            break;
         }
      }
   }
   return bOk;
}


CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.