Oakrey.Network.PacketCapture.Wrapper 2.0.3

dotnet add package Oakrey.Network.PacketCapture.Wrapper --version 2.0.3
                    
NuGet\Install-Package Oakrey.Network.PacketCapture.Wrapper -Version 2.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Oakrey.Network.PacketCapture.Wrapper" Version="2.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Oakrey.Network.PacketCapture.Wrapper" Version="2.0.3" />
                    
Directory.Packages.props
<PackageReference Include="Oakrey.Network.PacketCapture.Wrapper" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Oakrey.Network.PacketCapture.Wrapper --version 2.0.3
                    
#r "nuget: Oakrey.Network.PacketCapture.Wrapper, 2.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Oakrey.Network.PacketCapture.Wrapper@2.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Oakrey.Network.PacketCapture.Wrapper&version=2.0.3
                    
Install as a Cake Addin
#tool nuget:?package=Oakrey.Network.PacketCapture.Wrapper&version=2.0.3
                    
Install as a Cake Tool

Oakrey.Network.PacketCapture.Wrapper

A managed .NET wrapper for WinPcap/Npcap on Windows. Exposes packet capture, Berkeley Packet Filter (BPF) compilation, queue-based transmission, device configuration, and capture statistics through a P/Invoke layer over wpcap.dll.


Features

  • Packet capture via pcap_next_ex and pcap_dispatch callback loop
  • Berkeley Packet Filter (BPF) compilation and application (CompileFilter, SetFilter)
  • Raw packet transmission (SendPacket) and queue-based batch transmission (SendQueue, AddPacket)
  • Device configuration: kernel buffer size, capture mode, blocking/non-blocking mode, minimum copy size
  • Capture statistics retrieval (GetCaptureStatistics)
  • Network address parsing: IPv4, IPv6, MAC (AddressContainer)
  • Safe handle types for all native resources (DeviceHandle, QueueHandle, DumpHandle, StatisticHandle)
  • Utility extensions for unmanaged memory allocation and marshalling (PointerExtension)

Requirements

  • .NET 10 (Windows only, net10.0-windows)
  • Npcap or WinPcap installed on the target machine (wpcap.dll must be present)
  • Administrator or elevated privileges are typically required for live capture

Installation

NuGet Package Manager

  1. Open Visual Studio.
  2. Go to Tools > NuGet Package Manager > Manage NuGet Packages for Solution.
  3. Search for Oakrey.Network.PacketCapture.Wrapper and install.

.NET CLI

dotnet add package Oakrey.Network.PacketCapture.Wrapper

Package Manager Console

Install-Package Oakrey.Network.PacketCapture.Wrapper

Architecture

graph TD
    Consumer["Consumer code"]
    PCC["PacketCaptureWrapper\n(static extension methods)"]
    WCW["WindowsCaptureWrapper\n(Windows-specific extensions)"]
    SNM["SafeNativeMethods\n(P/Invoke to wpcap.dll)"]
    Handles["Handles\nDeviceHandle / QueueHandle\nDumpHandle / StatisticHandle"]
    DataTypes["DataTypes\nBPF / CaptureStatistics\nPacketCaptureHeader / etc."]
    Wpcap["wpcap.dll\n(Npcap / WinPcap)"]

    Consumer --> PCC
    Consumer --> WCW
    PCC --> SNM
    WCW --> SNM
    PCC --> Handles
    WCW --> Handles
    SNM --> Handles
    SNM --> DataTypes
    SNM --> Wpcap

Key types

Type Purpose
PacketCaptureWrapper Cross-platform pcap operations as DeviceHandle extension methods
WindowsCaptureWrapper Windows-specific operations (send queue, kernel buffer, capture mode)
SafeNativeMethods Internal P/Invoke declarations against wpcap.dll
DeviceHandle Safe handle wrapping a pcap_t*
QueueHandle Safe handle wrapping a pcap_send_queue*
AddressContainer Parsed network address (IPv4, IPv6, or MAC)
PointerExtension Unmanaged memory helpers (Alloc, Free, ToStruct, ToPointer)
PacketCaptureException Exception thrown on capture or filter errors
StatisticsException Exception thrown when statistics retrieval fails

Usage

The API is exposed as extension methods on DeviceHandle. All handle types implement IDisposable and should be used inside using blocks.

Read packets in a dispatch loop

// DeviceHandle is obtained from a higher-level device abstraction
// (e.g. Oakrey.Network.PacketCapture.Device).
using DeviceHandle handle = ...; // open device

handle.SetFilter("ip and tcp");

ReadResult result = handle.ReadPackets(
    (param, header, data) =>
    {
        PacketCaptureHeader pktHeader = header.ToStruct<PacketCaptureHeader>();
        byte[] bytes = new byte[pktHeader.CaptureLength];
        Marshal.Copy(data, bytes, 0, bytes.Length);
        // process bytes
    },
    packetCount: 100);

Read one packet at a time

IntPtr header = IntPtr.Zero;
IntPtr data   = IntPtr.Zero;
handle.GetNextPacketPointers(ref header, ref data);

Compile and apply a BPF filter manually

IntPtr bpfProgram = handle.CompileFilter("udp port 53", mask: 0);
// bpfProgram is freed automatically after SetFilter
handle.SetFilter("udp port 53");

Send a raw packet

byte[] rawPacket = ...; // pre-built Ethernet frame
handle.SendPacket(rawPacket, rawPacket.Length);

Queue-based batch transmission (Windows only)

using QueueHandle queue = ...;

PacketCaptureHeader header = new() { CaptureLength = (uint)packet.Length, PacketLength = (uint)packet.Length };
queue.AddPacket(packet, header);

handle.SendQueue(queue, SendMode.Synchronized);

Capture statistics

CaptureStatistics stats = handle.GetCaptureStatistics();
Console.WriteLine($"Received: {stats.Received}, Dropped: {stats.Dropped}");

Device configuration (Windows only)

handle.SetKernelBufferSize(4 * 1024 * 1024);   // 4 MB kernel buffer
handle.SetMode(CaptureMode.Capture);
handle.SetBlocking(false);
handle.MinimumDataSizeToCopy(1);

Library version

string? version = PacketCaptureWrapper.Version;
// e.g. "Npcap version 1.79, based on libpcap version 1.10.4"

Development notes

  • The project enables AllowUnsafeBlocks for unmanaged memory operations.
  • SafeNativeMethods is decorated with [SuppressUnmanagedCodeSecurity] for performance; callers are responsible for security validation.
  • Most P/Invoke declarations use [LibraryImport] (source-generated marshalling, .NET 7+). A few fallbacks use [DllImport] where source generation is not applicable.
  • PointerExtension provides Alloc/Free/ToStruct helpers used throughout the wrapper to reduce Marshal boilerplate.
  • This library is intentionally low-level. For a higher-level device abstraction see Oakrey.Network.PacketCapture.Device.

Project information

Property Value
Author Oakrey
Company Oakrey (oakrey.cz)
License MIT
Target framework net10.0-windows
NuGet Oakrey.Network.PacketCapture.Wrapper
Repository Azure DevOps
Product Compatible and additional computed target framework versions.
.NET net10.0-windows7.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Oakrey.Network.PacketCapture.Wrapper:

Package Downloads
Oakrey.Network.PacketCapture.Device

The "WindowCaptureDevice" project provides a high-level abstraction for managing live packet capture devices on Windows.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.3 38 5/15/2026
2.0.2 127 2/2/2026
2.0.1 117 2/2/2026
2.0.0 440 11/18/2025
1.0.0 305 4/17/2025