Mark Snelling
March 30th, 1999, 02:23 AM
Here's a quick class I made up to handle this kind of thing.
Just create a CRASConnection object (defined below), call
CRASConnection::Refresh()
then check if there is an active connection using the
CRASConnection ::IsConnected()
member function which returns TRUE if there is a connected RAS session.
I hope this helps!
Mark Snelling
mark@bakedbeans.com
PS: if you can improve this in any way, could you send the updated file back to me? thanks :)
// RASConnection.h: interface for the CRASConnection class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_RASCONNECTION_H__272696F1_CB23_11D2_914C_0040052AB9BC__INCLUDED_)
#define AFX_RASCONNECTION_H__272696F1_CB23_11D2_914C_0040052AB9BC__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <ras.h>
#include <raserror.h>
class CRASConnection
{
public:
DWORD Refresh();
BOOL IsConnected();
CRASConnection();
virtual ~CRASConnection();
private:
DWORD m_cConn;
LPHRASCONN m_lphRasConn;
LPRASCONN m_lpRasConn;
RASCONNSTATUS m_RasStatus;
};
#endif // !defined(AFX_RASCONNECTION_H__272696F1_CB23_11D2_914C_0040052AB9BC__INCLUDED_)
// RASConnection.cpp: implementation of the CRASConnection class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "RASConnection.h"
#pragma message("library is linking with \"rasapi32.lib\"")
#pragma comment(lib, "rasapi32.lib")
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CRASConnection::CRASConnection()
{
// Allocate memory for RAS connection information array
m_lpRasConn = (LPRASCONN)malloc(sizeof(RASCONN));
// Allocate memory for RAS connection handle array
m_lphRasConn = (LPHRASCONN)malloc(sizeof(HRASCONN));
// Set 'dwSize' attribute to reflect structure version
m_RasStatus.dwSize = sizeof(RASCONNSTATUS);
}
CRASConnection::~CRASConnection()
{
if (m_lpRasConn)
free(m_lpRasConn);
if (m_lphRasConn)
free(m_lphRasConn);
}
DWORD CRASConnection::Refresh()
{
DWORD ret = 0;
DWORD dwStatus = 0;
DWORD cbBuf = 0;
DWORD ndx = 0;
// Temporary pointers for memory reallocation
LPRASCONN lpTemp = NULL;
LPHRASCONN lphTemp = NULL;
cbBuf = sizeof(RASCONN);
// Set size parameter to let 'RasEnumConnections' know version of struct
m_lpRasConn->dwSize = sizeof(RASCONN);
// Attempt to get RAS connection details
dwStatus = RasEnumConnections(m_lpRasConn, &cbBuf, &m_cConn);
// Check if array is large enough
if (dwStatus == ERROR_BUFFER_TOO_SMALL)
{
// Reallocate memory for RAS connection information array
lpTemp = (LPRASCONN)realloc(m_lpRasConn, cbBuf);
// Was the memory reallocated??
if (lpTemp == NULL)
ret = -1;
m_lpRasConn = lpTemp;
// Call function again with correct array size
dwStatus = RasEnumConnections(m_lpRasConn, &cbBuf, &m_cConn);
// Reallocate memory for RAS connection handle array
lphTemp = (LPHRASCONN)realloc(m_lphRasConn, sizeof(HRASCONN) * m_cConn);
// Was the memory reallocated??
if (lphTemp == NULL)
ret = -1;
m_lphRasConn = lphTemp;
}
if (ret == 0)
{
// Populate connection handle array
for (ndx = 0; ndx < m_cConn; ndx++)
m_lphRasConn[ndx] = m_lpRasConn[ndx].hrasconn;
}
// Return status
return ret;
}
BOOL CRASConnection::IsConnected()
{
// Set default connection state
BOOL ret = FALSE;
// Check if there is a connection
if (m_cConn != NULL)
{
// Get connection status for eaqch connection
for (DWORD ndx = 0; ndx < m_cConn; ndx++)
{
RasGetConnectStatus(m_lpRasConn[ndx].hrasconn , &m_RasStatus);
// Check status of connection
if (m_RasStatus.rasconnstate == RASCS_Connected)
{
// We have a valid connection!
ret = TRUE;
}
}
}
// Return connection status
return ret;
}
Just create a CRASConnection object (defined below), call
CRASConnection::Refresh()
then check if there is an active connection using the
CRASConnection ::IsConnected()
member function which returns TRUE if there is a connected RAS session.
I hope this helps!
Mark Snelling
mark@bakedbeans.com
PS: if you can improve this in any way, could you send the updated file back to me? thanks :)
// RASConnection.h: interface for the CRASConnection class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_RASCONNECTION_H__272696F1_CB23_11D2_914C_0040052AB9BC__INCLUDED_)
#define AFX_RASCONNECTION_H__272696F1_CB23_11D2_914C_0040052AB9BC__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <ras.h>
#include <raserror.h>
class CRASConnection
{
public:
DWORD Refresh();
BOOL IsConnected();
CRASConnection();
virtual ~CRASConnection();
private:
DWORD m_cConn;
LPHRASCONN m_lphRasConn;
LPRASCONN m_lpRasConn;
RASCONNSTATUS m_RasStatus;
};
#endif // !defined(AFX_RASCONNECTION_H__272696F1_CB23_11D2_914C_0040052AB9BC__INCLUDED_)
// RASConnection.cpp: implementation of the CRASConnection class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "RASConnection.h"
#pragma message("library is linking with \"rasapi32.lib\"")
#pragma comment(lib, "rasapi32.lib")
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CRASConnection::CRASConnection()
{
// Allocate memory for RAS connection information array
m_lpRasConn = (LPRASCONN)malloc(sizeof(RASCONN));
// Allocate memory for RAS connection handle array
m_lphRasConn = (LPHRASCONN)malloc(sizeof(HRASCONN));
// Set 'dwSize' attribute to reflect structure version
m_RasStatus.dwSize = sizeof(RASCONNSTATUS);
}
CRASConnection::~CRASConnection()
{
if (m_lpRasConn)
free(m_lpRasConn);
if (m_lphRasConn)
free(m_lphRasConn);
}
DWORD CRASConnection::Refresh()
{
DWORD ret = 0;
DWORD dwStatus = 0;
DWORD cbBuf = 0;
DWORD ndx = 0;
// Temporary pointers for memory reallocation
LPRASCONN lpTemp = NULL;
LPHRASCONN lphTemp = NULL;
cbBuf = sizeof(RASCONN);
// Set size parameter to let 'RasEnumConnections' know version of struct
m_lpRasConn->dwSize = sizeof(RASCONN);
// Attempt to get RAS connection details
dwStatus = RasEnumConnections(m_lpRasConn, &cbBuf, &m_cConn);
// Check if array is large enough
if (dwStatus == ERROR_BUFFER_TOO_SMALL)
{
// Reallocate memory for RAS connection information array
lpTemp = (LPRASCONN)realloc(m_lpRasConn, cbBuf);
// Was the memory reallocated??
if (lpTemp == NULL)
ret = -1;
m_lpRasConn = lpTemp;
// Call function again with correct array size
dwStatus = RasEnumConnections(m_lpRasConn, &cbBuf, &m_cConn);
// Reallocate memory for RAS connection handle array
lphTemp = (LPHRASCONN)realloc(m_lphRasConn, sizeof(HRASCONN) * m_cConn);
// Was the memory reallocated??
if (lphTemp == NULL)
ret = -1;
m_lphRasConn = lphTemp;
}
if (ret == 0)
{
// Populate connection handle array
for (ndx = 0; ndx < m_cConn; ndx++)
m_lphRasConn[ndx] = m_lpRasConn[ndx].hrasconn;
}
// Return status
return ret;
}
BOOL CRASConnection::IsConnected()
{
// Set default connection state
BOOL ret = FALSE;
// Check if there is a connection
if (m_cConn != NULL)
{
// Get connection status for eaqch connection
for (DWORD ndx = 0; ndx < m_cConn; ndx++)
{
RasGetConnectStatus(m_lpRasConn[ndx].hrasconn , &m_RasStatus);
// Check status of connection
if (m_RasStatus.rasconnstate == RASCS_Connected)
{
// We have a valid connection!
ret = TRUE;
}
}
}
// Return connection status
return ret;
}