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

 
GeneralRe: Windows 7 64-bit Pin
dvrotsos3-Sep-10 9:22
dvrotsos3-Sep-10 9:22 
GeneralRe: Windows 7 64-bit Pin
SamNasr3-Sep-10 9:48
SamNasr3-Sep-10 9:48 
GeneralRe: Windows 7 64-bit Pin
dvrotsos3-Sep-10 12:02
dvrotsos3-Sep-10 12:02 
GeneralRe: Windows 7 64-bit Pin
PhantomHawk30-Sep-10 6:43
PhantomHawk30-Sep-10 6:43 
GeneralRe: Windows 7 64-bit Pin
mikah trent17-May-11 4:57
mikah trent17-May-11 4:57 
GeneralRe: Windows 7 64-bit Pin
Member 1003725110-Feb-15 12:23
Member 1003725110-Feb-15 12:23 
GeneralRe: Windows 7 64-bit Pin
melntess8-Jul-11 11:11
melntess8-Jul-11 11:11 
QuestionSolution: Losing Input Reports? Pin
faav4-May-10 10:24
faav4-May-10 10:24 
I was using this great library with success but then noticed that when sending large amounts of data, I would randomly lose some percentage of the packets. Apparently me .NET program was not servicing the arriving data fast enough and the Windows ReadFile buffer was overflowing and throwing out the oldest reports.

Jan Axelson describes how to increase the default number of reports that Windows stores before overflowing:

*big* thanks to Jan and Wimar for making this info/code available!!!

http://www.lvr.com/hidfaq.htm



How large is the ReadFile buffer?

Under Windows 98 Gold, 2 reports. Under Windows 98 SE, Windows 2000 and Windows Me, the default is 8 reports. Under Windows XP, the default is 32 reports. Under all but Windows 98 Gold, you can change the defaults with HidD_SetNumInputBuffers. The maximum size under Windows XP and later is 512. The maximum size under Windows 2000 is 200. Use HidD_GetNumInputBuffers to read the buffer size.

How can I keep my application from losing reports?

If the ReadFile buffer is full, on receiving a new report, the driver drops the oldest report. To prevent losing reports, use HidD_SetNumInputBuffers to increase the buffer size, use longer reports sent less frequently, have the application read reports more often, or increase the size of the buffer in the call to ReadFile. (ReadFile returns as many reports as are available, up to ReadFile's buffer size.)



---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Default is 32, max is 512 for WinXP

[DllImport("hid.dll", SetLastError = true)]
protected static extern Boolean HidD_GetNumInputBuffers(IntPtr HidDeviceObject, ref Int32 NumberBuffers);

[DllImport("hid.dll", SetLastError = true)]
protected static extern Boolean HidD_SetNumInputBuffers(IntPtr HidDeviceObject, Int32 NumberBuffers);
GeneralI don't understand how to use the example project Pin
12313s4-May-10 8:05
12313s4-May-10 8:05 
Questionthere are a new version of it? Pin
hilbi8830-Apr-10 23:22
hilbi8830-Apr-10 23:22 
GeneralHappy but Perplexed Pin
gregmemphis20-Feb-10 21:15
gregmemphis20-Feb-10 21:15 
GeneralRe: Happy but Perplexed Pin
Hosenite19-Mar-10 9:53
Hosenite19-Mar-10 9:53 
GeneralRe: Happy but Perplexed [modified] Pin
aklein5315-Feb-11 11:48
aklein5315-Feb-11 11:48 
QuestionCapability Questions Pin
jschaenzle6-Feb-10 6:47
jschaenzle6-Feb-10 6:47 
GeneralMy vote of 1 Pin
Render_V1-Feb-10 19:42
Render_V1-Feb-10 19:42 
GeneralFailed to create handle when opening form again Pin
ric_vas11-Jan-10 10:57
ric_vas11-Jan-10 10:57 
GeneralRe: Failed to create handle when opening form again - update Pin
ric_vas12-Jan-10 6:50
ric_vas12-Jan-10 6:50 
GeneralWinEr: 00000005 Pin
cabron1238-Jan-10 5:36
cabron1238-Jan-10 5:36 
GeneralRe: WinEr: 00000005 Pin
Hosenite14-Jan-10 9:46
Hosenite14-Jan-10 9:46 
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 

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.