C++ RasDial之调用RasEnumEntries遍历系统中存在的宽带连接名称

代码执行效果如下图:


代码如下:

#include <windows.h>
#include <iostream>
#include "ras.h"
#include "raserror.h"
#pragma comment(lib, "rasapi32.lib")

using namespace std;

DWORD  wmain(){	
    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 0;
        }	
        // 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;
            cout << "遍历出的连接的名称:" << endl;
            for (DWORD i = 0; i < dwEntries; i++){
                cout << i << "    " << lpRasEntryName[i].szEntryName << endl;
            }
        }		
        // 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;
            cout << "以上操作未能获取缓冲区的大小. " << endl;
        }else{			
            // cout << "There were no RAS entry names found:." << endl;
            cout << "系统中没有RAS连接. " << endl;
        }
    }
    cin.get ();
    return 0;
}

其中注释中文为注释英文的译文,自己翻译的。

本代码在Win7 + Vc6.0 中编译调试通过!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值