-2

I am going to design a program using WinDivert to manipulate the network traffic. The language I use is C++ and the program is designed under Visual Studio 2008. Firstly I create a project in visual C++ CLR (Windows Forms Application) so I can implement the UI simply.

For importing the WinDirvert Library, I have done the following setting in project properties:

  1. Configuaration Properties: General
    Common Language Runtime support: Common Language Runtime Support(/ctr)
  2. Configuaration Properties: Linker
    Additional Dependencies: link of WinDivert.lib
    Module Definition File: link of windivert.def

Within the project I have created, I also added the windivert.h in the header files.

Also, windivert.h is included in the main entry point of my project (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;
}

Finally, I put the {WinDivert.dll, windivert.h, WinDivert.lib, WinDivert32.sys} under the project directory.

However, the following error is shown:

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

Additional: (a warning)

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

Question: How can I resolve this situation?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • This is a complete train wreck. Not only do you have the compiler and linker settings drastically wrong, you divert the call a microsecond before the program terminates. Do not attempt to do this in a .NET program. – Hans Passant Jun 18 '14 at 12:25
  • @HansPassant I wrote a .net wrapper for WinDivert, now I'm terrified of the day you ever see it. lol –  Nov 19 '15 at 09:48

1 Answers1

0

a) your main source is .cpp, so you can delete [STAThreadAttribute] and change
int main(array<System::String ^> ^args) to int _tmain(int argc, _TCHAR* argv[])

b) exclude windivert.def from linker Module Definition File, this only when you are creating a DLL

c) the DLL/SYS files would need to be copied to the Debug and Release folders

Edward Clements
  • 5,040
  • 2
  • 21
  • 27
  • after excluding the windivert.def, more errors are shown and all of them are LNK 2028 and LNK 2019 which state that there are unsolved external symbol "extern "C" int_cdecl... – user3704347 Jun 18 '14 at 10:58
  • if you did add WinDivert.lib to the Linker Input "Additional Dependencies", then "unsolved external" errors point to a difference between the function declaration (normally in the .h file) and the function implementation in the .lib file – Edward Clements Jun 18 '14 at 12:05
  • thanks the problem is solved, finally I found that the .lib found in the WinDivert WDDK is not compatible with Visual Studio. Then, I use the .lib in another winDivert package (MSVC) and it works. – user3704347 Jun 18 '14 at 12:38