Anp.Usb.Hid.Win32 1.0.0

dotnet add package Anp.Usb.Hid.Win32 --version 1.0.0
                    
NuGet\Install-Package Anp.Usb.Hid.Win32 -Version 1.0.0
                    
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="Anp.Usb.Hid.Win32" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Anp.Usb.Hid.Win32" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Anp.Usb.Hid.Win32" />
                    
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 Anp.Usb.Hid.Win32 --version 1.0.0
                    
#r "nuget: Anp.Usb.Hid.Win32, 1.0.0"
                    
#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 Anp.Usb.Hid.Win32@1.0.0
                    
#: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=Anp.Usb.Hid.Win32&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Anp.Usb.Hid.Win32&version=1.0.0
                    
Install as a Cake Tool

Anp.Usb.Hid.Win32

Managed Windows HID library for .NET Framework 4.8, .NET Standard 2.0, and .NET 8+. Focused on basic I/O for custom communication devices that use the "driverless" HID transport — not aimed at system HID devices (keyboards, mice, gamepads). Provides device discovery, hot-plug monitoring, and async I/O via Win32 P/Invoke.

Devices can be created from the built-in discovery or from an externally obtained device interface path (e.g. a config file, a saved preference, or a tool like PnpDeviceToolkit).

NuGet

Features

  • Discovery — enumerate HID devices, optionally filtered by USB VID/PID
  • Hot-plug monitoring — get notified when devices arrive or are removed (Windows 8+)
  • Async I/OReadInputReportAsync, WriteOutputReportAsync, ReadInputDataAsync, WriteOutputDataAsync with meaningful per-call timeouts
  • Sync wrappersReadInputReport, WriteOutputReport, WriteOutputData for simple use cases
  • Feature reportsReadFeatureReport, WriteFeatureReport (synchronous HID class driver calls)
  • Raw data and report access — work with HidReport (report ID + payload) or raw byte arrays; read exact byte counts across multiple reports via ReadInputDataAsync
  • Graceful disconnect handling — mid-I/O device removal throws HidDisconnectedException, leaving the device in a clean closed state
  • Device metadataProductName, SerialNumber, VendorId, ProductId, VersionNumber, Usage, UsagePage, report lengths — all probed at construction
  • Extensible — subclass HidDevice with OnOpened() / OnClosing() hooks for protocol initialization and teardown; use GetDevices<T> for typed discovery
  • Diagnostics — subscribe to HidDiag.Error for library-level error reporting

Quick Start

Discover devices

using Anp.Usb.Hid.Win32;

// All HID devices
var devices = HidDiscovery.GetDevices();

// Filter by USB VID/PID
var filtered = HidDiscovery.GetDevices(vendorId: 0x04D8, productId: 0x003F);

foreach (var device in devices)
{
    Console.WriteLine($"{device.ProductName} (VID={device.VendorId}, PID={device.ProductId}, Usage=0x{device.Usage:X2}, Page=0x{device.UsagePage:X4})");
    device.Dispose();
}

Open, write, read

using Anp.Usb.Hid.Win32;

// From discovery, config file, or any external source
var device = new HidDevice(@"\\?\HID#VID_04D8&PID_003F#...");
device.Open();

// Write an output report
var report = new HidReport(0x00, new byte[] { 0x01, 0x02, 0x03 });
await device.WriteOutputReportAsync(report);

// Read an input report
HidReport input = await device.ReadInputReportAsync(timeout: TimeSpan.FromSeconds(5));
Console.WriteLine($"Report ID: {input.ReportId}, Data: {BitConverter.ToString(input.Data)}");

device.Close();
device.Dispose();

Write and read raw data (no report framing)

// Write raw bytes — automatically framed into an output report
await device.WriteOutputDataAsync(new byte[] { 0x01, 0x02, 0x03 }, reportId: 0x00);

// Read exactly N bytes of input data (loops across multiple reports)
byte[] data = await device.ReadInputDataAsync(bytesToRead: 64);

Feature reports

