-2

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

为了导入 WinDirvert 库,我在项目属性中做了如下设置:

  1. 配置属性:通用
    公共语言运行时支持:公共语言运行时支持(/ctr)
  2. 配置属性:链接器
    附加依赖项:WinDivert.lib
    模块定义文件的链接:windivert.def 的链接

在我创建的项目中,我还在头文件中添加了 windivert.h。

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

#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;
}

最后我把{WinDivert.dll, windivert.h, WinDivert.lib, WinDivert32.sys}放到了项目目录下。

但是,显示以下错误:

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

问题:我该如何解决这种情况?

4

1 回答 1

0

a)您的主要来源是.cpp,因此您可以删除[STAThreadAttribute]并更改
int main(array<System::String ^> ^args)int _tmain(int argc, _TCHAR* argv[])

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

c) DLL/SYS 文件需要复制到 Debug 和 Release 文件夹

于 2014-06-18T10:44:22.103 回答