How to list all attached USB devices in Visual C++

vinzenzweber picture vinzenzweber · Apr 29, 2010 · Viewed 8.2k times · Source

In Short: I need to detect hotplug events of my USB CDC device by PID/VID and get the corresponding virtual COM port which was created by Windows in Visual C++ and in the end create a dll.

I have a USB CDC device which I need to be notified of when connected/disconnected on Windows. My approach is to use RegisterDeviceNotification and an "invisible" Window to receive WM_DEVICECHANGE notifications. This part is working so far.

Now as far as I found out I need to get the list of USB devices that is plugged, iterate over it and filter out the devices with my PID/VID? I assume that I am then able to get more informations about the device including the COM port?

Is the only way to achieve my goal to use SetupDi calls in setupapi.h? Is using WDK / DDK the only way to achieve my goal?

As soon as that is working I open-source it on http://github.com/vinzenzweber/USBEventHandler. The Mac version is available already!

Answer

vinzenzweber picture vinzenzweber · May 6, 2010

After digging through tons of useless documentation at msdn and some debugging I found the missing link: SetupDi calls in setupapi.h: More infos as well as source code for Mac and Windows can be found in my USBEventHandler project at github.com with sources for Mac and Windows.