1.设置
(1)包含文件(include)、库文件(lib)目录路径设置
Tools->Options->Directories,在"Show directories for:"中选择"Include files",单
击下面的"New"图标,单击编辑框右侧进行浏览,选择WinPcap的Include目录,"OK"
同理,为"Library files"选择对应的Lib目录即可
(2)引入头文件(pcap.h)、库文件(wpcap.lib)
头文件(pcap.h):#include <pcap.h>
库文件(wpcap.lib):#pragma comment(lib, "wpcap.lib") 或在工程中设置:
Project->Settings->Link->Object/Library modules中,添加wpcap.lib
2.获取网络接口信息
#include<stdio.h>
#include<pcap.h>
#pragma comment(lib,"wpcap.lib")
void main()
{
struct pcap_if * pdevs=NULL,*p;
char errbuff[PCAP_ERRBUF_SIZE];
if(pcap_findalldevs(&pdevs,errbuff) != 0)
{
printf("发生错误:%s/n",errbuff);
return;
}
p=pdevs;
for(p;p!=NULL;p=p->next)
{
printf("Name:%s/n",p->name);
if(p->description)
{
printf("Description:%s/n",p->description);
}
print("*************************************************/n");
}
pcap_freealldevs(pdevs);
}
执行结果:
Name:/Device/NPF_GenericDialupAdapter
Description:Adapter for generic dialup and VPN capture
*************************************************
Name:/Device/NPF_{6FBEF434-C9DE-4DE4-9C66-9611F158C899}
Description:ADMtek AN983/AN985/ADM951X NDIS5 Driver (Microsoft's Packet
Scheduler)
*************************************************
3.获取网络设备信息及IP、NetMask
#include<stdio.h>
#include<pcap.h>//INCLUDE目录下
#include<winsock2.h>
#pragma comment(lib,"wpcap.lib")
#pragma comment(lib,"WS2_32.lib")
void main()
{
struct pcap_if *pdevs,*p;
char errbuff[PCAP_ERRBUF_SIZE];//#define PCAP_ERRBUF_SIZE 256
struct sockaddr_in * paddr;
if(pcap_findalldevs(&pdevs,errbuff) == -1)
{
printf("发生错误:%s/n",errbuff);
return;
}
for(p=pdevs;p!=NULL;p=p->next)
{
printf("Name:%s/n",p->name);
if(p->description)
{
printf("Description:%s/n",p->description);
}
if(p->addresses)
{
paddr=(struct sockaddr_in *)(p->addresses->addr);
printf("IP:%s/n",inet_ntoa(paddr->sin_addr));
paddr=(struct sockaddr_in *)(p->addresses->netmask);
printf("NetMask:%s/n",inet_ntoa(paddr->sin_addr));
}
printf("***************************************************/n");
}
pcap_freealldevs(pdevs);
}
执行结果:
Name:/Device/NPF_GenericDialupAdapter
Description:Adapter for generic dialup and VPN capture
***************************************************
Name:/Device/NPF_{6FBEF434-C9DE-4DE4-9C66-9611F158C899}
Description:ADMtek AN983/AN985/ADM951X NDIS5 Driver (Microsoft's Packet
Scheduler)
IP:211.67.17.123
NetMask:255.255.255.0
***************************************************