2012-08-22 22 views
1

工作作爲MSDN RasEnum code評論我面臨同樣的問題:碼校正,使RASENUM連接到XP

在註釋部分LPCB PARAM可能不準確。調用 RasEnumConnections與lprasconn設置爲NULL,以確定 所需的緩衝區大小,它似乎並沒有對窗口工作版本< Vista系統。

我在調用RasEnumConnections之前添加了下面兩行,它工作正常。這樣對嗎?如果我錯了,請糾正我。

lpRasConn =(LPRASCONN)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY, 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; 
} 
+0

所創建的緩衝器是零長度 – Jay

回答

0

的Psiphon項目的程序員已經解決了這個問題,請參閱source code323 -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);