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

本文介绍了使用WinPcap库中的pcap_findalldevs_ex函数获取本地及远程设备列表的过程,包括解决编译错误的方法及最终成功运行的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

在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==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==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_{FE74219E-5D08-45CA-8EFF-19CEA31C26AA}

设备1的描述Network adapter 'Broadcom NetXtreme Gigabit Ethernet Driver' on local

 host

请按任意键继续. . .

呵呵,有些乱啊

好在通过了

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

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

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

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

呵呵

 当然,这只是测试代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值