Click here to Skip to main content
15,304,161 members
Articles / Programming Languages / C#
Posted 27 May 2003


146 bookmarked

Packet Sniffing with Winpcap Functions Ported to a .NET Library

Rate me:
Please Sign up or sign in to vote.
4.83/5 (54 votes)
25 Mar 2009GPL32 min read
Using Winpcap functions in the .NET Framework
Sample Image - dotnetwinpcap.jpg


Winpcap has been the de facto library in packet capture applications, but the problem is that it is only natively available for C++ and C.

This is an attempt to port some of the crucial Winpcap functions for the .NET environment. The demonstration project here is written in C#.

First of all, you need to install Winpcap from winpcap's Web site and then extract the project zip file. Be sure to reference dotnetwinpcap.dll in the project if not already so.

Methods Available

  • C#
    static ArrayList FindAllDevs()

    Returns an ArrayList of Device objects, each describing an Ethernet interface on the system.

  • C#
    bool Open(string source, int snaplen, int flags, int read_timeout)

    Opens an Ethernet interface with source as the name of the interface obtained from a Device object, snaplen is the max number of bytes to be captured from each packet, flags=1 means promiscuous mode, read_timeout is the blocking time of ReadNext before it returns.

  • C#
    PCAP_NEXT_EX_STATE ReadNext( out PacketHeader p, out byte[] packet_data)

    Reads a next packet and return the packet details (size and timestamp) to object p, and packet raw data in packet_data (array of bytes).

  • C#
    void StopDump()

    Stops dumping of capture data to a file.

  • C#
    bool StartDump(string filename) 

    Starts dumping of capture data to a file.

  • C#
    bool SetMinToCopy(int size)

    Sets the minimum number of bytes required to be received by the driver before OnReceivePacket fires. Lowering this can increase response time, but increases system calls which lowers program efficiency.

  • C#
    bool SetKernelBuffer(int bytes)

    Sets the number of bytes in the driver kernel buffer for packet capture. Increase this to avoid packet loss and improve performance. Default is 1 MB.

  • C#
    void StartListen()

    Starts listening for packets.

  • C#
    void StopListen()

    Stops listening for packets.

  • C#
    void Close()

    Stops all operations and releases all resources.

  • C#
    bool SendPacket(byte[] rawdata)

    Sends bytes contained in rawdata over the wire. The ethernet checksum will be automatically added prior to sending the packet. Returns true if send is successful, false otherwise.


  • C#
    bool IsListening

    true if the dotnetWinpcap object is listening, false otherwise.

  • C#
    string LastError

    Returns the last error encountered by the library, if any.

Event Support

delegate void ReceivePacket (object sender, PacketHeader p, byte[] s);
event ReceivePacket OnReceivePacket;

Once StartListen() is called, OnReceivePacket will start to fire on every packet encountered, until StopListen() is called, or Close() is called.

Delegate objects of the above signature may be attached to the OnReceivePacket event to receive notification and perform further processing, as demonstrated in the demo source code.


  • 28th May, 2003: Initial post
  • 25th Aug 2003 - Updated source code
  • 28th June, 2008: Updated source code
  • 24th March, 2009: Updated source code to include client code as requested by Ashin


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Victor Tan
Australia Australia
No Biography provided

Comments and Discussions

QuestionUnbalanced the stack error Pin
avramik31-Mar-16 12:06
Memberavramik31-Mar-16 12:06 
QuestionHow the source code works in visualstudio2012 Pin
Member 1123070212-Feb-15 19:31
MemberMember 1123070212-Feb-15 19:31 
GeneralMy vote of 5 Pin
Miklo B27-Oct-14 11:44
MemberMiklo B27-Oct-14 11:44 
QuestionNeed help to Show the Dump in RichTextBox instead of File Pin
Miklo B27-Oct-14 11:43
MemberMiklo B27-Oct-14 11:43 
QuestionAdding packet filtering Pin
Chris00319-Jun-12 8:22
MemberChris00319-Jun-12 8:22 
QuestionAccessViolationException Pin
Member 864901414-Jun-12 0:48
MemberMember 864901414-Jun-12 0:48 
GeneralCross threading Pin
Oj500025-May-11 4:48
MemberOj500025-May-11 4:48 
Generalhelp to convert to C++/CLI Pin
miki itz clutch18-Feb-11 1:54
Membermiki itz clutch18-Feb-11 1:54 
GeneralPcap.Net - a full WinPcap .NET wrapper Pin
brickner26-Feb-10 6:17
Memberbrickner26-Feb-10 6:17 
GeneralThanks!! Pin
yuriyag3-Feb-11 23:00
Memberyuriyag3-Feb-11 23:00 
GeneralWinCe Pin
MPH195621-Feb-10 0:12
MemberMPH195621-Feb-10 0:12 
GeneralWinCE Pin
MPH195621-Feb-10 0:10
MemberMPH195621-Feb-10 0:10 
GeneralC# Sample-Project dont works with c# 2008 Pin
eightbitwitch4-Feb-10 12:41
Membereightbitwitch4-Feb-10 12:41 
GeneralRe: C# Sample-Project dont works with c# 2008 Pin
PeterKM11-May-10 5:09
MemberPeterKM11-May-10 5:09 
AnswerRe: C# Sample-Project dont works with c# 2008 Pin
boblogan9-Sep-10 4:11
Memberboblogan9-Sep-10 4:11 
GeneralGet all Packet, inclusive Packets with Set FIN-Flag Pin
eightbitwitch4-Feb-10 7:17
Membereightbitwitch4-Feb-10 7:17 
GeneralSendPacket are not public - Not acces from VB9 Pin
eightbitwitch4-Feb-10 7:11
Membereightbitwitch4-Feb-10 7:11 
GeneralChanges required to run on Vista 64 in Visual Studio 2008 Pin
Umopepisdn4-Jan-10 23:43
MemberUmopepisdn4-Jan-10 23:43 
Generalthanks Pin
791671158-Sep-09 22:28
Member791671158-Sep-09 22:28 
General??? please help Pin
Jordan Wis15-Aug-09 16:28
MemberJordan Wis15-Aug-09 16:28 
GeneralMy vote of 1 Pin
legion_9-Jul-09 10:44
Memberlegion_9-Jul-09 10:44 
Generalcraft packet Pin
Huisheng Chen1-Jan-09 15:37
MemberHuisheng Chen1-Jan-09 15:37 
GeneralFull source uploaded Pin
Victor Tan28-Jun-08 7:25
MemberVictor Tan28-Jun-08 7:25 
GeneralRe: Full source uploaded Pin
Anil Maurya24-Oct-08 3:45
MemberAnil Maurya24-Oct-08 3:45 
GeneralRe: Full source uploaded Pin
Victor Tan24-Oct-08 4:30
MemberVictor Tan24-Oct-08 4:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.