C 代码更正,使RASENUM连接在XP中工作

C 代码更正,使RASENUM连接在XP中工作,c,winapi,ras,C,Winapi,Ras,正如我所评论的,我面临着同样的问题: lpcb参数注释部分可能不准确。使命感 lprasconn设置为NULL的RasEnumConnections可确定 所需的缓冲区大小,它似乎不适用于Windows Vista 在调用RasEnumConnections之前,我添加了以下两行代码,效果很好。是这样吗?如果我错了,请纠正我 lpRasConn=(lpRasConn)HeapAlloc(GetProcessHeap(),HEAP\u ZERO\u内存, dwCb);lpRasConn[0]。dw

正如我所评论的,我面临着同样的问题:

lpcb参数注释部分可能不准确。使命感 lprasconn设置为NULL的RasEnumConnections可确定 所需的缓冲区大小,它似乎不适用于Windows Vista

在调用RasEnumConnections之前,我添加了以下两行代码,效果很好。是这样吗?如果我错了,请纠正我

lpRasConn=(lpRasConn)HeapAlloc(GetProcessHeap(),HEAP\u ZERO\u内存, dwCb);lpRasConn[0]。dwSize=sizeof(RASCONN)

请添加您的建议

#include <windows.h>
#include <stdio.h>
#include "ras.h"
#include "raserror.h"
#pragma comment(lib, "rasapi32.lib")

DWORD __cdecl wmain(){

    DWORD dwCb = 0;
    DWORD dwRet = ERROR_SUCCESS;
    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
    /*I ADDED THE BELOW TWO LINES */

    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");
    }else{
        wprintf(L"There are no active RAS connections.\n");
    }

    return 0;
}
#包括
#包括
#包括“ras.h”
#包括“raserror.h”
#pragma注释(lib,“rasapi32.lib”)
德沃德{
DWORD dwCb=0;
DWORD dwRet=错误\成功;
DWORD dwConnections=0;
LPRASCONN LPRASCONN=NULL;
//使用lpRasConn=NULL调用RasEnumConnections。返回的dwCb具有所需的缓冲区大小和
//返回错误代码\u缓冲区\u太小\u
/*我加了下面两行*/
lpRasConn=(lpRasConn)HeapAlloc(GetProcessHeap(),HEAP\u ZERO\u内存,dwCb);
lpRasConn[0]。dwSize=sizeof(RASCONN);
dwRet=RasEnumConnections(lpRasConn、&dwCb、&dwConnections);
if(dwRet==错误\u缓冲区\u太小){
//分配RAS结构数组所需的内存。
lpRasConn=(lpRasConn)HeapAlloc(GetProcessHeap(),HEAP\u ZERO\u内存,dwCb);
如果(lpRasConn==NULL){
wprintf(L“HeapAlloc失败!\n”);
返回0;
}
//阵列中的第一个RASCONN结构必须包含RASCONN结构大小
lpRasConn[0]。dwSize=sizeof(RASCONN);
//调用RasEnumConnections枚举活动连接
dwRet=RasEnumConnections(lpRasConn、&dwCb、&dwConnections);
//如果成功,请打印活动连接的名称。
如果(错误\成功==dwRet){
wprintf(L“以下RAS连接当前处于活动状态:\n”);
对于(DWORD i=0;i=1){
wprintf(L“该操作无法获取缓冲区大小。\n”);
}否则{
wprintf(L“没有活动的RAS连接。\n”);
}
返回0;
}

Psiphon项目程序员已经解决了这个问题,请参见第394行

以下是来源的第一行:

HRASCONN rasConnection = 0;
RASCONN conn;
memset(&conn, 0, sizeof(conn));
conn.dwSize = sizeof(conn);
LPRASCONN rasConnections = &conn;
DWORD bufferSize = sizeof(conn);
DWORD connections = 0;

DWORD returnCode = RasEnumConnections(rasConnections, &bufferSize, &connections);

// On Windows XP, we can't call RasEnumConnections with rasConnections = 0 because it
// fails with 632 ERROR_INVALID_SIZE.  So we set rasConnections to point to a single
// RASCONN, and we'll always reallocate, even if there is only one RASCONN and the
// first call succeeds.
if (ERROR_SUCCESS == returnCode)
{
    returnCode = ERROR_BUFFER_TOO_SMALL;
}

// NOTE: Race condition where a new connection is added between the buffersize check
//       and the second call.
if (ERROR_BUFFER_TOO_SMALL != returnCode && connections > 0)
{
    my_print(false, _T("RasEnumConnections failed (%d)"), returnCode);
}
else if (ERROR_BUFFER_TOO_SMALL == returnCode && connections > 0)
{
    // See "A fix to work with older versions of Windows"
    if (bufferSize < (connections * sizeof(RASCONN)))
    {
        bufferSize = connections * sizeof(RASCONN);
    }

    // Allocate the memory needed for the array of RAS structure(s).
    rasConnections = (LPRASCONN)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bufferSize);
hraconn rasConnection=0;
拉斯康;
memset(&conn,0,sizeof(conn));
conn.dwSize=sizeof(conn);
LPRASCONN rasConnections=&conn;
DWORD bufferSize=sizeof(康涅狄格州);
DWORD连接=0;
DWORD returnCode=RasEnumConnections(rasConnections、缓冲区大小和连接);
//在Windows XP上,无法使用rasConnections=0调用RasEnumConnections,因为它
//失败,出现632错误\u无效\u大小。因此,我们将rasConnections设置为指向单个
//RASCONN,我们将始终重新分配,即使只有一个RASCONN和
//第一次呼叫成功。
如果(错误\成功==返回代码)
{
returnCode=错误\缓冲区\太小;
}
//注意:在缓冲区大小检查之间添加新连接的争用条件
//还有第二个电话。
if(错误\u缓冲区\u太小!=returnCode&&connections>0)
{
my_print(false,_T(“RasEnumConnections failed(%d)”),返回代码);
}
else if(ERROR\u BUFFER\u TOO\u SMALL==returnCode&&connections>0)
{
//请参阅“使用旧版本Windows的修复程序”
if(缓冲区大小<(连接*sizeof(RASCONN)))
{
bufferSize=连接数*sizeof(RASCONN);
}
//分配RAS结构数组所需的内存。
rasConnections=(LPRASCONN)HeapAlloc(GetProcessHeap(),HEAP\u ZERO\u内存,bufferSize);

创建的缓冲区长度为零