Receiving data from a USB device in C or C++

I need a list of all plugged in USB devices and have the user select one to let the console application receive any data the USB device sends. I can then start playing around with the data in my program. I don't want to use library's, only standard C++ functions, and the program should work in Windows 98.

8,256 8 8 gold badges 39 39 silver badges 90 90 bronze badges asked Jun 5, 2010 at 18:34 7,101 5 5 gold badges 35 35 silver badges 55 55 bronze badges There are no Standard C++ functions to do this. Commented Jun 5, 2010 at 18:38 I'm not sure. Can you please tell me how it can be done then. Commented Jun 5, 2010 at 18:40 why don't you want to use libraries? Commented Jun 5, 2010 at 18:45 I want to know the background of it and write own code. Commented Jun 5, 2010 at 18:49 You will have to use Windows API. Also check out the USB protocol. Commented Jun 5, 2010 at 18:55

5 Answers 5

This is a very persistent question in forums and programming Q+A sites. Never with a happy ending. The B in USB means bus. That is a term in computer hardware design to describe an electrical interface for electronic devices to exchange data. It plays the exact same role as, say, the PCI (express) bus inside your machine. Since it is an electrical specification first and foremost, USB supports a very large number of types of devices. Anything from a wireless network adapter, modem, flash memory card to a teapot warmer. Just about the only kinds of devices that it doesn't handle well are ones that require a very large bandwidth, like a video adapter.

The USB specification has a very elegant protocol specification that describes how devices can share the bus and how they can exchange data. That protocol spec however does not describe the format of the data at all, it merely defines the notion of being able to deliver chunks of bytes. It is up to the device itself to give a meaning to those bytes.

On the machine end, you need software to interpret those bytes and make the machine do something interesting with them. That requires a device driver. Just like your video card and your network interface card require a device driver. Obviously a video driver is very different from a NIC driver. The same is true for USB drivers, there is little commonality.

If you want to write software that treats USB devices similar then you need to write that at the level where they still have something in common. That's at the USB controller level, you could write a filter driver that injects itself in the USB driver stack and peeks at the I/O request packets between the controller and the device driver. Similar to, say, the winpcap filter driver that spies on TCP/IP traffic. There isn't much of anything interesting to see though, you'd be staring at the blobs of bytes that pass back and forth. It is a much bigger problem than winpcap, at least it sees bytes fly by whose meaning is documented somewhere in an RFC. That's not the case for USB, the company that makes the USB device is also usually the device driver supplier. They keep the internal format undocumented.

Writing filter drivers requires pretty advanced skills, there are lots of pain points. Like crashing the operating system when you make a simple mistake. There has also been considerable flux in the Windows Driver Model lately, USB drivers have been getting moved into ring 3 (user mode) to keep the operating system stable.

To get started, download the Windows WDK (aka "DDK") and read Walter Oney's books. Preferably all of them.