Click here to Skip to main content
6,595,444 members and growing! (19,015 online)
Email Password   helpLost your password?
General Programming » Internet / Network » Dialup     Intermediate

Detect RAS Connection

By Gilad Novik

Detect internet connection using RAS API
VC6Win2K, MFC, Dev
Posted:2 Jul 2001
Views:120,633
Bookmarked:28 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 4.20 Rating: 4.40 out of 5
2 votes, 25.0%
1

2

3

4
6 votes, 75.0%
5

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


Member
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"
Occupation: Web Developer
Location: Israel Israel

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 29 (Total in Forum: 29) (Refresh)FirstPrevNext
Generalhow i can get disconnecting and disconnect status from RasGetConnectStatus method PinmemberKhurram Shakoor4:23 1 Jul '09  
GeneralRe: how i can get disconnect status from RasGetConnectStatus method Pinmemberprogrammer8119:13 12 Oct '09  
GeneralHi , I need to use rasapi32.dll Pinmember_AGENT_SMITH_8:38 3 May '08  
GeneralIn C++ Builder 6.0, I use the function RasEnumConnections,but it always returns a Error 632,wy? Pinmemberking21th5:56 3 Mar '08  
GeneralGet modem connection speed using RAS? Pinmemberdaluu12:31 7 Sep '07  
GeneralRAS problem - RasGetConnectStatus returns wrong status PinmemberBaggersnuiver4:23 22 Feb '06  
GeneralDoesn't seem to work Pinmemberalan938:25 25 Oct '05  
Generalvpn value PinmemberEgn3:48 10 May '05  
GeneralRe: vpn value PinmemberGilad Novik4:44 10 May '05  
GeneralRe: vpn value PinmemberEgn17:59 10 May '05  
GeneralWHAT'S A ..... Pinmemberscvarz7:01 26 Aug '04  
GeneralRasEnumConnections under win2k PinmemberMuhammad Asif Khan23:37 16 Aug '04  
GeneralProblems with Borland C++Builder 5.5 Pinmembersherifd12:09 30 Apr '04  
GeneralRe: Problems with Borland C++Builder 5.5 PinmemberGilad Novik22:45 1 May '04  
GeneralHelp using it on VC7 PinmemberLocura6666:06 31 Mar '04  
GeneralRasEnumConnections returns no connection PinmemberYefi3:57 25 Apr '02  
Generalhow to write a RAS Service and Client? PinmemberSungrass23:40 19 Jan '02  
GeneralHow do I detect the device used in a connection PinmemberTero22:44 14 Oct '01  
GeneralRe: How do I detect the device used in a connection PinsussAnonymous23:17 3 Dec '03  
GeneralHow do I detect non-RAS connection? PinmemberDrew Berkemeyer2:29 5 Sep '01  
GeneralRe: How do I detect non-RAS connection? PinmemberDrew Berkemeyer4:02 5 Sep '01  
GeneralRe: How do I detect non-RAS connection? PinmemberGilad Novik5:08 5 Sep '01  
GeneralRe: How do I detect non-RAS connection? PinmemberDrew Berkemeyer6:55 5 Sep '01  
GeneralRe: How do I detect non-RAS connection? PinmemberGilad Novik7:11 5 Sep '01  
GeneralRe: How do I detect non-RAS connection? PinmemberJase Jennings12:14 28 Jul '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 Jul 2001
Editor: Nishant Sivakumar
Copyright 2001 by Gilad Novik
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project