Qt 5 configures the WinPcap development environment

Step 1: Download the necessary tools.

  • Download the WinPcap program, unzip it and install it.

  • Download the WpdPack toolkit. After decompression, the folder contains Includeand Libtwo subfolders.

Step 2: Configure the development environment, there are two ways.

  • The first one:
    create a new WpdPackfolder , and then copy the Includeand Libtwo folders to this folder. Add
    the following to the project file.pro

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

    Note : The location of the folder here can be freely selected, as long as the file paths in the project file are consistent.

  • The second: copy all the contents of the folder
    to the directory Copy all the contents of the folder to the directory Then just add to the project fileIncludeC:\Qt\Qt5.6.1\5.6\mingw49_32\include
    LibC:\Qt\Qt5.6.1\5.6\mingw49_32\lib
    .proLIBS += wpcap.lib

Step 3: Write the program and run it, add the following to the header file:

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

Here is a test code.

#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();
}

The running result is as follows:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324812456&siteId=291194637