Click here to Skip to main content
15,887,135 members
Articles / Desktop Programming / MFC

Detect RAS Connection

Rate me:
Please Sign up or sign in to vote.
4.92/5 (10 votes)
2 Jul 20014 min read 202.4K   2.5K   32  
Detect internet connection using RAS API
////   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

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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


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