zoukankan      html  css  js  c++  java
  • 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: ");
    for (DWORD i = 0; i < dwConnections; i++){
    //OutputDebugInfo("EnumRasConnections_xp %s ", 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. ");
    }else{
    wprintf(L"There are no active RAS connections. ");
    }
    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:
    ");
                for (DWORD i = 0; i < dwConnections; i++){
                    //wprintf(L"%s
    ", 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.
    ");
        }else{
            wprintf(L"There are no active RAS connections.
    ");
        }
        return  retList;
    }
  • 相关阅读:
    JNI编程基础
    C语言指针学习
    C语言字符串以及二维数组指针
    CPP数据类型本质以及变量本质分析
    junit在idea中的使用(2)--实践篇
    idea创建maven项目
    SourceTree的基本使用---团队开发/参与开源
    SourceTree的基本使用---基本介绍/本地开发
    流量分析系统---启动流程
    流量分析系统---redis
  • 原文地址:https://www.cnblogs.com/zhangdongsheng/p/6839054.html
Copyright © 2011-2022 走看看