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

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

  1. vector<CRasdilInfo> EnumAdslNames_win7(void)
  2. {
  3. vector<CRasdilInfo> retList;
  4. DWORD dwCb = ;
  5. DWORD dwRet = ERROR_SUCCESS;
  6. DWORD dwEntries = ;
  7. LPRASENTRYNAME lpRasEntryName = NULL;
  8. // Call RasEnumEntries with lpRasEntryName = NULL. dwCb is returned with the required buffer size and
  9. // a return code of ERROR_BUFFER_TOO_SMALL
  10. // 用lpRasEntryName = NULL 来调用 RasEnumEntries, 其中dwCb是一个传出值, 用来返回成功调用所需的缓冲区的字节数.
  11. dwRet = RasEnumEntries(NULL, NULL, lpRasEntryName, &dwCb, &dwEntries);
  12. // 函数成功返回0
  13. if (dwRet == ERROR_BUFFER_TOO_SMALL){
  14. // Allocate the memory needed for the array of RAS entry names.
  15. // 分配遍历条目所需要的字节输
  16. lpRasEntryName = (LPRASENTRYNAME) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCb);
  17. // 如果lpRasEntryName指针为NULL, 则说明分配内存失败
  18. if (lpRasEntryName == NULL){
  19. // cout << "HeapAlloc failed!" << endl;
  20. //cout << "分配内存失败! " << endl;
  21. return retList;
  22. }
  23. // The first RASENTRYNAME structure in the array must contain the structure size
  24. // 数组中第一个 RASENRTYNAME 结构必须包含结构体的大小
  25. lpRasEntryName[].dwSize = sizeof(RASENTRYNAME);
  26. // Call RasEnumEntries to enumerate all RAS entry names
  27. // 调用 RasEnumEntries 枚举所有的连接名称
  28. dwRet = RasEnumEntries(NULL, NULL, lpRasEntryName, &dwCb, &dwEntries);
  29.  
  30. // If successful, print the RAS entry names
  31. // 如果调用成功, 打印出每个连接的名称
  32. if (ERROR_SUCCESS == dwRet){
  33. // cout << "The following RAS entry names were found:" << endl;
  34. for (DWORD i = ; i < dwEntries; i++){
  35. //cout << i << " " << lpRasEntryName[i].szEntryName << endl;
  36. CRasdilInfo obj;
  37. obj.strEntryName = lpRasEntryName[i].szEntryName;
  38. obj.strPhoneBook = lpRasEntryName[i].szPhonebookPath;
  39. //GetRasParam(&obj.rasDialParam,obj.strPhoneBook.c_str(),obj.strEntryName.c_str());
  40. retList.push_back(obj);
  41. }
  42. }
  43. // Deallocate memory for the connection buffer
  44. // 释放用于存放连接名称的内存
  45. HeapFree(GetProcessHeap(), , lpRasEntryName);
  46. // 赋值空指针
  47. lpRasEntryName = NULL;
  48. }else {
  49. // There was either a problem with RAS or there are RAS entry names to enumerate
  50. // 枚举连接名称出现的问题
  51. if(dwEntries >= ){
  52. // cout << "The operation failed to acquire the buffer size." << endl;
  53. }else{
  54. // cout << "There were no RAS entry names found:." << endl;
  55.  
  56. }
  57. }
  58. return retList;
  59. }
  60.  
  61. vector<CRasdilInfo> EnumAdslNames_xp(void)
  62. {
  63. OutputDebugInfo("EnumAdslNames_xp");
  64. vector<CRasdilInfo> retList;
  65. DWORD dwCb = sizeof(RASENTRYNAME);
  66. DWORD dwRet = ERROR_SUCCESS;
  67. DWORD dwEntries = ;
  68.  
  69. RASENTRYNAME ras_entry_name = {};
  70. ras_entry_name.dwSize = sizeof(RASENTRYNAME);
  71. LPRASENTRYNAME lpRasEntryName = &ras_entry_name;
  72.  
  73. dwRet = RasEnumEntries(NULL, NULL, lpRasEntryName, &dwCb, &dwEntries);
  74. if(dwRet == ERROR_SUCCESS)
  75. dwRet = ERROR_BUFFER_TOO_SMALL;
  76.  
  77. if(dwRet != ERROR_BUFFER_TOO_SMALL && dwEntries > )
  78. {
  79. }
  80. else if(dwRet == ERROR_BUFFER_TOO_SMALL && dwEntries > )
  81. {
  82. if(dwCb < (dwEntries * sizeof(RASENTRYNAME)))
  83. dwCb = dwEntries * sizeof(RASENTRYNAME);
  84.  
  85. lpRasEntryName = (LPRASENTRYNAME) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCb);
  86. lpRasEntryName->dwSize = sizeof(RASENTRYNAME);
  87. dwRet = RasEnumEntries(NULL, NULL, lpRasEntryName, &dwCb, &dwEntries);
  88. if(dwRet == ERROR_SUCCESS)
  89. {
  90. for (DWORD i = ; i < dwEntries; i++){
  91. CRasdilInfo obj;
  92. obj.strEntryName = lpRasEntryName[i].szEntryName;
  93. obj.strPhoneBook = lpRasEntryName[i].szPhonebookPath;
  94. retList.push_back(obj);
  95. }
  96. }
  97. HeapFree(GetProcessHeap(), , lpRasEntryName);
  98. lpRasEntryName = NULL;
  99.  
  100. }
  101. return retList;
  102. }
  1.  

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;

  1.  

