ras api win7 和 win xp 遍历时的不同

简介: 由于在调用RasEnumEntries和RasEnumConnections在xp和win7以上的操作系统中有所不同,所以在win7下正常的代码在xp不一定就可以。 主要是在win7 下可以给参数传NULL来得到所需要大小,而在xp下则不可以传NULL,在xp下只需要传一个对象的大小,然后得到所需大小。

由于在调用RasEnumEntries和RasEnumConnections在xp和win7以上的操作系统中有所不同,所以在win7下正常的代码在xp不一定就可以。

主要是在win7 下可以给参数传NULL来得到所需要大小,而在xp下则不可以传NULL,在xp下只需要传一个对象的大小,然后得到所需大小。再进行分配存储空间,再进行遍历 。废话不说了,直接上代码了。

vector<CRasdilInfo> EnumAdslNames_win7(void)
{
    vector<CRasdilInfo> retList;
    DWORD dwCb = 0;  
    DWORD dwRet = ERROR_SUCCESS;  
    DWORD dwEntries = 0;  
    LPRASENTRYNAME lpRasEntryName = NULL;  
    // Call RasEnumEntries with lpRasEntryName = NULL. dwCb is returned with the required buffer size and  
    // a return code of ERROR_BUFFER_TOO_SMALL  
    // 用lpRasEntryName = NULL 来调用 RasEnumEntries, 其中dwCb是一个传出值, 用来返回成功调用所需的缓冲区的字节数.  
    dwRet = RasEnumEntries(NULL, NULL, lpRasEntryName, &dwCb, &dwEntries);    
    // 函数成功返回0  
    if (dwRet == ERROR_BUFFER_TOO_SMALL){         
        // Allocate the memory needed for the array of RAS entry names.  
        // 分配遍历条目所需要的字节输          
        lpRasEntryName = (LPRASENTRYNAME) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCb);        
        // 如果lpRasEntryName指针为NULL, 则说明分配内存失败         
        if (lpRasEntryName == NULL){  
            // cout << "HeapAlloc failed!" << endl;  
            //cout << "分配内存失败! " << endl;  
            return retList;  
        }     
        // The first RASENTRYNAME structure in the array must contain the structure size  
        // 数组中第一个 RASENRTYNAME 结构必须包含结构体的大小       
        lpRasEntryName[0].dwSize = sizeof(RASENTRYNAME);          
        // Call RasEnumEntries to enumerate all RAS entry names  
        // 调用 RasEnumEntries 枚举所有的连接名称        
        dwRet = RasEnumEntries(NULL, NULL, lpRasEntryName, &dwCb, &dwEntries);  

        // If successful, print the RAS entry names  
        // 如果调用成功, 打印出每个连接的名称         
        if (ERROR_SUCCESS == dwRet){  
            // cout <<  "The following RAS entry names were found:" << endl;  
            for (DWORD i = 0; i < dwEntries; i++){  
                //cout << i << "    " << lpRasEntryName[i].szEntryName << endl;  
                CRasdilInfo obj;
                obj.strEntryName = lpRasEntryName[i].szEntryName;
                obj.strPhoneBook = lpRasEntryName[i].szPhonebookPath;
                //GetRasParam(&obj.rasDialParam,obj.strPhoneBook.c_str(),obj.strEntryName.c_str());
                retList.push_back(obj);
            }  
        }         
        // Deallocate memory for the connection buffer  
        // 释放用于存放连接名称的内存  
        HeapFree(GetProcessHeap(), 0, lpRasEntryName);        
        // 赋值空指针  
        lpRasEntryName = NULL;  
    }else {       
        // There was either a problem with RAS or there are RAS entry names to enumerate  
        // 枚举连接名称出现的问题        
        if(dwEntries >= 1){            
            // cout << "The operation failed to acquire the buffer size." << endl;  
        }else{            
            // cout << "There were no RAS entry names found:." << endl;  
            
        }  
    }  
    return retList;
}

