【发布时间】:2014-08-08 14:34:18
【问题描述】:
我将设计一个使用 WinDivert 来控制网络流量的程序。 我使用的语言是 C++,程序是在 Visual Studio 2008 下设计的。 首先,我在 Visual C++ CLR(Windows 窗体应用程序)中创建了一个项目,这样我就可以简单地实现 UI。
为了导入WinDirvert库,我在项目属性中做了如下设置:
- 配置属性:常规
公共语言运行时支持:公共语言运行时支持(/ctr) - 配置属性:链接器
附加依赖: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
问题: 我该如何解决这种情况?
【问题讨论】:
-
这是一个完整的火车残骸。不仅编译器和链接器设置严重错误,还在程序终止前一微秒转移了调用。不要尝试在 .NET 程序中执行此操作。
-
@HansPassant 我为 WinDivert 编写了一个 .net 包装器,现在我害怕你看到它的那一天。大声笑
标签: c++-cli dllimport unmanaged wfp