winpcap 编程及环境配置

1、首先要明确,一共有两个文件需要下载,一个是winpcap安装文件用来实际操作http://www.winpcap.org/install/default.htm,另一个是winpcap开发包https://www.winpcap.org/devel.htm

2、然后进行环境配置

  把开发包解压后得WpdPack放在你的工程文件夹下,然后找到Configuration Properties->VC++ directories下的Include Directories和Libraries Directories下分别加入($SolutionDir)WpdPack/Include和($SolutionDir)WpdPack/Lib

然后在C/C++->Preprocessor 下的Preprocessor Definitions下加入WPCAP和HAVE_REMOTE

然后在Linker->Command Line下的additional Option加入wpcap.lib ws2_32.lib

完毕

这是一个测试

#include <pcap.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")

void main()
{
    char erro_content[PCAP_ERRBUF_SIZE];
    struct in_addr net_ip_address;
    struct in_addr net_mask_address;
    char *net_interface=NULL;
    char *net_ip_string=NULL;
    char *net_mask_string=NULL;
    pcap_if_t *alldevs;
    pcap_if_t *d;

    u_int32_t net_ip;
    u_int32_t net_mask;
    //net_interface = pcap_lookupdev(erro_content);
    if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL , &alldevs, erro_content) == -1)
    {
        printf("查找网卡错误!%s\n", erro_content);
        return;
    }
    for (d=alldevs; d; d=d->next)
    {
        printf("%s\n",d->name);  
        printf("Description: %s\n",d->description); 
        pcap_lookupnet(d->name, &net_ip, &net_mask, erro_content);
        net_ip_address.s_addr = net_ip;
        net_ip_string = inet_ntoa(net_ip_address);
        printf("网络地址:%s\n", net_ip_string);
        net_mask_address.s_addr = net_mask;
        net_mask_string = inet_ntoa(net_mask_address);
        printf("网路掩码:%s\n", net_mask_string);
    }
    pcap_freealldevs(alldevs); 
    system("pause");
    return;
}

如果你用的winpcap的开发版本与安装版本不一致的话,这个代码可能不能运行成功

posted @ 2014-03-16 21:50  Hacker_MJW  阅读(2539)  评论(0编辑  收藏  举报