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

 
QuestionUSB Polling Time - USB HUB Pin
bublabla20-Jul-09 20:33
bublabla20-Jul-09 20:33 
GeneralMy vote of 2 Pin
jasona2218-Jun-09 5:21
jasona2218-Jun-09 5:21 
JokeDoesn't work on 64 bit Vista. Pin
melntess28-May-09 10:14
melntess28-May-09 10:14 
GeneralRe: Doesn't work on 64 bit Vista. Pin
melntess28-May-09 12:25
melntess28-May-09 12:25 
GeneralRe: Doesn't work on 64 bit Vista. Pin
Yuriy Opryshko17-Aug-09 23:34
Yuriy Opryshko17-Aug-09 23:34 
GeneralRe: Doesn't work on 64 bit Vista. Pin
Yuriy Opryshko18-Aug-09 1:06
Yuriy Opryshko18-Aug-09 1:06 
GeneralOMG. You are my savior. Pin
melntess27-May-09 19:12
melntess27-May-09 19:12 
QuestionCloseHandle problem on Win XP SP3? Pin
Henrik Danielsson9-May-09 8:52
Henrik Danielsson9-May-09 8:52 
Hi!

When the CloseHandle in the HIDDevice.Dispose function is called on my Vista computer the Async Callback is called and the handle is released. But when the same code is running on my Windows XP SP3 computer the Async Callback is not called and the handle is not released. This makes it imposible to open a new handle to the HID device unless it's first disconnected and then reconnected to the computer, or when I'm debugging I have to restart the app. I've tried to compile with .NET 2.0, 3.0, 3.5, it makes no difference.

The ErrorCode from Marshal.GetLastWin32Error is 0x3e5 after the CloseHandle on both Vista and my XP. When I try to open a new handle on Vista that is success (ErorCode 0x00) but on my WinXP I get ErrorCode 0x20.

Please help me!

Regards Henrik Danielsson
AnswerRe: CloseHandle problem on Win XP SP3? Pin
gunnibaba16-Feb-11 12:46
gunnibaba16-Feb-11 12:46 
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 

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.