Click here to Skip to main content
13,764,506 members
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 2 Jul 2001

Detect RAS Connection

, 2 Jul 2001
Rate this:
Please Sign up or sign in to vote.
Detect internet connection using RAS API
<!-- Add the rest of your HTML here -->


There was a time I needed to detect an internet connection using the modem. I've checked the RAS library and found 2 useful functions: RasEnumConnections and RasGetConnectStatus.

The first function (RasEnumConnections) retrieve a specified amount of connections:

DWORD RasEnumConnections (
  LPRASCONN lprasconn, // buffer to receive connections data
  LPDWORD lpcb, // size in bytes of buffer
  LPDWORD lpcConnections // number of connections written to buffer
  • lprasconn - Points to a buffer that receives an array of RASCONN structures, one for each RAS connection. Before calling the function, an application must set the dwSize member of the first RASCONN structure in the buffer to sizeof(RASCONN) in order to identify the version of the structure being passed.
  • lpcb - Points to a variable that contains the size, in bytes, of the buffer specified by lprasconn. On return, the function sets this variable to the number of bytes required to enumerate the RAS connections.
  • lpcConnections - Points to a variable that the function sets to the number of RASCONN structures written to the buffer specified by lprasconn.

Return Value: 0 - Succeed; Other - Error code

The RASCONN is a structure which hold the information on a single connection:

typedef struct _RASCONN {
 DWORD dwSize;
 HRASCONN hrasconn;
 TCHAR szEntryName[RAS_MaxEntryName + 1];
#if (WINVER >= 0x400) 
 CHAR szDeviceType[RAS_MaxDeviceType + 1];
 CHAR szDeviceName[RAS_MaxDeviceName + 1];
  • dwSize - Specifies the size, in bytes, of the RASCONN structure.
  • hrasconn - Specifies the remote access connection. This handle is used in other remote access API calls.
  • szEntryName - A string that specifies the phone-book entry used to establish the remote access connection. If the connection was established using an empty entry name, this string consists of a "." followed by the connection phone number.
  • szDeviceType - A null-terminated string that contains the device type through which the connection is made.
  • szDeviceName - A null-terminated string that contains the device name through which the connection is made.

The second function (RasGetConnectStatus) retrieves information on the current status of the specified connection

DWORD RasGetConnectStatus (
  HRASCONN hrasconn, // handle to RAS connection of interest
  LPRASCONNSTATUS lprasconnstatus // buffer to receive status data
  • hrasconn - Identifies the remote access connection for which to retrieve the status. This handle must have been obtained from RasDial or RasEnumConnections.
  • lprasconnstatus - Points to a RASCONNSTATUS structure that the function fills with status information. Before calling the function, an application must set the dwSize member of the structure to sizeof(RASCONNSTATUS) in order to identify the version of the structure being passed.

Return Value: 0 - Succeed; Other - Error code

The RASCONNSTATUS structure describes the current status of a connection.

typedef struct _RASCONNSTATUS {
 DWORD dwSize;
 RASCONNSTATE rasconnstate;
 DWORD dwError;
 TCHAR szDeviceType[RAS_MaxDeviceType + 1];
 TCHAR szDeviceName[RAS_MaxDeviceName + 1];
  • dwSize - Specifies the structure size, in bytes.
  • rasconnstate - Specifies a RASCONNSTATE enumerator value that indicates the current state of the RasDial connection process; that is, the piece of the RasDial process that is currently executing.

    Two state values are especially significant:

    RASCS_ConnectedIndicates that the connection has been successfully established.
    RASCS_DisconnectedIndicates that the connection has failed.

  • dwError - If nonzero, indicates the reason for failure. The value is one of the error values from the RAS header file or one of ERROR_NOT_ENOUGH_MEMORY or ERROR_INVALID_HANDLE.
  • szDeviceType  - A string that specifies the type of the current device, if available. For example, common device types supported by RAS are "modem", "pad", "switch", "isdn", or "null".
  • szDeviceName - A string that specifies the name of the current device, if available. This would be the name of the modem — for example, "Hayes Smartmodem 2400"; the name of the PAD, for example "US Sprint"; or the name of a switch device, for example "Racal-Guardata".

To use the RAS library, you need to include <ras.h> in your source files and add rasapi32.lib to link with your application.

I've distributed my application and then I had a problem. One day, I got a mail which describes an error. It turns out that my client's computer missing rasapi32.dll. Only when you install the "Dial-out networking" you get rasapi32.dll. So instead linking rasapi32.lib to my application, I've decided to load it at runtime so I can detect if it is installed instead of getting an error message. What I had to remember is that rasapi32.dll contains 2 kinds of functions: ANSI and UNICODE, so the code need to know which one to use. I check the _UNICODE constant definition. Then, I load the library, look for the functions address and call them to get my connection status.

All you need to do to use the code, is to include "rasstatus.h" in your project. I also made a demo project but it is very simple and it only used to check that the code really works.

The code is not so hard to understand and I added some comments so you can understand easily.

////   RAS Connect status function
////   Written by Gilad Novik
////   For any questions or comments,

#ifndef _RASSTATUS
#define _RASSTATUS
#include <ras.h>

// We need to declare the functions type
typedef DWORD (WINAPI *RasEnumConnectionsType)(LPRASCONN lprasconn,
    LPDWORD lpcb,LPDWORD lpcConnections);
typedef DWORD (WINAPI *RasGetConnectStatusType)(HRASCONN hrasconn,
    LPRASCONNSTATUS lprasconnstatus);

#ifdef _UNICODE
#define RasFileName L"RASAPI32.DLL"
#define RasEnumConnectionsName L"RasEnumConnectionsW"
#define RasGetConnectStatusName L"RasGetConnectStatusW"
#define RasFileName "RASAPI32.DLL"
#define RasEnumConnectionsName "RasEnumConnectionsA"
#define RasGetConnectStatusName "RasGetConnectStatusA"

BOOL IsRasConnected()
  HINSTANCE hLib = LoadLibrary(RasFileName);  // Try to load the library
  if (hLib == NULL)
    return FALSE;  // Return FALSE if we can't find it

  // Get functions address
  RasEnumConnectionsType _RasEnumConnections = 
  RasGetConnectStatusType _RasGetConnectStatus = 
  BOOL bResult = FALSE;
  if (_RasEnumConnections && _RasGetConnectStatus)  
          // Check if it is valid pointer
    RASCONN RasConn;
    RASCONNSTATUS RasConnStatus;
    RasConnStatus.dwSize = sizeof(RASCONNSTATUS);    
           // We need to set the size of the structure
    DWORD dwConnSize = sizeof(RASCONN);
    DWORD dwRasCount = 1;  // We only retrieve one connection
    RasConn.dwSize = dwConnSize;  // We need to set the size 
                          // of the structure
    bResult =    // Let's check it out !!!
      (((*_RasEnumConnections)(&RasConn,&dwConnSize,&dwRasCount)) == 0)
        && (((*_RasGetConnectStatus)(RasConn.hrasconn,&RasConnStatus)) == 0) 
        && (RasConnStatus.rasconnstate == RASCS_Connected);
  FreeLibrary(hLib);    // Don't forget to unload the library from memory
  return bResult;
#endif // _RASSTATUS

All you need to do is to call IsRasConnected which returns TRUE is your are connected to the internet or FALSE otherwise. Hope it could help :) If you have any questions or comments, you can contact me: Gilad Novik -


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Gilad Novik
Web Developer
Israel Israel
Gilad was born accidently to a pair of old lesbians. His childhood was full of vibrators and drugs. Married without kids. Has 14 grandsons around the world, 4 crocodiles, 2 mushrooms and a green alien living behind the refrigerator.

Hobbies: Watching hardcore porn, sculpturing with snot, skydiving from stairs.

Check my Homepage for additional resources.

Quote: "There's always one more bug"

You may also be interested in...

Comments and Discussions

Answernnn Pin
Sohini Dey2127-Jan-12 1:40
memberSohini Dey2127-Jan-12 1:40 
GeneralMy vote of 1 Pin
ambika_clinton1-Nov-11 1:47
memberambika_clinton1-Nov-11 1:47 
GeneralMy vote of 1 Pin
ambika_clinton1-Nov-11 0:28
memberambika_clinton1-Nov-11 0:28 
NewsDotRAS Pin
Ferdinando Santacroce4-Mar-10 11:59
memberFerdinando Santacroce4-Mar-10 11:59 
Questionhow i can get disconnecting and disconnect status from RasGetConnectStatus method Pin
Khurram Shakoor1-Jul-09 4:23
memberKhurram Shakoor1-Jul-09 4:23 
AnswerRe: how i can get disconnect status from RasGetConnectStatus method Pin
programmer8112-Oct-09 19:13
memberprogrammer8112-Oct-09 19:13 
GeneralHi , I need to use rasapi32.dll Pin
_AGENT_SMITH_3-May-08 8:38
member_AGENT_SMITH_3-May-08 8:38 
QuestionIn C++ Builder 6.0, I use the function RasEnumConnections,but it always returns a Error 632,wy? Pin
king21th3-Mar-08 5:56
memberking21th3-Mar-08 5:56 
QuestionGet modem connection speed using RAS? Pin
daluu7-Sep-07 12:31
memberdaluu7-Sep-07 12:31 
GeneralRAS problem - RasGetConnectStatus returns wrong status Pin
Baggersnuiver22-Feb-06 4:23
memberBaggersnuiver22-Feb-06 4:23 
GeneralDoesn't seem to work Pin
alan9325-Oct-05 8:25
memberalan9325-Oct-05 8:25 
Generalvpn value Pin
Egn10-May-05 3:48
memberEgn10-May-05 3:48 
GeneralRe: vpn value Pin
Gilad Novik10-May-05 4:44
memberGilad Novik10-May-05 4:44 
GeneralRe: vpn value Pin
Egn10-May-05 17:59
memberEgn10-May-05 17:59 
GeneralWHAT'S A ..... Pin
scvarz26-Aug-04 7:01
memberscvarz26-Aug-04 7:01 
GeneralRasEnumConnections under win2k Pin
Muhammad Asif Khan16-Aug-04 23:37
memberMuhammad Asif Khan16-Aug-04 23:37 
GeneralProblems with Borland C++Builder 5.5 Pin
sherifd30-Apr-04 12:09
membersherifd30-Apr-04 12:09 
GeneralRe: Problems with Borland C++Builder 5.5 Pin
Gilad Novik1-May-04 22:45
memberGilad Novik1-May-04 22:45 
GeneralHelp using it on VC7 Pin
Locura66631-Mar-04 6:06
memberLocura66631-Mar-04 6:06 
GeneralRasEnumConnections returns no connection Pin
25-Apr-02 3:57
suss25-Apr-02 3:57 
Questionhow to write a RAS Service and Client? Pin
19-Jan-02 23:40
suss19-Jan-02 23:40 
QuestionHow do I detect the device used in a connection Pin
14-Oct-01 22:44
suss14-Oct-01 22:44 
AnswerRe: How do I detect the device used in a connection Pin
Anonymous3-Dec-03 23:17
memberAnonymous3-Dec-03 23:17 
QuestionHow do I detect non-RAS connection? Pin
Drew Berkemeyer5-Sep-01 2:29
memberDrew Berkemeyer5-Sep-01 2:29 
AnswerRe: How do I detect non-RAS connection? Pin
Drew Berkemeyer5-Sep-01 4:02
memberDrew Berkemeyer5-Sep-01 4:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.181113.4 | Last Updated 3 Jul 2001
Article Copyright 2001 by Gilad Novik
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid