Home >Backend Development >C++ >How Can I Programmatically Retrieve a List of Connected USB Devices in Windows?

How Can I Programmatically Retrieve a List of Connected USB Devices in Windows?

DDD
DDDOriginal
2025-01-25 00:52:10619browse

How Can I Programmatically Retrieve a List of Connected USB Devices in Windows?

Programmatically Accessing Connected USB Devices on Windows

Many applications require the ability to identify and interact with USB devices connected to a Windows PC. This guide demonstrates a simple method to retrieve a list of all connected USB devices using the System.Management namespace.

Utilizing the System.Management Namespace

To manage hardware, including USB devices, your project needs a reference to the System.Management namespace. This provides access to classes and methods for hardware management.

Enumerating USB Devices via WMI

The connected USB devices are enumerated using Windows Management Instrumentation (WMI). WMI allows querying and accessing hardware components. The code below uses ManagementObjectSearcher to query for all instances of Win32_USBHub. Each instance represents a USB device, providing details like Device ID, PNP Device ID, and description.

Custom USB Device Information Object

A USBDeviceInfo class is defined to store device information (DeviceID, PnpDeviceID, Description). The WMI query results are stored in instances of this class.

Summary

The System.Management namespace and WMI queries provide a straightforward way to list connected USB devices and access their properties on Windows. The code example offers a clear method for developers to interact with USB devices, building applications that integrate seamlessly with external hardware.

The above is the detailed content of How Can I Programmatically Retrieve a List of Connected USB Devices in Windows?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn