代码执行效果如下图:
代码如下:
#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 中编译调试通过!