C++ cli 使用WinDivert时DLL导入失败

C++ cli 使用WinDivert时DLL导入失败,c++-cli,dllimport,unmanaged,wfp,C++ Cli,Dllimport,Unmanaged,Wfp,我将使用WinDivert设计一个程序来操纵网络流量。 我使用的语言是C++,程序是在Visual Studio 2008下设计的。 首先,我在Visual C++ + CLR(Windows窗体应用程序)中创建了一个项目,这样我就可以简单地实现UI。 为了导入WinDirvert库,我在项目属性中进行了以下设置: 配置属性:常规 公共语言运行时支持:公共语言运行时支持(/ctr) 配置属性:链接器 其他依赖项:WinDivert.lib的链接 模块定义文件:windivert.def的链接 在

我将使用WinDivert设计一个程序来操纵网络流量。 我使用的语言是C++,程序是在Visual Studio 2008下设计的。 首先,我在Visual C++ + CLR(Windows窗体应用程序)中创建了一个项目,这样我就可以简单地实现UI。 为了导入WinDirvert库,我在项目属性中进行了以下设置:

  • 配置属性:常规
    公共语言运行时支持:公共语言运行时支持(/ctr)
  • 配置属性:链接器
    其他依赖项:WinDivert.lib的链接
    模块定义文件:windivert.def的链接
  • 在我创建的项目中,我还在头文件中添加了windivert.h

    此外,windivert.h包含在我的项目(ProjectG.cpp)的主要入口点中:

    问题: 如何解决此问题?

    a)您的主要源代码是.cpp,因此您可以删除
    [STAThreadAttribute]
    并更改
    int main(数组^args)
    to
    int\u tmain(int argc,\u TCHAR*argv[])

    b) 从链接器模块定义文件中排除windivert.def,这仅在您创建DLL时发生


    c) 排除windivert.def后,需要将DLL/SYS文件复制到调试和发布文件夹

    ,会显示更多错误,所有错误都是LNK 2028和LNK 2019,它们表示存在未解决的外部符号“extern”c“int_cdecl…”如果确实将windivert.lib添加到链接器输入“其他依赖项”,则“未解决的外部”错误指出函数声明(通常在.h文件中)和.lib文件中的函数实现之间存在差异。感谢问题得到解决,最后我发现在WinDivert WDDK中找到的.lib与Visual Studio不兼容。然后,我在另一个WinDivert包(MSVC)中使用了.lib它是有效的。这是一次彻底的火车失事。不仅你的编译器和链接器设置严重错误,而且在程序终止前一微秒你就转移了呼叫。不要在.NET程序中尝试这样做。@HansPassant我为WinDivert写了一个.NET包装器,现在我害怕你看到它的那一天。哈哈
    #include "stdafx.h"
    #include "Form1.h"
    #pragma managed(push, off)
    #include "windivert.h"
    #pragma managed(pop)
    
    using namespace ProjectG;
    
    [STAThreadAttribute]
    int main(array<System::String ^> ^args)
    {
        // Enabling Windows XP visual effects before any controls are created
        Application::EnableVisualStyles();
        Application::SetCompatibleTextRenderingDefault(false);
    
        // Create the main window and run it
        Application::Run(gcnew Form1());
    
    
        HANDLE handle;
        unsigned char packet[8192];
        UINT packet_len;
        WINDIVERT_ADDRESS addr;
        handle = WinDivertOpen("udp", WINDIVERT_LAYER_NETWORK, 0,
            WINDIVERT_FLAG_DROP);
        if (handle == INVALID_HANDLE_VALUE)
        {
            Application::Exit();        
        }
        while (TRUE)
        {
            // Read a matching packet.
            if (!WinDivertRecv(handle, packet, sizeof(packet), &addr, &packet_len))
            {
                MessageBox::Show("Fail");
                continue;
            }
        }
        return 0;
    }
    
    fatal error LNK1306: DLL entry point "int __clrcall main(cli::array<class
    System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z) cannot be managed;    
    compile to native   ProjectG.obj    ProjectG
    
    warning LNK4070: /OUT:WinDivert.dll directive in .EXP differs from output filename   
    'C:\Users\David\Desktop\css\ProjectG\Debug\ProjectG.exe'; ignoring directive    
    ProjectG.exp    ProjectG