1.下载winpcap及开发包。
- winpcap下载地址:http://www.winpcap.org/install/default.htm
- winpcap开发包: http://www.winpcap.org/devel.htm
2. 安装winpcap
3. 解压winpcap开发包,将得到一个WpdPack目录,该目录中包含了5个子目录:docs、Examples-pcap、Examples-remote、Include和Lib。
- docs中是winpcap的帮助文档,比较通俗易懂。
- Examples的是一些例子
- include和lib则分别为C++项目的头文件和链接库
4.配置上inlucde和lib引用
5.在进行编程前还要注意一点,找到pcap.h文件,打开它,在#include <pcap/pcap.h>这句之前加上#define HAVE_REMOTE ,否则可能会报错说上面的结构体和函数都没有定义,看到网上有人说要加入#include "remote-ext.h" ,但其实加上以后会出现#error : Please do not include this file directly. Just define HAVE_REMOTE and then include pcap.h 意思是说不要包含这个头文件,而要定义HAVE_REMOTE宏
6.
#include "pcap.h"
#include "stdio.h"
int main(int argc,char *argv[])
{
pcap_if_t *alldevs; //define the list of the Network adapter device
pcap_if_t *d;
int i=0;
char errbuf[PCAP_ERRBUF_SIZE]; //PCAP_ERRBUF_SIZE is 256
if(pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&alldevs,errbuf))
{
fprintf(stderr,"Error in pcap_findalldevs_ex",errbuf); /
exit(1);
}
for(d=alldevs;d;d=d->next)
{
printf("%d.%s/t",++i,d->name);
if(d->description)
{
printf("%s/n",d->description);
}
else
{
printf("No description available");
}
}
if(i==0)
{
printf("No interface found!! Make sure Winpcap is installed/n");
return -1;
}
pcap_freealldevs(alldevs);
return 0;
}
该程序在我的电脑上正确运行,编译环境vc++6.0 ,运行结果应该是显示出你所有的网卡设备。
如果出现“error C2144: syntax error : missing ';' before type 'unsigned int'”,“fatal error C1004: unexpected end of file found”的错误,把_W64删掉就可以编译成功。