Knowledge Base Nr: 00073 Get_IP_from_RAS_sample.cpp - http://www.swe-kaiser.de

Downloads:

Win32: Get the current IP address(es) from an existing Remote Access Connection

  
#include <ras.h>

BOOL GetIPfromRAS(CString& strIP)
{
strIP = "<not connected>";

RASPPPIP proj_buf; // points to buffer that receives
// projection information
DWORD cb; // points to variable that specifies
// buffer size
RASCONN rasconn[1]; // buffer to receive connections data
DWORD cConnections;

cb = sizeof(rasconn);
rasconn[0].dwSize = cb;

DWORD ret = RasEnumConnections(
&rasconn[0], // buffer to receive connections data
&cb, // size in bytes of buffer
&cConnections); // number of connections written to buffer

if (ret == 0)
{
for (int n=0; n<cConnections; n++)
{
cb = sizeof(proj_buf);
proj_buf.dwSize = cb;
if (!RasGetProjectionInfo(rasconn[n].hrasconn, RASP_PppIp, (LPVOID)&proj_buf, &cb))
{
strIP += proj_buf.szIpAddress;
if (n<cConnections-1) //suppress trailing space
strIP += " / ";
}
}

return (cConnections > 0);
}
else
{
char szErrorString[200]; // buffer to hold error string
RasGetErrorString(ret, // error to get string for
&szErrorString[0], // buffer to hold error string
sizeof(szErrorString)); // size, in characters, of buffer
strIP.Format("RAS error: %s\n", szErrorString);
fprintf(stderr, strIP);
}

return FALSE;
}


...
CString strIP;
BOOL bSucc = GetIPfromRAS(strIP);
ASSERT(bSucc);
...