vector<CRasdilInfo> EnumAdslNames_xp(void)
{
    OutputDebugInfo("EnumAdslNames_xp");
    vector<CRasdilInfo> retList;
    DWORD dwCb = sizeof(RASENTRYNAME);  
    DWORD dwRet = ERROR_SUCCESS;  
    DWORD dwEntries = 0;  

    RASENTRYNAME ras_entry_name = {0};
    ras_entry_name.dwSize = sizeof(RASENTRYNAME); 
    LPRASENTRYNAME  lpRasEntryName = &ras_entry_name;

    dwRet = RasEnumEntries(NULL, NULL, lpRasEntryName, &dwCb, &dwEntries);    
    if(dwRet == ERROR_SUCCESS)
        dwRet = ERROR_BUFFER_TOO_SMALL;

    if(dwRet != ERROR_BUFFER_TOO_SMALL && dwEntries > 0)
    {
    }
    else if(dwRet == ERROR_BUFFER_TOO_SMALL && dwEntries > 0)
    {
        if(dwCb < (dwEntries * sizeof(RASENTRYNAME)))
            dwCb = dwEntries * sizeof(RASENTRYNAME);

        lpRasEntryName = (LPRASENTRYNAME) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCb);
        lpRasEntryName->dwSize = sizeof(RASENTRYNAME);
        dwRet = RasEnumEntries(NULL, NULL, lpRasEntryName, &dwCb, &dwEntries); 
        if(dwRet == ERROR_SUCCESS)
        {
            for (DWORD i = 0; i < dwEntries; i++){  
                CRasdilInfo obj;
                obj.strEntryName = lpRasEntryName[i].szEntryName;
                obj.strPhoneBook = lpRasEntryName[i].szPhonebookPath;
                retList.push_back(obj);
            } 
        }
        HeapFree(GetProcessHeap(), 0, lpRasEntryName);        
        lpRasEntryName = NULL;  

    }
    return retList;
}
 
 

vector<CRasdilInfo> EnumRasConnections_xp()
{
//OutputDebugInfoA("EnumRasConnections_xp");
DWORD dwCb = 704; //windows xp 固定为704
DWORD dwRet = ERROR_SUCCESS;
DWORD dwConnections = 0;
LPRASCONN lpRasConn = NULL;
RASCONN conn = {0};
lpRasConn = &conn;
lpRasConn->dwSize = 704;//windows xp 固定为704
dwRet = RasEnumConnections(lpRasConn, &dwCb, &dwConnections);
if(dwRet == ERROR_SUCCESS)
dwRet = ERROR_BUFFER_TOO_SMALL;
vector<CRasdilInfo> retList;
if (dwRet != ERROR_BUFFER_TOO_SMALL && dwConnections > 0)
{
}
else if(dwRet == ERROR_BUFFER_TOO_SMALL && dwConnections > 0)
{
// Allocate the memory needed for the array of RAS structure(s).
lpRasConn = (LPRASCONN) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCb);
// The first RASCONN structure in the array must contain the RASCONN structure size
lpRasConn[0].dwSize = 704;

 
 

// 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++){
//OutputDebugInfo("EnumRasConnections_xp %s\n", lpRasConn[i].szEntryName);
CRasdilInfo Obj;
Obj.strEntryName = lpRasConn[i].szEntryName;
Obj.strPhoneBook = lpRasConn[i].szPhonebook;
Obj.hRasConn = lpRasConn[i].hrasconn;
retList.push_back(Obj);
}
}
//Deallocate memory for the connection buffer
HeapFree(GetProcessHeap(), 0, lpRasConn);
lpRasConn = NULL;
}

 
 

// 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 retList;
}



vector<CRasdilInfo> EnumRasConnections_win7()
{
    

    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
    dwRet = RasEnumConnections(lpRasConn, &dwCb, &dwConnections);
    vector<CRasdilInfo> retList;
    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);
        // 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);
                CRasdilInfo Obj;
                Obj.strEntryName = lpRasConn[i].szEntryName;
                Obj.strPhoneBook = lpRasConn[i].szPhonebook;
                Obj.hRasConn = lpRasConn[i].hrasconn;
                retList.push_back(Obj);
            }
        }
        //Deallocate memory for the connection buffer
        HeapFree(GetProcessHeap(), 0, lpRasConn);
        lpRasConn = NULL;
    }

    // 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  retList;
}

 

相关文章
|
11月前
|
IDE 开发工具 C++
[记录][问题]Win32调用C++/WinRT DLL
[记录][问题]Win32调用C++/WinRT DLL
|
开发工具 Android开发 C++
windows 8.1 x64位 adb 缺少api-ms-win-crt-private-l1-1-0.dll
windows 8.1 x64位 adb 缺少api-ms-win-crt-private-l1-1-0.dll
244 0
windows 8.1 x64位 adb 缺少api-ms-win-crt-private-l1-1-0.dll
|
安全 Windows
WIN10中如何关闭Windows Search
WIN10中如何关闭Windows Search
102 0
WIN10中如何关闭Windows Search
|
API Windows
Qt实用技巧:Qt中添加对windows api的支持,显示/隐藏任务栏和桌面(解决无法找到windows api)
Qt实用技巧:Qt中添加对windows api的支持,显示/隐藏任务栏和桌面(解决无法找到windows api)
windows激活报错 0xC004F074
windows激活报错 0xC004F074的案例分享
windows激活报错 0xC004F074
|
安全 Windows API
[转载]Windows NT/2000/XP下不用驱动的Ring0代码实现
大家知道,Windows NT/2000为实现其可靠性,严格将系统划分为内核模式与用户模式,在i386系统中分别对应CPU的Ring0与Ring3级别。Ring0下,可以执行特权级指令,对任何I/O设备都有访问权等等。
1322 0