Click here to Skip to main content
15,892,746 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.6K   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

 
QuestionMustek UPS via USB Pin
Member 23494633-Dec-08 1:42
Member 23494633-Dec-08 1:42 
QuestionError when trying to Run - 'System.Security.SecurityException' occurred in mscorlib.dll Pin
Member 23494633-Dec-08 0:02
Member 23494633-Dec-08 0:02 
QuestionTypo? Pin
Reelix26-Nov-08 11:16
Reelix26-Nov-08 11:16 
GeneralUsbLibrary.dll' doesn't contain any UserControl types. Pin
harshanahnd21-Nov-08 17:57
harshanahnd21-Nov-08 17:57 
GeneralRe: UsbLibrary.dll' doesn't contain any UserControl types. Pin
BobbyCrougar25-Nov-08 8:26
BobbyCrougar25-Nov-08 8:26 
GeneralRe: UsbLibrary.dll' doesn't contain any UserControl types. Pin
markjuggles6-Jun-10 11:49
markjuggles6-Jun-10 11:49 
GeneralRe: UsbLibrary.dll' doesn't contain any UserControl types. Pin
TheMattster20-Sep-10 10:51
TheMattster20-Sep-10 10:51 
GeneralUsing the library in a Windows Service Pin
jfos16-Nov-08 14:01
jfos16-Nov-08 14:01 
Hello,

Has anyone had any experience using this library in a Windows Service?

In particular, a Windows Service does not have a window and thus the two overrides that we are told to do (OnHandleCreated and WndProc) don't exist in that context. So far I have found that I will need to call RegisterDeviceNotification to handle what WndProc is doing and yes I have found the c# declaration for the call, but I don't know what to pass for the parameters (for example, "A handle to the window or service that will receive device events"... a handle to the service?)

Then, anyone have any clues as to how to deal with the OnHandleCreated override?

Thanks in advance,
Jim
GeneralSpecifiedDevice Pin
canhelie13-Nov-08 9:33
canhelie13-Nov-08 9:33 
QuestionHow to get Documentation.chm help file to work? Pin
Bill Ewing5-Nov-08 11:46
Bill Ewing5-Nov-08 11:46 
AnswerRe: How to get Documentation.chm help file to work? Pin
Member 366413225-Nov-08 9:33
Member 366413225-Nov-08 9:33 
GeneralRe: How to get Documentation.chm help file to work? Pin
markjuggles6-Jun-10 12:03
markjuggles6-Jun-10 12:03 
GeneralHID USB reader with multiple tracks Pin
belzer29-Oct-08 15:22
belzer29-Oct-08 15:22 
GeneralHIDDevice.Initialise Pin
Trhacz3-Oct-08 11:26
Trhacz3-Oct-08 11:26 
GeneralRe: HIDDevice.Initialise Pin
peterspeybrouck9-Dec-09 13:12
peterspeybrouck9-Dec-09 13:12 
GeneralCan't get it to work [modified] Pin
mscheaf16-Sep-08 5:28
mscheaf16-Sep-08 5:28 
GeneralRe: Can't get it to work Pin
mscheaf16-Sep-08 5:46
mscheaf16-Sep-08 5:46 
GeneralRe: Can't get it to work Pin
thompsons16-Sep-08 7:03
thompsons16-Sep-08 7:03 
GeneralRe: Can't get it to work Pin
Bill Ewing7-Nov-08 3:32
Bill Ewing7-Nov-08 3:32 
Questionnot recognizing my usb product id and vendor id Pin
Member 37226873-Sep-08 6:17
Member 37226873-Sep-08 6:17 
AnswerRe: not recognizing my usb product id and vendor id Pin
Trhacz3-Oct-08 11:01
Trhacz3-Oct-08 11:01 
GeneralThis code recognized my device immediately Pin
Bill Ewing7-Nov-08 3:28
Bill Ewing7-Nov-08 3:28 
GeneralRe: not recognizing my usb product id and vendor id Pin
yesman14-Jul-09 20:18
yesman14-Jul-09 20:18 
AnswerRe: not recognizing my usb product id and vendor id Pin
sarunasg26-Nov-09 22:04
sarunasg26-Nov-09 22:04 
QuestionUSB-At89c5131 code Pin
Nycil29-Jul-08 21:06
Nycil29-Jul-08 21:06 

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.