// Write a feature report
var feature = new HidReport(0x01, new byte[] { 0xAA, 0xBB });
device.WriteFeatureReport(feature);

// Read a feature report
HidReport result = device.ReadFeatureReport(reportId: 0x01);

Monitor hot-plug events

using Anp.Usb.Hid.Win32;

using (var watcher = new HidWatcher(vendorId: 0x04D8))
{
    watcher.DeviceArrived += (s, e) => Console.WriteLine($"Connected: {e.DevicePath}");
    watcher.DeviceRemoved += (s, e) => Console.WriteLine($"Disconnected: {e.DevicePath}");
    watcher.StartWatching();

    Console.ReadLine(); // keep alive
}

Subclass for protocol initialization

public class MyHidDevice : HidDevice
{
    public MyHidDevice(string path) : base(path) { }

    protected override void OnOpened()
    {
        // Device is open — send initialization command, etc.
        WriteOutputDataAsync(new byte[] { 0x01 }).GetAwaiter().GetResult();
    }

    protected override void OnClosing()
    {
        // Device is about to close — send shutdown command, etc.
        WriteOutputDataAsync(new byte[] { 0xFF }).GetAwaiter().GetResult();
    }
}

// Use with discovery
var devices = HidDiscovery.GetDevices<MyHidDevice>(path => new MyHidDevice(path));

API Overview

HidDevice

Member Description
Open() / Close() Open and close the device for I/O
IsOpen Whether the device is currently open
IsOpenChanged Event raised after open/close transitions
DevicePath Device interface path (symbolic link)
VendorId / ProductId USB IDs from device attributes, or null
VersionNumber Device version number, or null
ProductName Device-reported product name, or null
SerialNumber Device-reported serial number, or null
Usage / UsagePage HID usage code and usage page for the top-level collection
InputReportLength Input report size in bytes (includes report ID)
OutputReportLength Output report size in bytes (includes report ID)
FeatureReportLength Feature report size in bytes (includes report ID)
InputReportBufferCount Get/set the HID class driver input report ring buffer size
WriteOutputReportAsync(HidReport) Write an output report asynchronously
WriteOutputDataAsync(IEnumerable<byte>) Write raw data as a framed output report
ReadInputReportAsync() Read one input report asynchronously
ReadInputDataAsync(int) Read exactly N bytes across multiple input reports
WriteOutputReport(HidReport) Write an output report (sync)
WriteOutputData(IEnumerable<byte>) Write raw data as a framed output report (sync)
ReadInputReport() Read one input report (sync)
WriteFeatureReport(HidReport) Write a feature report (synchronous HID class driver call)
ReadFeatureReport(byte) Read a feature report by report ID
OnOpened() / OnClosing() Virtual hooks for subclass initialization

All async methods accept an optional TimeSpan? timeout parameter (default: 2 seconds).

HidReport

Member Description
ReportId The report ID byte
Data The report payload (excludes report ID)
HidReport(byte reportId, byte[] data) Construct from report ID and data
HidReport(int reportSize) Construct an empty report of the specified size

HidDiscovery

Member Description
GetDevices(vendorId?, productId?, probeDeviceInfo?) Discover and return HidDevice instances
GetDevices<T>(factory, vendorId?, productId?) Discover with custom factory for derived types
GetDevicePaths(vendorId?, productId?) Return device interface paths only

HidWatcher

Member Description
DeviceArrived Event: a matching HID device was connected
DeviceRemoved Event: a matching HID device was disconnected
StartWatching() / StopWatching() Begin/end PnP monitoring (Windows 8+)

Diagnostics

HidDiag.Error += (sender, error) =>
{
    Console.WriteLine($"[{error.Member}] {error.Message} {error.Exception}");
};

Requirements

  • .NET Framework 4.8, .NET Standard 2.0, or .NET 8+ (Windows)
  • Windows Vista or later (discovery and I/O)
  • Windows 8 or later (HidWatcher hot-plug monitoring)

License

See LICENSE in the repository root.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net8.0-windows7.0 is compatible.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.8

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • net8.0-windows7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 135 3/29/2026