Disconnect Dialup Connection

Environment: ******-->


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.


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.


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

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;
   return bOk;

This article was originally published on March 30th, 1999

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date