// Call RasEnumConnections to enumerate active connections
dwRet = RasEnumConnections(lpRasConn, &dwCb, &dwConnections);

  1.  

// 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;
}

  1.  

// 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;
}

  1.  
  2. vector<CRasdilInfo> EnumRasConnections_win7()
  3. {
  4.  
  5. DWORD dwCb = ;
  6. DWORD dwRet = ERROR_SUCCESS;
  7. DWORD dwConnections = ;
  8. LPRASCONN lpRasConn = NULL;
  9.  
  10. // Call RasEnumConnections with lpRasConn = NULL. dwCb is returned with the required buffer size and
  11. // a return code of ERROR_BUFFER_TOO_SMALL
  12. dwRet = RasEnumConnections(lpRasConn, &dwCb, &dwConnections);
  13. vector<CRasdilInfo> retList;
  14. if (dwRet == ERROR_BUFFER_TOO_SMALL){
  15. // Allocate the memory needed for the array of RAS structure(s).
  16. lpRasConn = (LPRASCONN) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCb);
  17. // The first RASCONN structure in the array must contain the RASCONN structure size
  18. lpRasConn[].dwSize = sizeof(RASCONN);
  19.  
  20. // Call RasEnumConnections to enumerate active connections
  21. dwRet = RasEnumConnections(lpRasConn, &dwCb, &dwConnections);
  22.  
  23. // If successful, print the names of the active connections.
  24. if (ERROR_SUCCESS == dwRet){
  25. wprintf(L"The following RAS connections are currently active:\n");
  26. for (DWORD i = ; i < dwConnections; i++){
  27. //wprintf(L"%s\n", lpRasConn[i].szEntryName);
  28. CRasdilInfo Obj;
  29. Obj.strEntryName = lpRasConn[i].szEntryName;
  30. Obj.strPhoneBook = lpRasConn[i].szPhonebook;
  31. Obj.hRasConn = lpRasConn[i].hrasconn;
  32. retList.push_back(Obj);
  33. }
  34. }
  35. //Deallocate memory for the connection buffer
  36. HeapFree(GetProcessHeap(), , lpRasConn);
  37. lpRasConn = NULL;
  38. }
  39.  
  40. // There was either a problem with RAS or there are no connections to enumerate
  41. if(dwConnections >= ){
  42. wprintf(L"The operation failed to acquire the buffer size.\n");
  43. }else{
  44. wprintf(L"There are no active RAS connections.\n");
  45. }
  46. return retList;
  47. }

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. python限制进程、子进程占用内存大小、CPU时间的方法:resource模块

    内置模块:resource 在mac环境下功能会存在问题.linux下可以使用:但是for i in range(10000)的值必须是10000或者更大的数值才有用.没有搞清楚为什么 #/usr/b ...

  2. Java实现将Excel导入数据库和从数据库中导出为Excel

    实现的功能: 用Java实现从Excel导入数据库,如果存在就更新 将数据库中的数据导出为Excel 1.添加jxl.jar mysql-connector-java.1.7-bin.jar包到项目的 ...

  3. OpenCV学习(29) 凸包(convexhull)

    在opencv中,通过函数convexHulll能很容易的得到一系列点的凸包,比如由点组成的轮廓,通过convexHull函数,我们就能得到轮廓的凸包.下面的图就是一些点集的凸包. 求凸包的代码如下: ...

  4. utf-8-validation

    https://leetcode.com/problems/utf-8-validation/ public class Solution { public boolean validUtf8(int ...

  5. cookie.setPath()的用法

    正常的cookie只能在一个应用中共享,即一个cookie只能由创建它的应用获得.1.可在同一应用服务器内共享方法:设置cookie.setPath("/");    本机tomc ...

  6. iOS开发-照片选择

    本来想做个注册登录的表单的,想想还是先做个简单的头像选择,一般情况下不管是内部管理系统还是面向公众的互联网公司,注册登录是免不了的,用户头像上传是免不了的,尤其是企业用户,上传了自己的图片才感觉自己买 ...

  7. Jump Game leetcode java

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  8. LRU Cache leetcode java

    题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  9. IntPtr 转 string

    假设有 intPtr pBuffer 方法一: 直接使用Marshal.PtrToStringAnsi方法: string ss = Marshal.PtrToStringAnsi(pBuffer); ...

  10. MongoDB学习笔记(六)--复制集+sharding分片 && 总结

    复制集+sharding分片                                                               背景 主机 IP 服务及端口 Server A ...