VS2019配置WinPcap开发

首先需要安装WinPcap运行库
下载WinPcap开发包,并解压
VS2019配置WinPcap开发_第1张图片


vs2019新建一个工程


  1. 打开工程调试属性
    添加包含路径(Include和Lib目录)包含目录和库目录
    VS2019配置WinPcap开发_第2张图片
    VS2019配置WinPcap开发_第3张图片

  2. 添加wpcap.lib和ws2_32.lib两个库
    VS2019配置WinPcap开发_第4张图片

  3. 添加WPCAP和HAVE_REMOTE宏
    VS2019配置WinPcap开发_第5张图片


注意
此处配置的为x64,在vs中使用是需要,否则会出现找不到pcap.h的情况

VS2019配置WinPcap开发_第6张图片

由于配置的是x64,因此在第一步包含Lib目录的时候需要包含x64的目录,如果是win32就只需要包含(WpdPack\Lib)即可x64目录


由于开发winpcap项目的目的在于为win32应用程序提供访问网络底层的能力,因此开发包里面有大量的WIN32的标识符,直接使用会出现
VS2019配置WinPcap开发_第7张图片
因此在使用时需要在文件中#define WIN32,或者在设置宏时添加WIN32
VS2019配置WinPcap开发_第8张图片


测试程序,使用WinPcap获取设备列表

#define WIN32
#include 
#include "pcap.h"
#pragma comment(lib,"wpcap")

using namespace std;

int  main()
{
    pcap_if_t* alldevs;
    pcap_if_t* d;
    char errbuf[PCAP_ERRBUF_SIZE];
    //获取设备列表
    if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
    {
        cout << "Error in pacp_findalldevs_ex:" << errbuf << endl;
        return -1;
    }
    int i = 0;
    for (d = alldevs; d != NULL; d = d->next)
    {
        cout << endl;
        cout << i++ << " " << d->name << endl;
        if (d->description)
            cout << " <" << d->description << ">" << endl;
        else
            cout << " " << endl;
    }
    if (i == 0)
    {
        cout << "No interfaces found!" << endl;
        return -1;
    }

    pcap_freealldevs(alldevs);

    return 0;
}

直接运行可能会出现
const char * 与char * 不兼容的问题
VS2019配置WinPcap开发_第9张图片
此时需要设置vs符合模式,将其改为 否
VS2019配置WinPcap开发_第10张图片

你可能感兴趣的:(杂,visual,studio)