Click here to Skip to main content
15,891,943 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

 
GeneralRe: FindSpecifiedDevice Problem Pin
tolgakayahan23-Aug-09 0:36
tolgakayahan23-Aug-09 0:36 
GeneralRe: FindSpecifiedDevice Problem Pin
wjjwlhk28-Apr-10 0:08
wjjwlhk28-Apr-10 0:08 
GeneralRe: FindSpecifiedDevice Problem Pin
SPI9-Jan-12 6:45
SPI9-Jan-12 6:45 
GeneralHID Device Name Pin
Iwan Budihalim16-Aug-07 2:48
Iwan Budihalim16-Aug-07 2:48 
GeneralRe: HID Device Name Pin
wimar16-Aug-07 7:52
wimar16-Aug-07 7:52 
GeneralRe: HID Device Name Pin
Iwan Budihalim21-Aug-07 23:02
Iwan Budihalim21-Aug-07 23:02 
QuestionCall CreateFile() failed! Pin
moonz_wu8-Aug-07 20:24
moonz_wu8-Aug-07 20:24 
AnswerRe: Call CreateFile() failed! Pin
wimar8-Aug-07 20:36
wimar8-Aug-07 20:36 
Hi Moonz_wu,

Are you using a hid device that is alway in use and locked by Windows? For example a mouse or keyboard. Because of devices from this type you can find the device path but you can't access it and will give the error you describe.

Greetings Wimar
GeneralRe: Call CreateFile() failed! Pin
moonz_wu8-Aug-07 21:26
moonz_wu8-Aug-07 21:26 
GeneralRe: Call CreateFile() failed! Pin
wimar16-Aug-07 7:43
wimar16-Aug-07 7:43 
GeneralRe: Call CreateFile() failed! Pin
TomXP411_112-Feb-08 5:34
TomXP411_112-Feb-08 5:34 
GeneralRe: Call CreateFile() failed! Pin
michaelrterry10-Aug-07 12:52
michaelrterry10-Aug-07 12:52 
GeneralRe: Call CreateFile() failed! Pin
wimar16-Aug-07 7:44
wimar16-Aug-07 7:44 
GeneralRe: Call CreateFile() failed! Pin
michaelrterry6-Sep-07 11:33
michaelrterry6-Sep-07 11:33 
GeneralRe: Call CreateFile() failed! Pin
Z789512316-Jul-09 16:07
Z789512316-Jul-09 16:07 
GeneralUSB/HID Pin
sswcharlie6-Jun-07 13:07
sswcharlie6-Jun-07 13:07 
GeneralRe: USB/HID Pin
wimar12-Jun-07 22:02
wimar12-Jun-07 22:02 
GeneralRe: USB/HID Pin
sswcharlie9-Aug-07 16:06
sswcharlie9-Aug-07 16:06 
QuestionMultiple identical devices? Pin
antmason29-May-07 4:59
antmason29-May-07 4:59 
AnswerRe: Multiple identical devices? Pin
wimar30-May-07 0:09
wimar30-May-07 0:09 
Generalcommunicating other than HID Pin
ardaarda15-May-07 22:10
ardaarda15-May-07 22:10 
GeneralRe: communicating other than HID Pin
wimar16-May-07 5:50
wimar16-May-07 5:50 
QuestionA problem is observed Pin
s_stavrev24-Apr-07 5:51
s_stavrev24-Apr-07 5:51 
QuestionUSB HID Pin
kingdom197713-Apr-07 2:45
kingdom197713-Apr-07 2:45 
AnswerRe: USB HID Pin
wimar15-Apr-07 21:41
wimar15-Apr-07 21:41 

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.