简体   繁体   English

在 Visual Studio 2015 中使用 Npcap 编译 VC++ 程序

[英]Compiling VC++ program with Npcap in Visual Studio 2015

I am compiling the following program in Visual Studio 2015 Community Edition.我正在 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.对于 pcap,我已经从Npcap项目@GitHub下载了该库。 I installed the release for getting the DLL and using its SDK library for header and linker libraries.我安装了用于获取 DLL 并将其 SDK 库用于头文件和链接器库的版本。 The installation of DLL is from the release package 0.0.8-r2 and SDK is from 0.0.7-r9. DLL 的安装来自发行包 0.0.8-r2,SDK 来自 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.配置属性 -> C/C++ -> 常规 -> 附加包含目录 -> 来自 SDK 的头文件夹的路径。
  2. Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions -> WIN32 _DEBUG _CONSOLE WPCAP HAVE_REMOTE配置属性 -> C/C++ -> 预处理器 -> 预处理器定义 -> WIN32 _DEBUG _CONSOLE WPCAP HAVE_REMOTE
  3. Configuration Properties -> Linker -> General -> Additional Library Directory -> Path to library folder from SDK.配置属性 -> 链接器 -> 常规 -> 附加库目录 -> SDK 中库文件夹的路径。
  4. Configuration Properties -> Linker -> Input -> Additional Dependencies -> wpcap.lib Packet.lib配置属性 -> 链接器 -> 输入 -> 附加依赖项 -> wpcap.lib Packet.lib

DLL from release exe gets installed in C:\\Windows\\System32\\Npcap.来自发行版 exe 的 DLL 安装在 C:\\Windows\\System32\\Npcap 中。 System is Windows 10 Home.系统是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.当我运行它时,它抱怨缺少 wpcap.dll 文件。 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.我是 VS 和 VC++ 的新手,我用谷歌搜索,发现最简单的技术解决了这个问题,我将 DLL 从 System32 复制到生成 .exe 文件的文件夹中。

After this DLL issue went away, but now I am getting.在此 DLL 问题消失后,但现在我明白了。

'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.我用谷歌搜索,似乎有 64 位和 32 位 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.找到 DLL 而不是复制到 exe 文件夹的更好方法(VC++ 世界中的良好实践)。
  2. Tips on how to find which DLL is causing the issue.有关如何查找导致问题的 DLL 的提示。

Thanks for the time.谢谢你的时间。

From the configuration, your HelloWorld.exe is a 32-bit (x86) program.从配置来看,您的HelloWorld.exe是一个 32 位 (x86) 程序。 I assume that you are using a x64 Windows OS, so C:\\Windows\\System32\\Npcap is for x64 DLLs.我假设您使用的是 x64 Windows 操作系统,因此C:\\Windows\\System32\\Npcap用于 x64 DLL。 And C:\\Windows\\SysWOW64\\Npcap is for x86 DLLs.C:\\Windows\\SysWOW64\\Npcap用于 x86 DLL。

You got the 0xc000007b error because your x86 HelloWorld.exe is trying to load Npcap's x64 DLLs, which is definitely not right.您收到0xc000007b错误,因为您的 x86 HelloWorld.exe正在尝试加载 Npcap 的 x64 DLL,这绝对是不对的。

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.因此,解决方案是将 DLL( wpcap.dllPacket.dll )从C:\\Windows\\SysWOW64\\Npcap到生成 .exe 文件的文件夹中。

Another way is adding C:\\Windows\\System32 to the PATH environment variable.另一种方法是将C:\\Windows\\System32PATH环境变量中。 So your x86 and x64 binaries will always find the correct Npcap DLLs no matter where they are located.因此,无论 x86 和 x64 二进制文件位于何处,它们都将始终找到正确的 Npcap DLL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM