Click here to Skip to main content
15,880,392 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 1.9M   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

 
AnswerRe: CloseHandle problem on Win XP SP3? Pin
gunnibaba16-Feb-11 13:41
gunnibaba16-Feb-11 13:41 
AnswerRe: CloseHandle problem on Win XP SP3? Pin
gunnibaba16-Feb-11 13:44
gunnibaba16-Feb-11 13:44 
GeneralUSB HID with Windows Mobile 5.0 Pin
mkohns26-Apr-09 12:38
mkohns26-Apr-09 12:38 
GeneralSourceForge Site Does Not Have Source Pin
mike2orb14-Apr-09 12:30
mike2orb14-Apr-09 12:30 
QuestionEndpoint number Pin
kaiwi6669-Apr-09 3:26
kaiwi6669-Apr-09 3:26 
GeneralNeed to develop a larger application using HID and USB , need contact Wimar Pin
Horacio Duhart2-Apr-09 9:02
Horacio Duhart2-Apr-09 9:02 
GeneralOutput Report and Specifying Endpoints [modified] Pin
Christopher Holland16-Mar-09 10:54
Christopher Holland16-Mar-09 10:54 
GeneralRe: Output Report and Specifying Endpoints Pin
Christopher Holland20-Mar-09 7:59
Christopher Holland20-Mar-09 7:59 
GeneralRe: Output Report and Specifying Endpoints Pin
dberteanu3-Jun-09 4:20
dberteanu3-Jun-09 4:20 
GeneralDebugging in VS C# 2008 Express Pin
LarryRPark9-Mar-09 16:32
LarryRPark9-Mar-09 16:32 
Questioncan't it send data to printer? [modified] Pin
Bathildis Baek3-Mar-09 21:33
Bathildis Baek3-Mar-09 21:33 
AnswerRe: can't it send data to printer? Pin
eeyrw19-Jun-09 8:51
eeyrw19-Jun-09 8:51 
QuestionHow to get connected device info Pin
barmagan26-Feb-09 2:16
barmagan26-Feb-09 2:16 
GeneralFeature Reports Pin
rnmisrahi22-Jan-09 10:43
rnmisrahi22-Jan-09 10:43 
GeneralMultiple HID devices capturing before keystroke injection Pin
GZ123420-Jan-09 4:55
GZ123420-Jan-09 4:55 
QuestionHow to disconnect a device ? Pin
tahiticecil12-Jan-09 1:04
tahiticecil12-Jan-09 1:04 
AnswerRe: How to disconnect a device ? Pin
barmagan26-Feb-09 2:01
barmagan26-Feb-09 2:01 
GeneralArray out of bound error Pin
harshanahnd6-Jan-09 15:51
harshanahnd6-Jan-09 15:51 
AnswerRe: Array out of bound error Pin
Yuriy Opryshko28-Jan-09 4:22
Yuriy Opryshko28-Jan-09 4:22 
GeneralUnable to receive data Pin
nighteagle30-Dec-08 0:55
nighteagle30-Dec-08 0:55 
GeneralRe: Unable to receive data Pin
jzonthemtn31-Dec-08 7:57
jzonthemtn31-Dec-08 7:57 
QuestionCan keyboard be captured? Pin
BrianMarshall29-Dec-08 6:55
BrianMarshall29-Dec-08 6:55 
GeneralBarcode Scanner Create File Fails Pin
murtaza dhari26-Dec-08 8:25
murtaza dhari26-Dec-08 8:25 
GeneralRe: Barcode Scanner Create File Fails Pin
BrendenJM3-Aug-09 9:53
BrendenJM3-Aug-09 9:53 
GeneralRe: Barcode Scanner Create File Fails Pin
bwechner18-Aug-09 0:32
bwechner18-Aug-09 0:32 

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.