WinPcap 常见安装和运行错误

来源:互联网 发布:华为升级软件 编辑:程序博客网 时间:2024/04/30 14:03
Visual Studio 2005 Express 下的 WinPcap 配置

1、安装 Visual Studio 2005 Express Edition 和 Paltform SDK。

如 何安装Visual Studio 2005 Express在这里就不赘述了,很简单的。由于VC Express没有自带 Platform SDK,所以需要自己下载安装(如果不安装 psdk的话,就会出现 找不到 winsock2.h 的编译错误)。由于微软现在官网提供的psdk下载比较麻烦,需要windows正版验证,再加上体积比较大,所以我这里就不用,我用的psdk是在这里 下载的:
XPSP2 PSDK Full Download with Local Install
还有一个,不知道能不能安装在xp上,有兴趣的兄弟可以自己试试
Windows Server 2003 PSDK Full Download with Local Install
似乎这两个链接在官网上是找不到的
下载、解压、安装,然后再配置 VC++:
tools --> options --> Projects and Solutions --> VC++ Directories   : 把以下路径添加到相应的下拉节点中去:(其中psdk是你的sdk安装目录)
  • Executalbe files :psdkdir/Bin

  • Include files :psdkdir/include

  • Library files:psdkdir/lib

2、安装 winpcap:到这里下载 winpcap
安装后按要求重启,如果没安装这个包,程序即使编译成功也不能运行,会提示找不到 winpcap.dll

3、下载 WinPcap Developer's Packs
解压后会得一个目录WpdPack四个子目录:
docs
Examples-pcap
Examples-remote
Include
Lib
然后配置VC++
tools --> options --> Projects and Solutions --> VC++ Directories :
  • Include files :WpdPackPath/include

  • Library files: WpdPackPath/lib

其中 WpdPackPath是目录WpdPack的绝对路径

4、新建一个 win32->win32 console application 工程,然后配置工程属性:
  • 右 键 -> Properties -> Configuration Properties -> C/C++ -> Preprocessor -> Proprocessor Definition   往上面添加 WPCAP就可以了(这一步不做似乎也没什么问题~)
  • 右键 -> Properties -> Configuration Properties -> Linker -> input -> Additional Dependencies 往上面添加 wpcap.lib Packet.lib
5、一个例子:

#include "pcap.h"
#include
"remote-ext.h"

int main()
{
     pcap_if_t
*alldevs;
     pcap_if_t
*d;
    
int i=0;
    
char errbuf[PCAP_ERRBUF_SIZE];
    
    
/* Retrieve the device list from the local machine */
    
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL /* auth is not needed */, &alldevs, errbuf) == -1)
     {
         fprintf(stderr,
"Error in pcap_findalldevs_ex: %s/n", errbuf);
         exit(
1);
     }
    
    
/* Print the list */
    
for(d= alldevs; d != NULL; d= d->next)
     {
         printf(
"%d. %s", ++i, d->name);
        
if (d->description)
             printf(
" (%s)/n", d->description);
        
else
             printf(
" (No description available)/n");
     }
    
    
if (i == 0)
     {
         printf(
"/nNo interfaces found! Make sure WinPcap is installed./n");
        
return 1;
     }

    
/* We don't need any more the device list. Free it */
     pcap_freealldevs(alldevs);

    
return 0;
}
注意,如果不添加 #include "remote-ext.h" 也是会报错的~

