1. 程式人生 > >Qt 5 配置 WinPcap 開發環境

Qt 5 配置 WinPcap 開發環境

5.6 for wpc tde print int fprintf 文件 程序

第一步:下載必要的工具。

  • 下載 WinPcap 程序,解壓後安裝。

  • 下載 WpdPack 工具包,解壓後文件夾下包含有 IncludeLib 兩個子文件夾。

第二步:配置開發環境,有兩種方式。

  • 第一種:
    在 C 盤下新建一個 WpdPack 文件夾,然後將 IncludeLib 兩個文件夾拷貝到該文件夾中。
    在項目文件 .pro 添加以下內容

    INCLUDEPATH += C:\WpdPack\Include
    LIBS += C:/WpdPack/Lib/wpcap.lib

    說明:這裏文件夾的位置可自由選定,只要項目文件中的文件路徑保持一致即可。

  • 第二種:
    Include 文件夾下的全部內容拷貝到 C:\Qt\Qt5.6.1\5.6\mingw49_32\include

    目錄下
    Lib 文件夾下的全部內容拷貝到 C:\Qt\Qt5.6.1\5.6\mingw49_32\lib 目錄下
    這時只需在項目文件 .pro 添加 LIBS += wpcap.lib 即可

第三步:編寫程序並運行,在頭文件中添加以下內容:

#define HAVE_REMOTE
#include <pcap.h>
#include <remote-ext.h>

這裏提供一份測試代碼。

#include <QCoreApplication>
#include <QDebug>
#define HAVE_REMOTE
#include <pcap.h>
#include <remote-ext.h>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    pcap_if_t *alldevs;
    char errbuf[PCAP_ERRBUF_SIZE];

    /* Retrieve the device list from the local machine */
    if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL /* auth is not needed */,
                            &alldevs, errbuf) == -1) {
        fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf);
        exit(1);
    }

    /* Print the list */
    pcap_if_t *d;
    int i=0;
    for(d= alldevs; d != NULL; 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");
    } else {
        /* We don‘t need any more the device list. Free it */
        pcap_freealldevs(alldevs);
    }

    return a.exec();
}

運行結果如下圖:
技術分享圖片

Qt 5 配置 WinPcap 開發環境