Detecting USB devices
-
Hello, Most code used to detect USB relies on a Windows form. The program must register its interest in the USB insertion/removal events by sending its main window handle to RegisterDeviceNotification. What I'm trying to do is make a class library which exposes DeviceIsConnected property without having to use any forms. Any idea if this can be done, and if so how? Thanks! Mart
-
Hello, Most code used to detect USB relies on a Windows form. The program must register its interest in the USB insertion/removal events by sending its main window handle to RegisterDeviceNotification. What I'm trying to do is make a class library which exposes DeviceIsConnected property without having to use any forms. Any idea if this can be done, and if so how? Thanks! Mart
I would use WMI for that task. How To: (Almost) Everything In WMI via C# - Part 3: Hardware[^]. Example:
// Create event query to be notified within 1 second of
// a change in a service
WqlEventQuery query = new WqlEventQuery(eventName,
new TimeSpan(0, 0, 1),
"TargetInstance isa \"Win32_USBControllerDevice\"");// Initialize an event watcher and subscribe to events that match this query
EventWatcher = new ManagementEventWatcher();
EventWatcher.EventArrived += eventHandler;
EventWatcher.Query = query; -
I would use WMI for that task. How To: (Almost) Everything In WMI via C# - Part 3: Hardware[^]. Example:
// Create event query to be notified within 1 second of
// a change in a service
WqlEventQuery query = new WqlEventQuery(eventName,
new TimeSpan(0, 0, 1),
"TargetInstance isa \"Win32_USBControllerDevice\"");// Initialize an event watcher and subscribe to events that match this query
EventWatcher = new ManagementEventWatcher();
EventWatcher.EventArrived += eventHandler;
EventWatcher.Query = query;Thanks Kim! I went through the WMI hardware tutorial and was able to run the sample code, but it listed every piece of hardware in the PC except for the one I was interested in, i.e. a USB Input Device under Human Interface Devices. The HID device has this ID: USB\VID_0000&PID_0000&REV_0100. Any idea why I can't communicate with the device using WMI? Thanks again, Mart