c++visual-studiodllpcapnpcap

Compiling VC++ program with Npcap in Visual Studio 2015


I am compiling the following program in Visual Studio 2015 Community Edition.

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <pcap.h>

int main(int argc, char **argv)
{

    pcap_if_t *alldevsp, *device;


    char errbuf[100];
    int count = 1;

    //First get the list of available devices
    printf("Finding available devices ... ");
    if (pcap_findalldevs(&alldevsp, errbuf))
    {
        printf("Error finding devices : %s", errbuf);
        exit(1);
    }
    printf("Done");

    //Print the available devices
    printf("\nAvailable Devices are :\n");
    for (device = alldevsp; device != NULL; device = device->next)
    {
        printf("%d. %s - %s\n", count, device->name, device->description);
        count++;
    }

    return 0;
}

For pcap I have downloaded the library from Npcap project @ GitHub. I installed the release for getting the DLL and using its SDK library for header and linker libraries. The installation of DLL is from the release package 0.0.8-r2 and SDK is from 0.0.7-r9.

Following several pointers over the net how to setup the environment, I have following setting.

  1. Configuration Properties -> C/C++ -> General -> Additional Include Directories -> Path to header folder from SDK.
  2. Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions -> WIN32 _DEBUG _CONSOLE WPCAP HAVE_REMOTE
  3. Configuration Properties -> Linker -> General -> Additional Library Directory -> Path to library folder from SDK.
  4. Configuration Properties -> Linker -> Input -> Additional Dependencies -> wpcap.lib Packet.lib

DLL from release exe gets installed in C:\Windows\System32\Npcap. System is Windows 10 Home.

Question:

The above program compiles fine.

1>------ Build started: Project: HelloWorld, Configuration: Debug Win32 ------
1>  HelloWorld.cpp
1>  HelloWorld.vcxproj -> C:\Users\xxx\documents\visual studio 2015\Projects\HelloWorld\Debug\HelloWorld.exe
1>  HelloWorld.vcxproj -> C:\Users\xxx\documents\visual studio 2015\Projects\HelloWorld\Debug\HelloWorld.pdb (Full PDB)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

When I run it, it was complaining about missing wpcap.dll file. I am new to VS as well as VC++, I googled and simplest technique I found just get over the issue, I copied the DLL from System32 to folder where .exe file was getting generated.

After this DLL issue went away, but now I am getting.

'HelloWorld.exe' (Win32): Loaded 'C:\Users\xxx\Documents\Visual Studio 2015\Projects\HelloWorld\Debug\HelloWorld.exe'. Symbols loaded.
'HelloWorld.exe' (Win32): Loaded 'C:\Windows\syswow64\ntdll.dll'. Cannot find or open the PDB file.
'HelloWorld.exe' (Win32): Loaded 'C:\Windows\syswow64\kernel32.dll'. Cannot find or open the PDB file.
'HelloWorld.exe' (Win32): Loaded 'C:\Windows\syswow64\KernelBase.dll'. Cannot find or open the PDB file.
'HelloWorld.exe' (Win32): Loaded 'C:\Windows\syswow64\ucrtbased.dll'. Cannot find or open the PDB file.
'HelloWorld.exe' (Win32): Loaded 'C:\Windows\syswow64\vcruntime140d.dll'. Cannot find or open the PDB file.
The thread 0x160c has exited with code -1073741701 (0xc000007b).
The thread 0xd5c has exited with code -1073741701 (0xc000007b).
The thread 0x16c4 has exited with code -1073741701 (0xc000007b).
The program '[9632] HelloWorld.exe' has exited with code -1073741701 (0xc000007b).

I googled it, it seems there is mix of 64 bit and 32 bit DLL. I have no clue how to start debugging this issue.

I would really appreciate if some one guides me, to solve.

  1. Better way (good practice in VC++ world) to find DLL instead of copying to exe folder.
  2. Tips on how to find which DLL is causing the issue.

Thanks for the time.


Solution

  • From the configuration, your HelloWorld.exe is a 32-bit (x86) program. I assume that you are using a x64 Windows OS, so C:\Windows\System32\Npcap is for x64 DLLs. And C:\Windows\SysWOW64\Npcap is for x86 DLLs.

    You got the 0xc000007b error because your x86 HelloWorld.exe is trying to load Npcap's x64 DLLs, which is definitely not right.

    So the solution is to copy the DLLs (wpcap.dll, Packet.dll) from C:\Windows\SysWOW64\Npcap to folder where .exe file was getting generated.

    Another way is adding C:\Windows\System32 to the PATH environment variable. So your x86 and x64 binaries will always find the correct Npcap DLLs no matter where they are located.