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

 
GeneralRe: WinEr: 00000005 Pin
scarus23-Mar-10 1:22
scarus23-Mar-10 1:22 
AnswerRe: WinEr: 00000005 --- A solution to this problem Pin
fatih köksal22-Sep-10 7:27
fatih köksal22-Sep-10 7:27 
GeneralRe: WinEr: 00000005 --- A solution to this problem Pin
sellit6974-Apr-11 19:40
sellit6974-Apr-11 19:40 
GeneralRe: WinEr: 00000005 Pin
deadwood885-Jul-11 22:41
deadwood885-Jul-11 22:41 
Generalvb.net code Pin
ste199021-Dec-09 6:25
ste199021-Dec-09 6:25 
GeneralRe: vb.net code Pin
snuffybox24-Dec-09 22:09
snuffybox24-Dec-09 22:09 
GeneralUSBhidport Pin
J imran5-Dec-09 18:32
J imran5-Dec-09 18:32 
Questioncan I control the power line of usb port Pin
J imran5-Dec-09 16:59
J imran5-Dec-09 16:59 
QuestionIDs Pin
narutonaf12-Nov-09 16:00
narutonaf12-Nov-09 16:00 
AnswerRe: IDs Pin
narutonaf13-Nov-09 17:29
narutonaf13-Nov-09 17:29 
GeneralI can not receive data from device :( Pin
hande548-Oct-09 9:32
hande548-Oct-09 9:32 
GeneralRe: I can not receive data from device :( Pin
narutonaf18-Nov-09 10:09
narutonaf18-Nov-09 10:09 
GeneralRe: I can not receive data from device :( Pin
hande5424-Nov-09 8:50
hande5424-Nov-09 8:50 
GeneralRe: I can not receive data from device :( Pin
narutonaf24-Nov-09 9:43
narutonaf24-Nov-09 9:43 
GeneralRe: I can not receive data from device :( Pin
hande5424-Nov-09 10:44
hande5424-Nov-09 10:44 
GeneralRe: I can not receive data from device :( Pin
narutonaf25-Nov-09 6:49
narutonaf25-Nov-09 6:49 
GeneralGot ERROR_NO_TOKEN ( 1008) when CreateFile Pin
Nam Tu7-Oct-09 4:12
Nam Tu7-Oct-09 4:12 
GeneralRe: Got ERROR_NO_TOKEN ( 1008) when CreateFile Pin
Nam Tu8-Oct-09 0:34
Nam Tu8-Oct-09 0:34 
GeneralRe: Got ERROR_NO_TOKEN ( 1008) when CreateFile Pin
ijingle14-Nov-09 13:25
ijingle14-Nov-09 13:25 
QuestionDoes this work in Windows 2000? Pin
melntess6-Oct-09 9:45
melntess6-Oct-09 9:45 
AnswerRe: Does this work in Windows 2000? Pin
J imran5-Dec-09 16:30
J imran5-Dec-09 16:30 
QuestionIs it possible? Pin
rice-_-9-Sep-09 15:49
rice-_-9-Sep-09 15:49 
Generalproblem with USBlyzer Pin
Member 39111408-Sep-09 20:57
Member 39111408-Sep-09 20:57 
QuestionHow to get the friendly name of HID device? Pin
WuRichard27-Aug-09 8:20
WuRichard27-Aug-09 8:20 
AnswerRe: How to get the friendly name of HID device? [modified] Pin
yesman30-Aug-09 17:36
yesman30-Aug-09 17:36 

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.