Click here to Skip to main content
13,048,011 members (75,914 online)
Rate this:
Please Sign up or sign in to vote.
See more:
As commented on[^] this link RasEnum code I face the same problem:

Possible inaccuracy in the Note section to lpcb param. Calling RasEnumConnections with lprasconn set to NULL to determine the required buffer size, it doesn't seem to work on Window ver < Vista.

I added the below two lines before calling the RasEnumConnections, and it works fine. Is it right? Please correct me if I am wrong.
lpRasConn = (LPRASCONN) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCb); lpRasConn[0].dwSize = sizeof(RASCONN);

The below is my code, from microcsoft added with above two lines.
#include <windows.h>
#include <stdio.h>
#include "ras.h"
#include "raserror.h"
#pragma comment(lib, "rasapi32.lib")
DWORD __cdecl wmain(){
    DWORD dwCb = 0;
    DWORD dwConnections = 0;
    LPRASCONN lpRasConn = NULL;
    // Call RasEnumConnections with lpRasConn = NULL. dwCb is returned with the required buffer size and 
    // a return code of ERROR_BUFFER_TOO_SMALL
    lpRasConn = (LPRASCONN) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCb);
    lpRasConn[0].dwSize = sizeof(RASCONN);
    dwRet = RasEnumConnections(lpRasConn, &dwCb, &dwConnections);
    if (dwRet == ERROR_BUFFER_TOO_SMALL){
        // Allocate the memory needed for the array of RAS structure(s).
        lpRasConn = (LPRASCONN) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCb);
        if (lpRasConn == NULL){
            wprintf(L"HeapAlloc failed!\n");
            return 0;
        // The first RASCONN structure in the array must contain the RASCONN structure size
        lpRasConn[0].dwSize = sizeof(RASCONN);
        // Call RasEnumConnections to enumerate active connections
        dwRet = RasEnumConnections(lpRasConn, &dwCb, &dwConnections);
        // If successful, print the names of the active connections.
        if (ERROR_SUCCESS == dwRet){
            wprintf(L"The following RAS connections are currently active:\n");
            for (DWORD i = 0; i < dwConnections; i++){
                         wprintf(L"%s\n", lpRasConn[i].szEntryName);
        //Deallocate memory for the connection buffer
        HeapFree(GetProcessHeap(), 0, lpRasConn);
        lpRasConn = NULL;
        return 0;
    // There was either a problem with RAS or there are no connections to enumerate    
    if(dwConnections >= 1){
        wprintf(L"The operation failed to acquire the buffer size.\n");
        wprintf(L"There are no active RAS connections.\n");
    return 0;
Posted 22-Aug-12 4:20am
Updated 22-Aug-12 11:06am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The things that would make it 'wrong' are....

1. It doesn't work.
2. It throws exceptions.
3. It fails if you make repeated calls to this method.
4. It leaks resources.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 22 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100