6、链接错误:
anothertest.obj : error LNK2019: unresolved external symbol __imp__WSASetLastError@4 referenced in function _WspiapiGetNameInfo@28
anothertest.obj : error LNK2019: unresolved external symbol __imp__inet_ntoa@
4 referenced in function _WspiapiLegacyGetAddrInfo@16
anothertest.obj : error LNK2019: unresolved external symbol __imp__htonl@
4 referenced in function _WspiapiLegacyGetAddrInfo@16
anothertest.obj : error LNK2019: unresolved external symbol __imp__getservbyname@
8 referenced in function _WspiapiLegacyGetAddrInfo@16
anothertest.obj : error LNK2019: unresolved external symbol __imp__htons@
4 referenced in function _WspiapiLegacyGetAddrInfo@16
anothertest.obj : error LNK2019: unresolved external symbol __imp__inet_addr@
4 referenced in function _WspiapiParseV4Address@8
anothertest.obj : error LNK2019: unresolved external symbol __imp__WSAGetLastError@
0 referenced in function _WspiapiQueryDNS@24
anothertest.obj : error LNK2019: unresolved external symbol __imp__gethostbyname@
4 referenced in function _WspiapiQueryDNS@24
anothertest.obj : error LNK2019: unresolved external symbol __imp__gethostbyaddr@
12 referenced in function _WspiapiLegacyGetNameInfo@28
anothertest.obj : error LNK2019: unresolved external symbol __imp__getservbyport@
8 referenced in function _WspiapiLegacyGetNameInfo@28
anothertest.obj : error LNK2019: unresolved external symbol __imp__ntohs@
4 referenced in function _WspiapiLegacyGetNameInfo@28
解决该问题,需要只需把ws2_32.lib添加到wpcap.lib Packet.lib后面(见上面第4条)
http://www.blogjava.net/JafeLee/archive/2007/09/27/148719.html
-----------------------------------------------------------
网络编程学习日记(1)_WinPcap和VC的配置
2008-08-21 13:34

第一个简单程序是获取网卡适配器信息,里面一个函数pcap_findalldevs_ex找不到,网上资料说是因为这个函数属于远程网卡获取,它的声明在#include "remote-ext.h"里面,加上去就可以运行了。

第二个程序错误更多,

F:/学习/网络/WiPcap/08_08_20/2/Cpp1.cpp(155) : error C2065: 'socklen_t' : undeclared identifier
F:/学习/网络/WiPcap/08_08_20/2/Cpp1.cpp(155) : error C2146: syntax error : missing ';' before identifier 'sockaddrlen'
F:/学习/网络/WiPcap/08_08_20/2/Cpp1.cpp(155) : error C2065: 'sockaddrlen' : undeclared identifier
F:/学习/网络/WiPcap/08_08_20/2/Cpp1.cpp(164) : error C2065: 'getnameinfo' : undeclared identifier
F:/学习/网络/WiPcap/08_08_20/2/Cpp1.cpp(170) : error C2065: 'NI_NUMERICHOST' : undeclared identifier

错误根由是因为WinPcap支持ipv6,而VC的winsock2.h太老了,很多结构都没有被支持,所以产生错误。网上有人说可以用VS新版本调试,我电脑里面是VS2008,但是仍然产生错误

_vsnprintf属性与生命不匹配,网上没有合适的解决方案,我感觉是WinPcap的某个头文件与stdio.h参数不匹配。

既然VS也无法通过,我实在没能力去修改WinPcap的头文件,所以就用最后一个方案,使用VC6.0的最新的PlatForm SDK开发包,里面包含了新的头文件,就可以支持WinPcap了。

有人说PSDK只有Windows2003的版本,我在微软里面找到了WinXPSP2的PSDK。

网址如下

http://www.microsoft.com/msdownload/platformsdk/sdkupdate/XPSP2FULLInstall.htm

里面好几个Cab,网页里面有完整的安装说明。

我正在下,搞好之后如果能用再说。

弄好了,下面是PlatFormSDK安装步骤

(1)安装过程:

CMD运行PSDK-FULL.bat,参数为一个目录,里面会被解压缩安装包,然后Setup,一路Next就可以了。

(2)配置过程

打开Visual C++6.0,在选项里面连接,把PSDK安装后的include和lib加入相应的位置。

特别注意,要把这些目录的顺序调高,我直接放到了最高层去了。

呵呵,编译一下,通过了,好Happy啊。

http://hi.baidu.com/alswl/blog/item/4bffbb0ae9e6d838b1351da7.html

