Winpcap实现一配置项目→属性→配置属性→C/C++→在右边的包含目录里添加你winpcap文件夹里include文件夹所在的位置;项目→属性→配置属性→在点击链接器选项,在右边的附加库目录里添加lib文件夹所在的位置;项目→属性→配置属性→C/C++→预处理定义,WPCAP;HAVE_REMOTE;项目→属性→配置属性→链接器→输入,在右边附加依赖项里添加上:wpcap.libws2_32.lib。1获取适配器列表#includepcap.hint_tmain(intargc,_TCHAR*argv[]){pcap_if_t*allAdapters;//适配器列表pcap_if_t*adapter;charerrorBuffer[PCAP_ERRBUF_SIZE];//错误信息缓冲区if(pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&allAdapters,errorBuffer)==-1){//检索机器连接的所有网络适配器fprintf(stderr,Errorinpcap_findalldevs_exfunction:%s\n,errorBuffer);return-1;}if(allAdapters==NULL){//不存在人任何适配器printf(\nNoadaptersfound!MakesureWinPcapisinstalled.\n);return0;}intcrtAdapter=0;for(adapter=allAdapters;adapter!=NULL;adapter=adapter-next){//遍历输入适配器信息(名称和描述信息)printf(\n%d.%s,++crtAdapter,adapter-name);printf(--%s\n,adapter-description);}printf(\n);pcap_freealldevs(allAdapters);//释放适配器列表system(PAUSE);return0;}运行结果2打开指定适配器并捕获数据包#includepcap.hint_tmain(intargc,_TCHAR*argv[]){pcap_if_t*allAdapters;//适配器列表pcap_if_t*adapter;pcap_t*adapterHandle;//适配器句柄structpcap_pkthdr*packetHeader;constu_char*packetData;charerrorBuffer[PCAP_ERRBUF_SIZE];//错误信息缓冲区if(pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&allAdapters,errorBuffer)==-1){//检索机器连接的所有网络适配器fprintf(stderr,Errorinpcap_findalldevs_exfunction:%s\n,errorBuffer);return-1;}if(allAdapters==NULL){//不存在任何适配器printf(\nNoadaptersfound!MakesureWinPcapisinstalled.\n);return0;}intcrtAdapter=0;for(adapter=allAdapters;adapter!=NULL;adapter=adapter-next){//遍历输入适配器信息(名称和描述信息)printf(\n%d.%s,++crtAdapter,adapter-name);printf(--%s\n,adapter-description);}printf(\n);//选择要捕获数据包的适配器intadapterNumber;printf(Entertheadapternumberbetween1and%d:,crtAdapter);scanf_s(%d,&adapterNumber);if(adapterNumber1||adapterNumbercrtAdapter){printf(\nAdapternumberoutofrange.\n);//释放适配器列表pcap_freealldevs(allAdapters);return-1;}adapter=allAdapters;for(crtAdapter=0;crtAdapteradapterNumber-1;crtAdapter++)adapter=adapter-next;//打开指定适配器adapterHandle=pcap_open(adapter-name,//nameoftheadapter65536,//portionofthepackettocapture//65536guaranteesthatthewhole//packetwillbecapturedPCAP_OPENFLAG_PROMISCUOUS,//promiscuousmode1000,//readtimeout-1millisecondNULL,//authenticationontheremotemachineerrorBuffer//errorbuffer);if(adapterHandle==NULL){//指定适配器打开失败fprintf(stderr,\nUnabletoopentheadapter\n,adapter-name);//释放适配器列表pcap_freealldevs(allAdapters);return-1;}printf(\nCapturesessionstartedonadapter%s\n,adapter-name);pcap_freealldevs(allAdapters);//释放适配器列表//开始捕获数据包intretValue;while((retValue=pcap_next_ex(adapterHandle,&packetHeader,&packetData))=0){//timeoutelapsedifwereachthispointif(retValue==0)continue;//打印捕获数据包的信息printf(lengthofpacket:%d\n,packetHeader-len);}//ifwegethere,therewasanerrorreadingthepacketsif(retValue==-1){printf(Errorreadingthepackets:%s\n,pcap_geterr(adapterHandle));return-1;}system(PAUSE);return0;}运行结果3发送数据封包#includepcap.hint_tmain(intargc,_TCHAR*argv[]){pcap_if_t*allAdapters;//适配器列表pcap_if_t*adapter;pcap_t*adapterHandle;//适配器句柄u_charpacket[20];//待发送的数据封包charerrorBuffer[PCAP_ERRBUF_SIZE];//错误信息缓冲区if(pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&allAdapters,errorBuffer)==-1){//检索机器连接的所有网络适配器fprintf(stderr,Errorinpcap_findalldevs_exfunction:%s\n,errorBuffer);return-1;}if(allAdapters==NULL){//不存在人任何适配器printf(\nNoadaptersfound!MakesureWinPcapisinstalled.\n);return0;}intcrtAdapter=0;for(adapter=allAdapters;adapter!=NULL;adapter=adapter-next){//遍历输入适配器信息(名称和描述信息)printf(\n%d.%s,++crtAdapter,adapter-name);printf(--%s\n,adapter-description);}printf(\n);//选择适配器intadapterNumber;printf(Entertheadapternumberbetween1and%d:,crtAdapter);scanf_s(%d,&adapterNumber);if(adapterNumber1||adapterNumbercrtAdapter){printf(\nAdapternumberoutofrange.\n);//释放适配器列表pcap_freealldevs(allAdapters);return-1;}adapter=allAdapters;for(crtAdapter=0;crtAdapteradapterNumber-1;crtAdapter++)adapter=adapter-next;//打开指定适配器adapterHandle=pcap_open(adapter-name,//nameoftheadapter65536,//portionofthepackettocapture//65536guaranteesthatthewhole//packetwillbecapturedPCAP_OPENFLAG_PROMISCUOUS,//promiscuousmode1000,//readtimeout-1millisecondNULL,//authenticationontheremotemachineerrorBuffer//errorbuffer);if(adapterHandle==NULL){//指定适配器打开失败fprintf(stderr,\nUnabletoopentheadapter\n,adapter-name);//释放适配器列表pcap_freealldevs(allAdapters);return-1;}pcap_freealldevs(allAdapters);//释放适配器列表//创建数据封包//设置目标的MAC地址为01:01:01:01:01:01packet[0]=0x01;packet[1]=0x01;packet[2]=0x01;packet[3]=0x01;packet[4]=0x01;packet[5]=0x01;//设置源的MAC地址为02:02:02:02:02:02packet[6]=0x02;packet[7]=0x02;packet[8]=0x02;packet[9]=0x02;packet[10]=0x02;packet[11]=0x02;//设置封包其他部分内容for(intindex=12;index20;index++){packet[index]=0xC4;}//发送数据封包if(pcap_sendpacket(adapterHandle,//theadapterhandlepacket,//thepacket20//thelengthofthepacket)!=0){fprintf(stderr,\nErrorsendingthepacket:\n,pcap_geterr(adapterHandle));return-1;}system(PAUSE);return0;}运行结果#define_CRT_SECURE_NO_WARNINGS#includepcap.hvoidpacket_handler(u_char*user,conststructpcap_pkthdr*pkt_header,constu_char*pkt_data);intmain(){pcap_t*cap_ins_des;pcap_if_t*alldevs;pcap_if_