How to compile WinPcap with Visual Studio 2010?

The documentation for WinPcap was pretty poor. The last update for Visual Studio was for 2005, so this hasn’t been updated for Visual Studio 2008 or Visual Studio 2010.

Also, the steps don’t say what type of C++ project was created. This is probably why it missing information, such as the fact that WIN32 must be defined.

So I thought I would get it working and provide a post someone could actually find. I had to read the pcap.h file myself to figure out what was missing in the documentation.

Prerequisites

It is expected you have Visual Studio 2010 already. It may work the same with Visual C++ 2010.

Step 1 – Create a new empty project

  1. In Visual Studio go to File | New | Project.
  2. Select Other Languages | Visual C++ | General | Empty Project
  3. Enter a Project name, Location, and Solution Name.
  4. Take note of the solution directory.
  5. Click OK.

This creates the following directory structure:

.\WinPcapExample\WinPcapExample.sdf
.\WinPcapExample\WinPcapExample.sln
.\WinPcapExample\WinPcapExample\
.\WinPcapExample\WinPcapExample\WinPcapExample.vcxproj
.\WinPcapExample\WinPcapExample\WinPcapExample.vcxproj.filters
.\WinPcapExample\WinPcapExample\WinPcapExample.vcxproj.user

Step 2 – Create a main.cpp file

  1. Right-click on the project and choose Add | New Item.
  2. Select C++ File (.cpp)
  3. Enter the name as Main.cpp.
  4. Click Ok.
  5. Add the standard main function to the file:
    int main(int argc, char *argv[])
    {
    }
    
  6. Save the file.

Step 3 – Download and extract WinPCap

  1. Go to the WinPcap developer page:
    http://www.winpcap.org/devel.htm
  2. Download the latest stable version of WinPcap.
  3. Extract to your solution directory.

Step 4 – Configure the Project properties

Now that the needed resources are in your solution directory, you can configure your project to access them.

  1. In Visual Studio, right-click on your project and choose Properties.
  2. Make the following configuration changes to your project:

Configuration 1 – Add Additional Include Directories

  1. Go to Configuration Properties | C/C++ | General.
    Note: If you don’t see the C/C++ option, did you forget to do Step 2 above?
  2. For Additional Include Directories add the relative path:
    ..\WpdPack\Include

Configuration 2 – Add Preprocessor Definitions

  1. Go to Configuration Properties | C/C++ | Preprocessor.
  2. For Preprocessor Definitions, add these three:
    WIN32;WPCAP;HAVE_REMOTE.

Preprocessor Definintions

Important: If you fail to add WIN32, your compile will fail as follows:

Error	1	error C1083: Cannot open include file: 'sys/time.h': No such file or directory	c:\users\jared\documents\visual studio 2010\projects\c++\readpcapexample\libs\winpcap-4.1.2\include\pcap\pcap.h	47	1	ReadPCapExample

Configuration 3 – Add Additional Library Directories

  1. Go to Configuration Properties | Linker | General.
  2. For Additional Library Directories enter the following:
    ..\WpdPack\Lib
AdditionalLibraryDirectories

Configuration 4 – Add Additional Dependencies

  1. Go to Configuration Properties | Linker | Input.
  2. For Additional Dependencies, add the following:
    winpcap.lib;Packet.lib;

Additional Dependencies

Note: I removed the default libraries. You may need some of them:
kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;

Step 5 – Include pcap.h

  1. Open the Main.cpp file.
  2. Include pcap.h.
    #include
    
    int main(int argc, char *argv[])
    {
    }
    
  3. Try to compile and it should work.
You are now ready to develop using WinPcap.

17 Comments

  1. professional teeth whitening in West Los Angeles

    How to compile WinPcap with Visual Studio 2010? | Rhyous

  2. Shrike says:

    I get the weirdest error.
    "The C++ Standard Library forbids macroizing keywords. Enable warning C4005 to find the forbidden macro."

  3. Hind says:

    thanks for This.

    do i have to do these steps for every time i want to open new project ?

  4. stefan says:

    I build it successfully but when I try to run it it says that wpcap.dll is missing. Using visual studio C++ 2012 and Winpcap 4.1.2 developers pack

  5. Ami Data Tech. says:

    Thanks for this!
    However I would like to know how can I use later my project on another computer ?
    it doesn't create any dll that I can transfer ?!

  6. bedoui says:

    Mreza
    i have the same error
    cannot open file 'winpcap.lib'

  7. Prabha says:

    Created a small project in VC ++ 6.0 . Included pcap.h in source file.

    in Build menu observed the error msg " Cannot compile pcap-stdinc.h : no

    compile tool is associated with this file extension. please let me where i

    missed any step.

  8. mimosa says:

    Very helpful. Thank you!

  9. Evan says:

    Thanks for this! Quick and easy to follow. Worked out great.

  10. Mreza says:

    I've got the following error:

    cannot open file 'winpcap.lib' c:\Users\Taesiri\documents\visual studio 2010\Projects\WinPcapExample\WinPcapExample\LINK WinPcapExample

    (LINK folder doesn't exist!) 🙁

    thanks for the article BTW!

Leave a Reply

How to post code in comments?