Click here to Skip to main content
Click here to Skip to main content

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

Introduction

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];
#endif
} RASCONN ;
  • 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];
} RASCONNSTATUS;
  • 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:

    State Meaning
    RASCS_Connected Indicates that the connection has been successfully established.
    RASCS_Disconnected Indicates 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, gilad@bmidas.com


#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"
#else
#define RasFileName "RASAPI32.DLL"
#define RasEnumConnectionsName "RasEnumConnectionsA"
#define RasGetConnectStatusName "RasGetConnectStatusA"
#endif

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 = 
    (RasEnumConnectionsType)GetProcAddress(hLib,RasEnumConnectionsName);
  RasGetConnectStatusType _RasGetConnectStatus = 
    (RasGetConnectStatusType)GetProcAddress(hLib,RasGetConnectStatusName);
  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 Smile | :) If you have any questions or comments, you can contact me: Gilad Novik - gilad@bmidas.com

License

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"

Comments and Discussions

 
Answernnn PinmemberSohini Dey2127-Jan-12 0:40 
GeneralMy vote of 1 Pinmemberambika_clinton1-Nov-11 0:47 
GeneralMy vote of 1 Pinmemberambika_clinton31-Oct-11 23:28 
NewsDotRAS Pinmemberjesus_was_rasta4-Mar-10 10:59 
Questionhow i can get disconnecting and disconnect status from RasGetConnectStatus method PinmemberKhurram Shakoor1-Jul-09 3:23 
AnswerRe: how i can get disconnect status from RasGetConnectStatus method Pinmemberprogrammer8112-Oct-09 18:13 
GeneralHi , I need to use rasapi32.dll Pinmember_AGENT_SMITH_3-May-08 7:38 
Hi people,
I have been trying to use rasapi32.dll file on a simple win32 console (c/c++) program...
 
I include ras.h file and try to use it..
 
but during final run a link time error says that the file is corrupt
?
anyidea...? I am using windows xp, and I dont want to use getProcAddress
as it makes the internal c compiler (cl.exe ) crash...
 
im using winxp and vs2k5..
QuestionIn C++ Builder 6.0, I use the function RasEnumConnections,but it always returns a Error 632,wy? Pinmemberking21th3-Mar-08 4:56 
QuestionGet modem connection speed using RAS? Pinmemberdaluu7-Sep-07 11:31 
GeneralRAS problem - RasGetConnectStatus returns wrong status PinmemberBaggersnuiver22-Feb-06 3:23 
GeneralDoesn't seem to work Pinmemberalan9325-Oct-05 7:25 
Generalvpn value PinmemberEgn10-May-05 2:48 
GeneralRe: vpn value PinmemberGilad Novik10-May-05 3:44 
GeneralRe: vpn value PinmemberEgn10-May-05 16:59 
GeneralWHAT'S A ..... Pinmemberscvarz26-Aug-04 6:01 
GeneralRasEnumConnections under win2k PinmemberMuhammad Asif Khan16-Aug-04 22:37 
GeneralProblems with Borland C++Builder 5.5 Pinmembersherifd30-Apr-04 11:09 
GeneralRe: Problems with Borland C++Builder 5.5 PinmemberGilad Novik1-May-04 21:45 
GeneralHelp using it on VC7 PinmemberLocura66631-Mar-04 5:06 
GeneralRasEnumConnections returns no connection PinmemberYefi25-Apr-02 2:57 
Questionhow to write a RAS Service and Client? PinmemberSungrass19-Jan-02 22:40 
QuestionHow do I detect the device used in a connection PinmemberTero14-Oct-01 21:44 
AnswerRe: How do I detect the device used in a connection PinsussAnonymous3-Dec-03 22:17 
QuestionHow do I detect non-RAS connection? PinmemberDrew Berkemeyer5-Sep-01 1:29 
AnswerRe: How do I detect non-RAS connection? PinmemberDrew Berkemeyer5-Sep-01 3:02 
GeneralRe: How do I detect non-RAS connection? PinmemberGilad Novik5-Sep-01 4:08 
GeneralRe: How do I detect non-RAS connection? PinmemberDrew Berkemeyer5-Sep-01 5:55 
GeneralRe: How do I detect non-RAS connection? PinmemberGilad Novik5-Sep-01 6:11 
GeneralRe: How do I detect non-RAS connection? PinmemberJase Jennings28-Jul-03 11:14 
GeneralYou should zero-init the structs PinmemberMichael Dunn4-Jul-01 7:27 
GeneralRe: You should zero-init the structs PinmemberGilad Novik4-Jul-01 7:50 
GeneralRe: You should zero-init the structs PinmemberMichael Dunn4-Jul-01 7:55 
GeneralMisspelling PinmemberGilad Novik3-Jul-01 10:25 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 3 Jul 2001
Article Copyright 2001 by Gilad Novik
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid