Click here to Skip to main content
15,888,351 members
Articles / Programming Languages / C#
Article

A USB HID Component for C#

Rate me:
Please Sign up or sign in to vote.
4.12/5 (86 votes)
22 Mar 2007CPOL1 min read 2M   68.5K   240   339
A component to communicate with a USB HID device

Introduction

This article is about a USB HID component which enables you to communicate with HID devices over USB. There is no default component available for USB at this moment, and this component should provide you with a good starting point when writing your own USB HID enabled applications.

This article provides a sample application as well as the component itself.

Background

The component is based on various sources. A good start for USB in C# is this website. Also the book USB COMPLETE (third edition) by Jan Axelson is a must read for anyone wishing to incorporate USB HID into her/his application.

The component is developed during a project at the Avans Hogeschool in 's-Hertogenbosch, The Netherlands.

Using the Code

In the provided sample application, there is a good demonstration on how to include the component. Moreover, the use of the component is very well demonstrated. The only thing that must be done in your own application is implementing the events.

You'll also have to override the following functions in your form, so that your program is USB aware. In the property box, you'll have to provide a vendor and product id of your USB device in order to detect the correct device.

C#
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
usb.RegisterHandle(Handle);
}
protected override void WndProc(ref Messagea m)
{
usb.ParseMessages(ref m);
base.WndProc(ref m); // pass message on to base form
}

Points of Interest

A mouse is always in use by Windows, and cannot be captured by your own application. This also applies to HID devices in use by other applications.

History

  • 22nd March, 2007: First version, currently in development

Updates will be posted if there is a need for them.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionit will always return null and it won't select any device... Pin
MEhran.NET17-May-11 0:31
MEhran.NET17-May-11 0:31 
AnswerRe: it will always return null and it won't select any device... Pin
mikah trent17-May-11 5:06
mikah trent17-May-11 5:06 
GeneralRe: it will always return null and it won't select any device... [modified] Pin
MEhran.NET18-May-11 0:12
MEhran.NET18-May-11 0:12 
GeneralRe: it will always return null and it won't select any device... Pin
mikah trent18-May-11 3:16
mikah trent18-May-11 3:16 
GeneralRe: it will always return null and it won't select any device... [modified] Pin
mtmonte8-Dec-11 5:45
mtmonte8-Dec-11 5:45 
GeneralModified UsbLibrary to support multiple USB devices of the same kind Pin
dr34m26-Apr-11 11:42
dr34m26-Apr-11 11:42 
QuestionHow Do I changeThe product and vendor ID Pin
Member 78197347-Apr-11 18:12
Member 78197347-Apr-11 18:12 
GeneralFails in BeginAsyncRead() on call BeginRead() with error 00000005 Pin
catchit20007-Apr-11 14:04
catchit20007-Apr-11 14:04 
I'm using an audio I/O interface box.
Device is found with VID & PID and a handle is created with CreateFile but fails to communicate with BeginAsyncRead

any ideas to how I can fix this
GeneralCan't find my HID Device - Product ID and Vendor ID Pin
josh_u28-Mar-11 1:47
josh_u28-Mar-11 1:47 
GeneralRe: Can't find my HID Device - Product ID and Vendor ID Pin
josh_u28-Mar-11 2:07
josh_u28-Mar-11 2:07 
Questionhow to communicate with iphone Pin
Ali Habib14-Mar-11 23:27
Ali Habib14-Mar-11 23:27 
Questionhow to adapt the library for Windows CE 6.0? Pin
MyJok10-Feb-11 23:33
MyJok10-Feb-11 23:33 
GeneralMy vote of 5 Pin
anming20-Jan-11 20:08
anming20-Jan-11 20:08 
GeneralRe: My vote of 5 Pin
Enie300014-Oct-11 9:19
Enie300014-Oct-11 9:19 
QuestionComposite devices (multiple HID) Pin
João Martins18-Jan-11 6:05
João Martins18-Jan-11 6:05 
Generalnew circuit Pin
Myn9216-Nov-10 20:23
Myn9216-Nov-10 20:23 
GeneralMy vote of 5 Pin
Shahbaz Ali13-Oct-10 1:06
Shahbaz Ali13-Oct-10 1:06 
GeneralCheckDevicePresent how to... Pin
transparency26-Sep-10 16:48
transparency26-Sep-10 16:48 
GeneralRe: CheckDevicePresent how to... Pin
Paul Norström13-Jan-16 21:47
Paul Norström13-Jan-16 21:47 
GeneralMultiple instances of the same device Pin
Thomas Kjørnes7-Jul-10 11:25
Thomas Kjørnes7-Jul-10 11:25 
GeneralNeed to close application to connect find device again [modified] Pin
witekm30-Jun-10 19:12
witekm30-Jun-10 19:12 
GeneralThanks Pin
Member 66486821-Jun-10 4:40
Member 66486821-Jun-10 4:40 
QuestionUsing in seperate class ? Pin
borisdekat6-Jun-10 8:07
borisdekat6-Jun-10 8:07 
AnswerRe: Using in seperate class ? Pin
Erik Haverkamp25-Mar-11 23:51
Erik Haverkamp25-Mar-11 23:51 
GeneralWindows 7 64-bit Pin
SamNasr6-May-10 4:39
SamNasr6-May-10 4:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.