Is it possible to create (USB) device drivers with Visual C++?

This question pops up in the Visual C++ newsgroups from time to time.

You can’t. At least, you cannot directly perform USB communications. USB simply doesn’t work that way. They same is true for PCI, Firewire, SCSI, …

 

All USB IO has to be performed by a USB device driver. User space applications send IO requests to the device driver. The device driver will then perform some processing and complete the request.

 

The first possibility is that your USB device is of a standard device class in which case it doesn’t need a special device driver, or it needs its own device driver, but exports a standardized device interface.

 

A good example of this is a USB to serial converter. The device driver registers a standardized serial interface with the system.

Any user mode application can then use the normal .NET or win32 APIs for serial communications without having to care about the fact that the serial line is in fact connected to a USB port instead of being soldered to the motherboard.

 

The next possibility of course is that the device is a non-standard device. In that case you cannot access it through a standard windows high level API. Normally the device manufacturer will ship a high level API (e.g. a dll or ActiveX object) with the device driver to allow you to interact with the devices.

 

If no API is available, you will have to use the CreateFile, DeviceIoControl, ReadFile, WriteFile and CloseHandle functions (among others) to directly communicate with the device driver.

 

Lastly, it is possible that you have a custom device of which you only have the interface documentation, but no device driver or API. In that case you need to create your own device driver. For more information on writing device drivers you can read my articles (and others on www.codeproject.com) as a starting point:

http://www.codeproject.com/system/wdf_kmdf_basic.asp

http://www.codeproject.com/system/kmdf_osr_usb_fx2.asp

 

The first article explains all the steps involved in actually creating a device driver. The second article explains all the issues in programming a fully functional USB device driver. Both articles use the Microsoft’s new KMDF framework. It is relatively easy to learn if you have C/C++ experience.

 

There are some other toolkits available from 3d parties, but they cost money, and a bigger problem is that they are non-standard so you cannot ask other developers for support unless they use the same toolkit. Another problem is that the vendor might discontinue the toolkit, and leave you with nothing but unsupported software.

 

In any case, as soon as you need to develop a device driver you should not use VC++. Use the DDK build tools instead. They are the standard when it comes to building device drivers, and you can get good support on microsoft.public.development.device.drivers.

 

You can download the latest KMDF DDK here:

http://www.microsoft.com/whdc/driver/wdf/KMDF.mspx