1

I am trying to monitor my monthly network usage. Because the default Windows 10 Data Usage page doesn't work, I took the opportunity to learn C++, as I am familiar with languages like Python and PHP.

Upon hours of Google Searching, I came to the conclusion that WinPcap is the module I should use. I downloaded it from here: https://www.winpcap.org/devel.htm

I extracted the .zip into my C++ console application folder. So my application is in C:\Visual Studio\ProjectName123\, and I extracted WpdPack/ into there.

I am trying to use their example code:

#include "pch.h"
#include "WpdPack\Include\pcap\pcap.h"

main()
{
    pcap_if_t *alldevs;
    pcap_if_t *d;
    int i = 0;
    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 */
    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");
        return;
    }

    /* We don't need any more the device list. Free it */
    pcap_freealldevs(alldevs);
}

I get several errors, starting with: identifier "PCAP_SRC_IF_STRING" is undefined

Extremely frustrating following an example to the T, and it not running correctly. Extremely frustrated with C++.

Help would be appreciated, specifically explaining why my code, which follows this example perfectly, doesn't run.

  • 1
    If you have no prior experience with C++, then you'll want to learn C++ before jumping right into stuff like the above. Especially if all your programming experience so far is with PHP and Python, which are fundamentally different from C++. I'd recommend picking up a good book. You may find [the list here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) useful… – Michael Kenzel Apr 15 '19 at 03:33
  • @MichaelKenzelI I can't just learn from tutorials (like I can with Python)? :( Are there any online books I don't want to read in real – 127dot0dot0dot1 Apr 15 '19 at 03:34
  • 1
    I'm sure it's possible to learn C++ from tutorials. Personally, I wouldn't recommend it, however. A tutorial can be great when it comes to stuff like, e.g., an introduction to using a certain library, or a specific language feature. But when it comes to properly learning an entire programming language from the ground up, the format of a tutorial would seem far too limited in scope and depth. At least I've never seen such a tutorial that I would recommend. But that's just my experience/opinion… – Michael Kenzel Apr 15 '19 at 03:41
  • 1
    Whatever you do, continuing what you have started up there without a proper foundation in C++ will almost certainly just end in frustration. I would definitely recommend to first get comfortable with the basics of C++. Specifically, you'll at least want to have a grasp on the C++ build process (headers, object files, compiling vs linking) and solid understanding of things like pointers before getting back to your network traffic monitor… – Michael Kenzel Apr 15 '19 at 03:50
  • Also, be aware that the code you posted above is not really C++. It's more like an ancient version of C… – Michael Kenzel Apr 15 '19 at 03:55

1 Answers1

0

That sample code is misleading; they are intending that you replace PCAP_SRC_IF_STRING with a string value rather than use it as is. To use pcap_findalldevs_ex(), you need to pass the first argument as a string specifying where it should look for adapters. You should find that the following works better:

 pcap_findalldevs_ex("rpcap://",...

I'd suggest that you use this is a reference: WinPCAP exported functions

David Hoelzer
  • 15,862
  • 4
  • 48
  • 67