由于在调用RasEnumEntries和RasEnumConnections在xp和win7以上的操作系统中有所不同,所以在win7下正常的代码在xp不一定就可以。

主要是在win7 下可以给参数传NULL来得到所需要大小,而在xp下则不可以传NULL,在xp下只需要传一个对象的大小,然后得到所需大小。再进行分配存储空间,再进行遍历 。废话不说了,直接上代码了。

vector<CRasdilInfo> EnumAdslNames_win7(void)
{
vector<CRasdilInfo> retList;
DWORD dwCb = ;
DWORD dwRet = ERROR_SUCCESS;
DWORD dwEntries = ;
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[].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 = ; 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(), , lpRasEntryName);
// 赋值空指针
lpRasEntryName = NULL;
}else {
// There was either a problem with RAS or there are RAS entry names to enumerate
// 枚举连接名称出现的问题
if(dwEntries >= ){
// 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 = ; RASENTRYNAME ras_entry_name = {};
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 > )
{
}
else if(dwRet == ERROR_BUFFER_TOO_SMALL && dwEntries > )
{
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 = ; i < dwEntries; i++){
CRasdilInfo obj;
obj.strEntryName = lpRasEntryName[i].szEntryName;
obj.strPhoneBook = lpRasEntryName[i].szPhonebookPath;
retList.push_back(obj);
}
}
HeapFree(GetProcessHeap(), , 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 = ;
DWORD dwRet = ERROR_SUCCESS;
DWORD dwConnections = ;
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[].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 = ; 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(), , lpRasConn);
lpRasConn = NULL;
} // There was either a problem with RAS or there are no connections to enumerate
if(dwConnections >= ){
wprintf(L"The operation failed to acquire the buffer size.\n");
}else{
wprintf(L"There are no active RAS connections.\n");
}
return retList;
}

ras api win7 和 win xp 遍历时的不同的更多相关文章

  1. vs2012编译在win7 32位电脑和win xp电脑上运行的win32程序遇到的问题记录

    一.win7 32位电脑: vs2012编译的64位程序是没有问题的.但编译的32位程序在别的电脑(虚拟机模拟)出错: 感觉很无语,vs这么牛逼的东西,在设计时候都不考虑这些吗? 在自己电脑C:\Wi ...

  2. win xp 安装 VS2010 时要重启是因为没安装WINDOWS INSTALLER 4.5

    win xp 安装 VS2010 时要重启是因为没安装WINDOWS INSTALLER 4.5. 无意间看到VS2010安装列表中有一项是 WINDOWS INSTALLER 4.5 . 装这个玩意 ...

  3. SSL握手中win xp和SNI的那点事

    SSL握手中win xp和SNI的那点事 一.背景需求server1-3使用不同的域名对外提供https服务,用nginx作为前端负载均衡器并负责https集中解密工作(以用户访问的域名为依据进行流量 ...

  4. Win7与Ubuntu双系统时卸载Ubuntu的方法

    Win7与Ubuntu双系统时卸载Ubuntu的方法 [日期:2010-03-26] 来源:Ubuntu社区  作者:Ubuntu编辑 [字体:大 中 小]       1. 下载MBRFix工具,放 ...

  5. WIN XP蓝屏代码大全

    转自:廊坊师范学院信息技术提高班---韩正阳 http://blog.csdn.net/jiudihanbing WIN XP蓝屏代码大全WIN XP蓝屏代码大全一.蓝屏含义 1.故障检查信息 *** ...

  6. 【原理探究】女朋友问我ArrayList遍历时删除元素的正确姿势是什么?

    简介 我们在项目开发过程中,经常会有需求需要删除ArrayList中的某个元素,而使用不正确的删除方式,就有可能抛出异常.或者在面试中,会遇到面试官询问遍历时如何正常删除元素.所以在本篇文章中,我们会 ...

  7. java 集合list遍历时删除元素

    本文探讨集合在遍历时删除其中元素的一些注意事项,代码如下 import java.util.ArrayList; import java.util.Iterator; import java.util ...

  8. Java遍历时删除List、Set、Map中的元素(源码分析)

    在对List.Set.Map执行遍历删除或添加等改变集合个数的操作时,不能使用普通的while.for循环或增强for.会抛出ConcurrentModificationException异常或者没有 ...

  9. 【Java】List遍历时删除元素的正确方式

    当要删除ArrayList里面的某个元素,一不注意就容易出bug.今天就给大家说一下在ArrayList循环遍历并删除元素的问题.首先请看下面的例子: import java.util.ArrayLi ...

随机推荐

  1. HTTP 无状态啊无状态啊

    无状态的根本原因 根本原因是:因为,HTTP协议使用的是Socket套接字TCP连接的,每次监听到的套接字连接是不可能一个个保存起来的.(很消耗资源,假如一个人服务器只保存一个通信连接,一万个岂不是要 ...

  2. Unity3d-Particle System系统的学习(三)

    这节课我们来实战下上几节讲的几乎所有Particle System用到的参数. 我们今天制作下图所示的粒子: 类似于带有光晕的魔法球.用到的材质也就是上节课用到的材质贴图. http://pan.ba ...

  3. Android图片加载框架最全解析(四),玩转Glide的回调与监听

    大家好,今天我们继续学习Glide. 在上一篇文章当中,我带着大家一起深入探究了Glide的缓存机制,我们不光掌握了Glide缓存的使用方法,还通过源码分析对缓存的工作原理进行了了解.虽说上篇文章和本 ...

  4. 实现Hadoop的Writable接口Implementing Writable interface of Hadoop

    As we saw in the previous posts, Hadoop makes an heavy use of network transmissions for executing it ...

  5. mybatis映射文件遇到的小问题

    mybatis的映射文件插入操作时: 如果对应的属性是String类型的,那么一定要做空串的判断. 比如注册的时候,如果需要向数据库中插入一条记录时,对应的字段没有给他赋值,这个String类型的值传 ...

  6. 原生js获取宽高与jquery获取宽高的方法的关系

    说明:1.因为获取高度的情况跟获取宽度的情况一样,所以以下只说获取宽度的情况.  2.以下所说的所有方法与属性所返回的值都是不带单位的.  3.为了方便说明,以下情况采用缩写表示:  obj -> ...

  7. DIV-CSS布局中position属性详解

    本文向大家描述一下DIV CSS布局中的position属性的用法,position属性主要有四种属性值,任何元素的默认position的属性值均是static,静态.这节课主要讲讲relative( ...

  8. 在asp.net中使用jQuery实现类似QQ网站的图片切割效果

    今天要给大家介绍一个asp.net结合jQuery来切割图片的小程序,原理很简单,大致流程是: 加载原图 --> 用矩形框在原图上选取区域并将选取的顶点坐标和矩形尺寸发送至服务器 --> ...

  9. jQuery图片上传前先在本地预览

    js代码: /* *名称:图片上传本地预览插件 v1.1 *作者:周祥 *时间:2013年11月26日 *介绍:基于JQUERY扩展,图片上传预览插件 目前兼容浏览器(IE 谷歌 火狐) 不支持saf ...

  10. Android 色彩设计理念

    色彩 色彩从当代建筑.路标.人行横道以及运动场馆中获取灵感.由此引发出大胆的颜色表达激活了色彩,与单调乏味的周边环境形成鲜明的对照. 强调大胆的阴影和高光.引出意想不到且充满活力的颜色. 色样 – 0 ...