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信息】答疑
82 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模式,等待重启才能生效
1753 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。
1283 0
|
21天前
|
SQL API Python
Python DB API下规范下cursor对象常用接口
Python DB API下规范下cursor对象常用接口。
16 4
|
4天前
|
存储 缓存 运维
DataWorks操作报错合集之DataWorks根据api,调用查询文件列表接口报错如何解决
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
13 1
|
4天前
|
SQL 数据管理 API
数据管理DMS产品使用合集之阿里云DMS提供API接口来进行数据导出功能吗
阿里云数据管理DMS提供了全面的数据管理、数据库运维、数据安全、数据迁移与同步等功能,助力企业高效、安全地进行数据库管理和运维工作。以下是DMS产品使用合集的详细介绍。
|
4天前
|
运维 Serverless API
Serverless 应用引擎产品使用之在阿里函数计算中开启函数计算 API 接口如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
103 6
|
7天前
|
前端开发 Java 测试技术
IDEA 版 API 接口神器来了,一键生成文档,贼香!
IDEA 版 API 接口神器来了,一键生成文档,贼香!
20 0
|
8天前
|
API 开发者
邮件API接口使用的方法和步骤
AOKSEND指南:了解和使用邮件API接口,包括选择适合的接口(如AOKSEND、Mailgun、SMTP),获取访问权限,配置发件人、收件人及邮件内容,调用接口发送邮件,并处理返回结果,以高效集成邮件功能。