Skip to content

Managed wrappers around SetupAPI, Cfgmgr32, NewDev and DrvStore native APIs on Windows.

License

Notifications You must be signed in to change notification settings

ErisonNull/Nefarius.Utilities.DeviceManagement

 
 

Repository files navigation

Nefarius.Utilities.DeviceManagement

Build status Requirements Nuget Nuget

Managed wrappers around SetupAPI, Cfgmgr32, NewDev and DrvStore native APIs on Windows.

Features

  • Listen for device plugin and unplug events without depending on WinForms or WPF
  • Enumerate devices (present and absent)
  • Get and set various unified device properties
  • Convert various notations (Symlink to Instance ID etc.)
  • Enumerate and remove elements form the Driver Store

Documentation

Link to API docs.

Generating documentation

  • dotnet build -c:Release
  • dotnet tool install -g XMLDoc2Markdown
  • xmldoc2md .\bin\netstandard2.0\Nefarius.Utilities.DeviceManagement.dll .\docs\

Examples

Some usage examples of the core library features are presented below.

Enumerate all USB devices

The Devcon utility class offers helper methods to find devices.

var instance = 0;
// enumerate all devices that export the GUID_DEVINTERFACE_USB_DEVICE interface
while (Devcon.FindByInterfaceGuid(DeviceInterfaceIds.UsbDevice, out var path,
           out var instanceId, instance++))
{
    Console.WriteLine($"Path: {path}, InstanceId: {instanceId}");

    var usbDevice = PnPDevice
        .GetDeviceByInterfaceId(path)
        .ToUsbPnPDevice();

    Console.WriteLine($"Got USB device {usbDevice.InstanceId}");
}

Listen for new and removed USB devices

One or more instances of the DeviceNotificationListener can be used to listen for plugin and unplug events of various devices. This class has no dependency on WinForms or WPF and works in Console Applications and Windows Services alike.

var listener = new DeviceNotificationListener();

listener.DeviceArrived += Console.WriteLine;
listener.DeviceRemoved += Console.WriteLine;

// start listening for plugins or unplugs of GUID_DEVINTERFACE_USB_DEVICE interface devices
listener.StartListen(DeviceInterfaceIds.UsbDevice);

Get all driver packages in the Driver Store

var allDriverPackages = DriverStore.ExistingDrivers.ToList();

Remove all copies of mydriver.inf from the Driver Store

foreach (var driverPackage in allDriverPackages.Where(p => p.Contains("mydriver.inf", StringComparison.OrdinalIgnoreCase)))
{
    DriverStore.RemoveDriver(driverPackage);
}

Get Instance ID from a symbolic link (device path)

// e.g. the path "\\?\HID#VID_045E&PID_028E&IG_00#3&31f0e99d&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}"
// gets translated to instanceId "HID\VID_045E&PID_028E&IG_00\3&31f0e99d&0&0000"
var instanceId = PnPDevice.GetInstanceIdFromInterfaceId(path);

Get PnPDevice object from symbolic link

// example path: "\\?\HID#VID_046D&PID_C33F&MI_01&COL02#B&31580538&0&0001#{4D1E55B2-F16F-11CF-88CB-001111000030}"
PnPDevice device = PnPDevice.GetDeviceByInterfaceId(path);

Add a driver service to a device class' upper filters

string service = "HidHide";
DeviceClassFilters.AddUpper(DeviceClassIds.XnaComposite, service);

Sources & 3rd party credits

About

Managed wrappers around SetupAPI, Cfgmgr32, NewDev and DrvStore native APIs on Windows.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%