Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > 使用pcap_findalldevs_ex获取设备列表遇到的问题
【标  题】:使用pcap_findalldevs_ex获取设备列表遇到的问题
【关键字】:pcap_findalldevs_ex
【来  源】:http://blog.csdn.net/bingdian37/archive/2006/08/07/1034873.aspx

使用pcap_findalldevs_ex获取设备列表遇到的问题

Your Ad Here

在winpcap3.1beta4的文档中

有个获取设备列表的例子程序

其中使用到了pcap_findalldevs_ex()函数,在文档中是这样介绍这个函数的:

This function is a superset of the old 'pcap_findalldevs()', which is obsolete, and which allows listing only the devices present on the local machine.  Vice versa, pcap_findalldevs_ex() allows listing the devices present on a remote machine as well.

简单说pcap_findalldevs_ex()是pcap_findalldevs()的一个超集, 他不仅可以获取本地的设备列表,还可以获取远程计算机的社别列表,但是在将pcap_findalldevs()换成pcap_findalldevs_ex()的过程中却出现了意想不到的错误

 

 

 

#include <cstdlib>
#include 
<iostream>
#include 
<pcap.h>
using namespace std;

int main(int argc, char *argv[])
{
    pcap_if_t 
*alldevs;
    pcap_if_t 
*d;
    
int i=0;
    
char errbuf[PCAP_ERRBUF_SIZE];
    
    
if(pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&alldevs,errbuf)==0){
         
while(!(alldevs->next==NULL)){
               cout
<<"设备"<<i<<"的名称"<<(alldevs->name)<<endl;
               cout
<<"设备"<<i<<"的描述"<<(alldevs->description)<<endl;
               alldevs
=alldevs->next;
               i
++;                                                              
          }
    }    
    system(
"PAUSE");
    
return EXIT_SUCCESS;
}

 使用dev c++编译,出现以下错误

      F:\IT学习\c++\301\main.cpp In function `int main(int, char**)':
14 F:\IT学习\c++\301\main.cpp `PCAP_SRC_IF_STRING' undeclared (first use this function)
      (Each undeclared identifier is reported only once for each function it appears in.)
14 F:\IT学习\c++\301\main.cpp `pcap_findalldevs_ex' undeclared (first use this function)
       F:\IT学习\c++\301\Makefile.win [Build Error]  [main.o] Error 1

在网上查了一下,有人说这是wincap的一个失误,忘记把该函数的声明文件包含进去了

我打开pcap.h看了一下,确实没有pcap_findalldevs_ex函数的声明

不死心

找个文本搜索工具,在dev c++的include文件夹中搜索pcap_findalldevs_ex,[在此之前我已经把wincap的头文件全部拷入了该目录,呵呵,有些.....]

结果真让我查到了,该函数的名称在remote-ext.h找到了

我看了一下,嘿嘿,就是他了

该代码,包含该文件

 

#include <cstdlib>
#include 
<iostream>
#include 
<pcap.h>
#include 
<remote-ext.h>
using namespace std;

int main(int argc, char *argv[])
{
    pcap_if_t 
*alldevs;
    pcap_if_t 
*d;
    
int i=0;
    
char errbuf[PCAP_ERRBUF_SIZE];
    
    
if(pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&alldevs,errbuf)==0){
         
while(!(alldevs->next==NULL)){
               cout
<<"设备"<<i<<"的名称"<<(alldevs->name)<<endl;
               cout
<<"设备"<<i<<"的描述"<<(alldevs->description)<<endl;
               alldevs
=alldevs->next;
               i
++;                                                              
          }
    }    
    system(
"PAUSE");
    
return EXIT_SUCCESS;
}

再次编译,竟然还有错误

 F:\IT学习\c++\301\main.o(.text+0x15b) In function `main':
      [Linker error] undefined reference to `pcap_findalldevs_ex'
 F:\IT学习\c++\301\main.o(.text+0x15b) ld returned 1 exit status
 F:\IT学习\c++\301\Makefile.win [Build Error]  [工程1.exe] Error 1

想起来了,使用dev c++的时候常遇到的问题

加载dll

打开工程选项->参数->连接器->加入库或者对象

找到wpcap.lib,确定

然后再编译,通过

这是我机器上的运行结果

设备0的名称rpcap://\Device\NPF_GenericNdisWanAdapter
设备0的描述Network adapter 'Generic NdisWan adapter' on local host
设备1的名称rpcap://\Device\NPF_{CA1F1FE1-700F-45B4-BC50-423BB0784A86}
设备1的描述Network adapter 'NET IP/1394 Miniport' on local host
请按任意键继续. . .

呵呵,有些乱啊

好在通过了

现在看来,如果使用不到pcap_findalldevs_ex的高级特性的化

仅仅是想获取一下设备列表的话

还是使用原先的pcap_findalldevs()函数吧

简单易用,相信也不会发生这样的错误

呵呵

 

[转]C++ 学习指南-这篇文章真是太经典了:【上一篇】
孔乙己之五----虚函数(下):【下一篇】
【相关文章】
没有相关文章
【随机文章】
  • 传奇世界 传世里的最新骗术
  • Linux的安装(redhat)
  • 用PHP发送MIME邮件(三)
  • 实验三 静态路由设置
  • 软件开发规范(2)
  • C++中new_handler
  • 使用准则进行条件查询--1.1.指定一个准则
  • C趣味编程百例(11)
  • MySQL数据导入导出方法与工具介绍之一
  • 入侵与攻击漏洞资料
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.