如何在VC中配置winPcap开发环境

VC6.0中使用winpcap
1.下载winpcap的安装包,程序员开发包。(两个包)
2.执行安装包,这样你的机子就能运行winpcap程序了
3.解压开发包,在VC的option的include和lib中加入winpcap的include和lib
4. 在工程的setting中导入wpcap.lib和Packet.lib库
VC .net中使用winpcap
1、在项目-〉属性    -〉C++    -〉常规标签的附加包含目录中包含WinPcap的Include目录  
2、在项目-〉属性    -〉链接器    -〉输入标签的附加依赖项中添加lib库文件
在Microsoft的VC++中使用wpcap.dll创建应用的步骤如下:
在需要调用wpcap.dll动态连接库的功能和函数的所有源代码文件的开头引用头文件pcap.h; 
如果应用程序使用了Winpcap的Win32相关函数,记住在预处理定义中需要引用WPCAP; 
在编译连接选项Link中设置包括wpcap.lib库文件,wpcap.lib在Winpcap开发补丁中找到; 
在编译连接选项中设置包含winsock库函数(比如wsock32.lib)。这个文件由C编译器提供,包含了Windows环境下的socket函数,有些libpcap函数也需要调用它。 
还要记住:
为了增加预处理定义,必须在Project菜单中选择Settings,在tab控件中选择C/C++页面,在Category下拉框中选择General,在Preprocessor Definitions文本框中加入预定义。 
为了在Microsoft VC++项目中增加一个新库连接,需要从菜单Project中选择Settings,再在tab控件中选择Link,然后在Objcet/library modules编辑框中加入要加入的新连接库名字。 
为了在Microsoft VC++项目中加入一个新的库文件搜索路径,必须从Tools菜单中选择Options,然后在tab控件中选择Directories页面,在Show Directories for下拉框中选择Library files,在Directories列表框中加入到要加入的库文件路径。 
为了在Microsoft VC++中加入一条路径以便编译器能够找到包含文件,需要从Tools菜单中选择Options,在tab控件中选择Directories页面,从Show Directories for下拉框中选择Include files,在Directories列表框中加入到要加入的包含头文件路径。
一) 首先安装winpcap驱动,可以到winpcap官方网站上下载: http://www.winpcap.org/install/default.htm
安装winpcap驱动后:
1. C:/WINDOWS/system32目录下自动生成: wpcap.dll,packet.dll
2. C:/WINDOWS/system32/drivers下自动生成:npf.sys
(二)winpcap-4.0.3配置环境:
在项目->XX属性,选择配置属性:
1. c/c++->常规/附加包含目录:D:/WpdPack-4.0.3/Include
        ->预处理器/预处理器定义:WPCAP
2. 链接器->常规/附加库目录:D:/WpdPack-4.0.3/Lib
  ->输入/附加依赖项: Packet.lib wpcap.lib ws2_32.lib
(三)新建一个空WIN32控制台项目,添加。C文件:
#include "pcap.h"

int main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
int res;
char errbuf[PCAP_ERRBUF_SIZE];
struct tm *ltime;
char timestr[16];
struct pcap_pkthdr *header;
const u_char *pkt_data;
time_t local_tv_sec;
  
    
/* Retrieve the device list */
if(pcap_findalldevs(&alldevs, errbuf) == -1)
{
  fprintf(stderr,"Error in pcap_findalldevs: %s/n", errbuf);
  return -1;
}
    
    /* Print the list */
    for(d=alldevs; d; 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;
    }
    
    printf("Enter the interface number (1-%d):",i);
    scanf("%d", &inum);
    
    if(inum < 1 || inum > i)
    {
        printf("/nInterface number out of range./n");
        /* Free the device list */
        pcap_freealldevs(alldevs);
        return -1;
    }
    /* Jump to the selected adapter */
    for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
    
/* Open the adapter */
if ((adhandle= pcap_open_live(d->name, // name of the device
        65536,   // portion of the packet to capture. 
           // 65536 grants that the whole packet will be captured on all the MACs.
        1,    // promiscuous mode (nonzero means promiscuous)
        1000,   // read timeout
        errbuf   // error buffer
        )) == NULL)
{
  fprintf(stderr,"/nUnable to open the adapter. %s is not supported by WinPcap/n", d->name);
  /* Free the device list */
  pcap_freealldevs(alldevs);
  return -1;
}
    
    printf("/nlistening on %s.../n", d->description);
    /* At this point, we don't need any more the device list. Free it */
    pcap_freealldevs(alldevs);
/* Retrieve the packets */
while((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0){
  
  if(res == 0)
   /* Timeout elapsed */
   continue;
  
  /* convert the timestamp to readable format */
  local_tv_sec = header->ts.tv_sec;
  ltime=localtime(&local_tv_sec);
  strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
  
  printf("%s,%.6d len:%d/n", timestr, header->ts.tv_usec, header->len);
}
if(res == -1){
  printf("Error reading the packets: %s/n", pcap_geterr(adhandle));
  return -1;
}
   pcap_close(adhandle);  
   return 0;
}
编译,运行!
(三)在工具/选项/项目/VC++项目/可执行文件添加: D: /WpdPack-4.0.3/Include D:/WpdPack-4.0.3/Lib
D:/WpdPack-4.0.3/Include(包含文件)和D:/WpdPack-4.0.3/Lib(库文件)

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值