-=-------------------------------------------------------

测试成功的代码:

#pragma comment(lib,"wpcap.lib")
#include <iostream>
using namespace std;

#include "pcap.h"
#include "remote-ext.h"


main()
{
    pcap_if_t *alldevs;
    pcap_if_t *d;
    int i=0;
    char errbuf[PCAP_ERRBUF_SIZE];
   
    /* »ñÈ¡±¾µØ»úÆ÷É豸Áбí */
//    if (pcap_findalldevs(&alldevs, errbuf) == -1)

   if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL /* auth is not needed */, &alldevs, errbuf) == -1)
    {
        fprintf(stderr,"Error in pcap_findalldevs_ex: %s/n", errbuf);
        exit(1);
    }
   
    /* ´òÓ¡Áбí */
    for(d= alldevs; d != NULL; d= d->next)
    {
        printf("%d. %s", ++i, d->name);
        if (d->description)
            printf(" (%s)/n", d->description);
        else
            printf(" (No description available)/n");
    }
   
    if (i == 0)
    {
        printf("/nNo interfaces found! Make sure WinPcap is installed./n");
        return 0;
    }

    /* ²»ÔÙÐèÒªÉ豸ÁбíÁË£¬ÊÍ·ÅËü */
    pcap_freealldevs(alldevs);
    return 0;
}

------------------------------------------------

说明:WinPcap共有安装程序和开发包
如果只运行基于WinPcap的应用程序,只须安装WinPcap安装程序;
如果要编写和调试基于WinPcap的应用程序,除安装WinPcap,还须指定开发包中的头文件目录(Include)和

静态链接库目录(Lib)

 

1.运行WinPcap 程序,出现"无法找到组件"对话框错误:


没有找到wpcap.dll,因此这个应用程序未能启动。重新安装应用程序可能会修复此问题。

解决方法:安装WinPcap

 

2.在VC6.0中编译WinPcap程序,出现下面错误:
fatal error C1083: Cannot open include file: 'pcap.h': No such file or directory
解决方法

菜单Tool->Options->Directories选项卡->Show directories->Include files,选择WinPcap中的开发包目录中的Include目录


3.Build 基于WinPcap的应用程序,出现链接错误:
Linking...
LINK : fatal error LNK1104: cannot open file "wpcap.lib"
Error executing link.exe.

解决方法
菜单Tool->Options->Directories选项卡->Show directories->Library files,选择WinPcap中的开发包

目录中的Lib目录

4.Build 基于WinPcap的应用程序,出现链接错误:

Linking...
arpcap.obj : error LNK2001: unresolved external symbol _pcap_loop
arpcap.obj : error LNK2001: unresolved external symbol _pcap_close
arpcap.obj : error LNK2001: unresolved external symbol _pcap_setfilter
arpcap.obj : error LNK2001: unresolved external symbol _pcap_geterr
arpcap.obj : error LNK2001: unresolved external symbol _pcap_compile
arpcap.obj : error LNK2001: unresolved external symbol _pcap_freealldevs
arpcap.obj : error LNK2001: unresolved external symbol _pcap_open_live
arpcap.obj : error LNK2001: unresolved external symbol _pcap_findalldevs
Release/ArpCap.exe : fatal error LNK1120: 8 unresolved externals
Error executing link.exe.

 

解决方法
方法1:#pragma comment(lib,"wpcap.lib")
方法2:菜单Project->Settings->Link->Objects/library modules,按End键,输入" wpcap.lib",注意

wpcap.lib 前面有空格

http://blog.csdn.net/zhangyang0402/archive/2009/01/15/3789718.aspx

---------------------------------------------------------------------------------------------

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

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

呵呵

当然,这只是测试代码

http://blog.csdn.net/bingdian37/archive/2006/08/07/1034873.aspx

 

转自:http://hi.baidu.com/kuangxiangjie/blog/item/19e2c23f7505a7ca7c1e7160.html