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 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


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;
}


More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read