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信息】答疑
112 0
【编译原理+句柄+入栈顺序从右至左+系统调用+win api+程序安排+acm ieee usenix信息】答疑
|
测试技术 API Windows
利用global API hooks在Win7系统下隐藏进程
本文讲的是利用global API hooks在Win7系统下隐藏进程,在之前的文章《Powershell tricks::Hide Process by kd.exe》介绍过通过kd.exe隐藏进程的技巧,最大的缺点是需要开启Local kernel debugging模式,等待重启才能生效
1798 0
|
API 开发工具 Windows
试来试去,WIN下最简单的WIN API开发工具,Pelles C就好啦
昨晚试过N个,不是太大,就是不容易和WIN API集成。 今早一试就灵了个。。。。 Pelles C。 Pelles C是一款windows下的C IDE,支持调试,且为免费。它有一个高效率的链接器,目前已被广泛采用为各种语言的后台链接器使用LCC作为编译器并且完整支持win32编程,支持任何API调用,包含所有winAPI的库且含有完整 C Runtime Library。
1391 0
|
17天前
|
JSON API 数据格式
淘宝 / 天猫官方商品 / 订单订单 API 接口丨商品上传接口对接步骤
要对接淘宝/天猫官方商品或订单API,需先注册淘宝开放平台账号,创建应用获取App Key和App Secret。之后,详细阅读API文档,了解接口功能及权限要求,编写认证、构建请求、发送请求和处理响应的代码。最后,在沙箱环境中测试与调试,确保API调用的正确性和稳定性。
|
29天前
|
供应链 数据挖掘 API
电商API接口介绍——sku接口概述
商品SKU(Stock Keeping Unit)接口是电商API接口中的一种,专门用于获取商品的SKU信息。SKU是库存量单位,用于区分同一商品的不同规格、颜色、尺寸等属性。通过商品SKU接口,开发者可以获取商品的SKU列表、SKU属性、库存数量等详细信息。
|
1月前
|
JSON API 数据格式
店铺所有商品列表接口json数据格式示例(API接口)
当然,以下是一个示例的JSON数据格式,用于表示一个店铺所有商品列表的API接口响应
|
2月前
|
编解码 监控 API
直播源怎么调用api接口
调用直播源的API接口涉及开通服务、添加域名、获取API密钥、调用API接口、生成推流和拉流地址、配置直播源、开始直播、监控管理及停止直播等步骤。不同云服务平台的具体操作略有差异,但整体流程简单易懂。
|
20天前
|
JSON API 数据安全/隐私保护
拍立淘按图搜索API接口返回数据的JSON格式示例
拍立淘按图搜索API接口允许用户通过上传图片来搜索相似的商品,该接口返回的通常是一个JSON格式的响应,其中包含了与上传图片相似的商品信息。以下是一个基于淘宝平台的拍立淘按图搜索API接口返回数据的JSON格式示例,同时提供对其关键字段的解释
|
2月前
|
人工智能 自然语言处理 PyTorch
Text2Video Huggingface Pipeline 文生视频接口和文生视频论文API
文生视频是AI领域热点,很多文生视频的大模型都是基于 Huggingface的 diffusers的text to video的pipeline来开发。国内外也有非常多的优秀产品如Runway AI、Pika AI 、可灵King AI、通义千问、智谱的文生视频模型等等。为了方便调用,这篇博客也尝试了使用 PyPI的text2video的python库的Wrapper类进行调用,下面会给大家介绍一下Huggingface Text to Video Pipeline的调用方式以及使用通用的text2video的python库调用方式。