Main Page Modules Data Structures File List Data Fields Globals Related Pages

# Using WinPcap in your programs
[WinPcap user's manual]

## Modules
pktdump_ex example
pcap_filter example

## Detailed Description

## How to create an application that uses wpcap.dll

To create an application that uses wpcap.dll with Microsoft Visual C++, follow the next steps:

- Include the file pcap.h at the beginning of every source file that uses the functions exported by library.
- If your program uses Win32 specific functions of WinPcap, remember to include WPCAP among the preprocessor definitions.
- Set the options of the linker to include the wpcap.lib library file. wpcap.lib is generated compiling the WinPcap source code and can be found in the WinPcap developer's pack.
- Set the options of the linker to include the winsock library file (for example wsock32.lib). This file is distributed with the C compiler and contains the socket functions for Windows. It is needed by some libpcap functions.

Remember that:

- To add a preprocessor definition, you must select Settings from the Project menu, then select C/C++ from the tab control, and under the category General, you must add the definition under the Preprocessor Definitions text box.
- To add a new library to the project with Microsoft Visual C++, you must select Settings from the Project menu, then select Link from the tab control, and then add the name of the new library in the Objcet/library modules editbox.
- To add a new path where Microsoft Visual C++ will look for the libraries, you must select Options from the Tools menu, then Link from the tab control, library files from the show directories for combobox, and the add the path in the directories box.
- To add a new path where Microsoft Visual C++ will look for include files, you must select Options from the Tools menu, then Link from the tab control, include files from the show directories for combobox, and the add the path in the directories box.

## Sample programs

A couple of sample programs are provided to show the usage of the WinPcap API. The source of the examples, along with all the files needed to compile and run them, can be found in the Developer's Pack. For didactic purpose we provide here a browsable version of the code: it is possible to click on the variables and functions to jump the documentation of each of them. For a more complete and better documented set of samples, give a look at the tutorial section (WinPcap tutorial: a step by step guide to program WinPcap ).

pktdump_ex example

This program reads the packets from a file or a network adapter, depending on a command line switch. If a source is not provided, the program shows a list of available adapters and gives the possibility to choose one of them. Once the capture is started, the program prints the timestamp, the length and the raw content of the packets. Once compiled, it can run on all the Win32 platforms. It can be compiled to run on Unix as well (the makefile is provided).

pcap_filter example

This is a more complete example of libpcap usage. It shows, among other things, how to create and set filters and how to save a capture to disk. It can be compiled under Win32 or Unix (projects and makefiles are provided). Pcap_filter (pf.exe) is a general-purpose packet filtering application: its input parameters are a source of packets (it can be a physical interface or a file), a filter and an output file. It takes packets from the source until CTRL+C is pressed or the whole file is processed, applies the filter to the incoming packets and saves them to the output file if they satisfy the filter. Pcap_filter can be used to dump network data according to a particular filter, but also to extract a set of packets from a previously saved file. The format of both input and output files is the libpcap one, i.e. same of WinDump, tcpdump and many other network tools.

documentation. Copyright (c) 2002-2003 Politecnico di Torino. All rights reserved.