ras api win7 和 win 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;
}
复制代码

 

相关文章
|
Java API 数据库
【编译原理+句柄+入栈顺序从右至左+系统调用+win api+程序安排+acm ieee usenix信息】答疑
【编译原理+句柄+入栈顺序从右至左+系统调用+win api+程序安排+acm ieee usenix信息】答疑
81 0
【编译原理+句柄+入栈顺序从右至左+系统调用+win api+程序安排+acm ieee usenix信息】答疑
|
API 开发工具 Windows
试来试去,WIN下最简单的WIN API开发工具,Pelles C就好啦
昨晚试过N个,不是太大,就是不容易和WIN API集成。 今早一试就灵了个。。。。 Pelles C。 Pelles C是一款windows下的C IDE,支持调试,且为免费。它有一个高效率的链接器,目前已被广泛采用为各种语言的后台链接器使用LCC作为编译器并且完整支持win32编程,支持任何API调用,包含所有winAPI的库且含有完整 C Runtime Library。
1281 0
|
14天前
|
缓存 前端开发 API
API接口封装系列
API(Application Programming Interface)接口封装是将系统内部的功能封装成可复用的程序接口并向外部提供,以便其他系统调用和使用这些功能,通过这种方式实现系统之间的通信和协作。下面将介绍API接口封装的一些关键步骤和注意事项。
|
21天前
|
监控 前端开发 JavaScript
实战篇:商品API接口在跨平台销售中的有效运用与案例解析
随着电子商务的蓬勃发展,企业为了扩大市场覆盖面,经常需要在多个在线平台上展示和销售产品。然而,手工管理多个平台的库存、价格、商品描述等信息既耗时又容易出错。商品API接口在这一背景下显得尤为重要,它能够帮助企业在不同的销售平台之间实现商品信息的高效同步和管理。本文将通过具体的淘宝API接口使用案例,展示如何在跨平台销售中有效利用商品API接口,以及如何通过代码实现数据的统一管理。
|
1月前
|
安全 算法 API
产品经理必备知识——API接口
前言 在古代,我们的传输信息的方式有很多,比如写信、飞鸽传书,以及在战争中使用的烽烟,才有了著名的烽火戏诸侯,但这些方式传输信息的效率终究还是无法满足高速发展的社会需要。如今万物互联的时代,我通过一部手机就可以实现衣食住行的方方面面,比如:在家购物、远程控制家电、自动驾驶等等,背后都离不开我们今天要聊的API接口。
|
1月前
|
数据采集 JSON API
如何实现高效率超简洁的实时数据采集?——Python实战电商数据采集API接口
你是否曾为获取重要数据而感到困扰?是否因为数据封锁而无法获取所需信息?是否因为数据格式混乱而头疼?现在,所有这些问题都可以迎刃而解。让我为大家介绍一款强大的数据采集API接口。
|
1月前
|
安全 API 数据安全/隐私保护
API接口知识小结
应用程序接口API(Application Programming Interface),是提供特定业务输出能力、连接不同系统的一种约定。这里包括外部系统与提供服务的系统(中后台系统)或后台不同系统之间的交互点。包括外部接口、内部接口,内部接口又包括:上层服务与下层服务接口、同级接口。
|
1天前
|
Java API Android开发
[NDK/JNI系列04] JNI接口方法表、基础API与异常API
[NDK/JNI系列04] JNI接口方法表、基础API与异常API
7 0
|
4天前
|
XML JSON API
快速淘宝商品详情页面API接口传输 php
PI(Application Programming Interface,应用程序接口)是一组预定义的函数、协议和工具,用于构建软件应用程序之间的交互。它允许不同的软件系统和应用通过统一的接口进行数据交换和通信