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

在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()函数吧

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

呵呵

posted @ 2009-10-17 20:41  荷包蛋  阅读(12127)  评论(1编辑  收藏  举报