16

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.

7
  • There are no Standard C++ functions to do this.
    – anon
    Commented Jun 5, 2010 at 18:38
  • I'm not sure... Can you please tell me how it can be done then.
    – Midas
    Commented Jun 5, 2010 at 18:40
  • why don't you want to use libraries?
    – OlimilOops
    Commented Jun 5, 2010 at 18:45
  • 1
    I want to know the background of it and write own code.
    – Midas
    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

27

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.

4
  • Thanks, but I don't really want to make a driver. Is that a problem? And are you sure the book is good for me? I live in Belgium and I can't buy it here.
    – Midas
    Commented Jun 5, 2010 at 20:33
  • 1
    Yes, that would be a problem. Amazon.com delivers to Belgium afaik. You probably don't want to spend too much money on it, happy ending and all that. I know you've got good libraries available in university towns. Commented Jun 5, 2010 at 20:37
  • I still don't know. I just want to make a program, not a driver. :/
    – Midas
    Commented Jun 5, 2010 at 20:46
  • 1
    @Hans: My dad isn't very much interested in what I do on my PC. I didn't ever write my own program. I know other languages very well, like PHP and JavaScript. I know, a lot of fun, but what I have in my mind to realize in C++ is much more interesting and fun than a Freecell game. I think this is a very basic thing to do (finding out which USB devices are connected and receive data), and I don't see why it won't be possible with C++.
    – Midas
    Commented Jun 5, 2010 at 21:38
3

How can one write a program for USB, wanting to understand the background of it, while not wanting to read much about it, not wanting to use a library, all in the same time? Anyway. there is a project "libUSB Win32" from Stefan Meyer not under heavy development at the moment, but written in C maybe this could be something for you, it has also the ability to run under ancient windows versions ;) you can find it here:
http://sourceforge.net/apps/trac/libusb-win32/wiki
a while ago i have written this in VB6 that makes use of "libUSB Win32" don't know if it can be useful for you:
http://www.activevb.de/cgi-bin/upload/download.pl?id=3061

3
  • libUSB is based on standard C++ code, isn't it? I could write this myself, adapted to my program. I could maybe take a look at the source.
    – Midas
    Commented Jun 5, 2010 at 19:51
  • The URL seems to have changed. Your's doesn't work anymore. But using Google I found this link to be working: sourceforge.net/projects/libusb-win32
    – Scindix
    Commented Jan 19, 2018 at 17:47
  • Also the wiki mentiions the following: "libusb-win32 is now in Bug-Fix-Only maintenance mode. It is recommended new project[s] should migrate to libusb-1.0 API and use libusb Windows (libusb.info) instead."
    – Scindix
    Commented Jan 19, 2018 at 17:52
3

Here's something to get you started:

And for crying out loud update to an OS that's not from the last millenium. I hear Linux has great support for USB.

4
  • 1
    +1 for the update - Windows 98 is ancient. (+1 for the implied Linux recommendation.) If people would update, we'd get cool stuff faster.
    – Thanatos
    Commented Jun 5, 2010 at 19:09
  • I read a lot today without any result and I don't feel to read a whole book only about USB specifications. Can you maybe give me an example code of what I want to do? It can be very basic.
    – Midas
    Commented Jun 5, 2010 at 19:30
  • @Midas: Low-level stuff is not simple. I, for one, have spent a lot of time just reading the specs to get a better idea about how USB works. If you're looking for learn about things, it's sometimes better to read the docs instead of jumping into example code. Commented Jun 5, 2010 at 19:57
  • OK, I'll read it, but then I still don't know how to program for USB in C++.
    – Midas
    Commented Jun 5, 2010 at 20:16
0

I simply 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.

Getting the list isn't the big problem, it's receiving the data.

I know you don't want to write a driver, but this is what drivers do: receive data from a device.

0

The advice you're getting here is on the mark; USB is not easy, believe me... I'm working on a USB project but from the back-end. Seems to me that you need to be looking at the back-end too, that's where you really don't have to deal with libraries and all that, but you won't be able to get around the reading whatever you do.

Don't get discouraged yet, but you do need to wet you feet before you jump in and drown. USB2.0 and USB3.0 are quite dry, but I found OSDev Wiki and usbmadesimple to be useful stepping stones.

If you still need to be in the PC, then you should consider trying to get a hold of some open source usb code for maybe a USB sniffer if you can find it.

1
  • Please don't add signatures to your posts. (As per the "Do not use signature, taglines, or greetings." section of the help content.) Commented Aug 3, 2013 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.