如何在Visual Studio 2005下配置WinPcap开发环境

来源:互联网 发布:c语言结构体数组 编辑:程序博客网 时间:2024/05/16 18:58

【为什么选择VS2005】
由于Visual C++ 6.0 中的一些库文件很老了,新版的WinPcap如果运行在6.0的IDE里面,其帮助文档上面的例程多数都运行不了,被编译器无情的抛出了无数的错误,为此,需要在有着新的库文件的更高版本的IDE下面来做开发,于是我选择了VS2005。

 

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

 

【配置步骤】
1、安装 winpcap:
如果没安装这个包,程序即使编译成功也不能运行,会提示找不到 winpcap.dll

2、下载  WinPcap Developer's Packs
  解压后会得一个目录WpdPack和五个子目录:
  -docs
  -Examples-pcap
  -Examples-remote
  -Include
  -Lib
  然后配置VC++:(WpdPackPath是目录WpdPack的绝对路径):
 (1)执行下面的操作打开配置对话框:工具->选项->项目和解决方案->VC++目录
 (2)在“显示以下内容的目录”下面选择“包含文件”,添加新的项:WpdPackPath/include
 (3)在“显示以下内容的目录”下面选择“库文件”,添加新的项:WpdPackPath/lib

3、新建一个C++项目工程,然后配置工程属性:
 (1)右键->属性->配置属性->C/C++->预处理器-> 预处理器定义,添加WPCAP。
 (2)右键->属性->配置属性->连接器->输入->附加依赖项,添加 wpcap.lib Packet.lib ws2_32.lib。

【下面举一个例子】

//列举出机器上面的所有网卡
#include "pcap.h"
#include "remote-ext.h"//不包含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;
}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/nicegiving/archive/2008/05/25/2478915.aspx

原创粉丝点击