-1

I have installed WinPcap, the Developer pack, created a new project and adjusted the compiler/linker settings. I use the very basic example from WinPCap's homepage ("obtaining the device list"). But I get the error:

The argument of type "" const char * "" is incompatible with the parameter "" char * "".

in the line where the function pcap_findalldevs_ex() is called. I already tried all the casts mentioned here.

#include "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);
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Jane dOE
  • 21
  • 6
  • Do you mean not the first parameter PCAP_SRC_IF_STRING is wrong, instead the error buffer is? I just havent modified the code from the example, its my first touch into winpcap – Jane dOE Sep 19 '18 at 02:34

1 Answers1

1

On this line:

if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL /* auth is not needed */, &alldevs, errbuf) == -1)

The PCAP_SRC_IF_STRING constant is a preprocessor macro that maps to a narrow string literal:

#define PCAP_SRC_IF_STRING   "rpcap://" 

The first parameter of pcap_findalldevs_ex() expects a non-const char* pointer:

int pcap_findalldevs_ex(
    char *source,
    struct pcap_rmtauth *auth,
    pcap_if_t **alldevs,
    char *errbuf     
)   

In C++, a narrow string literal is of type const char[], which decays to const char*. You cannot pass a const char* pointer where a non-const char* pointer is expected. Older C++ compilers may have allowed it, but starting in C++11 this is required to be an error.

However, in this particular case, you can safely cast away the const-ness, as pcap_findalldevs_ex() does not write data to the source parameter:

pcap_findalldevs_ex(const_cast<char*>(PCAP_SRC_IF_STRING), ...)

If you don't want to do that, you will have to make a local non-const copy of the string data first:

char szSource[] = PCAP_SRC_IF_STRING;
pcap_findalldevs_ex(szSource, ...)
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770