Click here to Skip to main content
15,910,981 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   241   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: Problems with 64-bit Windows Pin
RenniePet28-Apr-08 9:32
RenniePet28-Apr-08 9:32 
QuestionRe: Problems with 64-bit Windows Pin
mellinge14-Feb-09 3:53
mellinge14-Feb-09 3:53 
AnswerRe: Does not work on 64 bit Vista? Pin
sophtware25-Jul-08 17:10
sophtware25-Jul-08 17:10 
GeneralRe: Does not work on 64 bit Vista? [modified] Pin
George Helyar26-Jul-08 0:32
George Helyar26-Jul-08 0:32 
GeneralRe: Does not work on 64 bit Vista? Pin
sophtware26-Jul-08 8:45
sophtware26-Jul-08 8:45 
GeneralRe: Does not work on 64 bit Vista? Pin
George Helyar26-Jul-08 10:04
George Helyar26-Jul-08 10:04 
GeneralRe: Does not work on 64 bit Vista? Pin
sophtware26-Jul-08 14:26
sophtware26-Jul-08 14:26 
GeneralRe: Does not work on 64 bit Vista? [modified] Pin
George Helyar26-Jul-08 22:23
George Helyar26-Jul-08 22:23 
Yes, I didn't really believe that the 64 bit version would be any more Unicode than the 32 bit version but many places suggest it without having tried it. On both 32 and 64bit, and on both Marshal.SystemDefaultCharSize = 1 and Marshal.SystemDefaultCharSize = 2, the SetupDiGetDeviceInterfaceDetail[^] function seems to always use non-Unicode ( _A ?) anyway.

I can see how it would be 5 on 32bit and 8 on 64bit, assuming that both use 8 bit characters but 64bit rounds the 5 up to 8, but the only way it is done automatically is if you manually set the packing on the struct. Pack=1 gives 5, Pack=8 gives 8. However, I can't see how to get 32bit to pack with 1 and 64bit to pack with 8 at runtime and anything to find this value at compile time will not work (may as well compile one version with the constant value 5 and one with the constant value 8 and distribute them separately, which is not what .net is about). It's really something to do with what SetupApi is using. Pack=0 (default platform packing) seems to be 8 for both 32 and 64bit regardless of what SetupApi is using.

Unless there is some way of pulling the packing size that SetupApi is using on the machine at runtime, so that it can be used to round up, there is no way of doing this better than the small conditional that I gave in my previous message. Even with 4+IntPtr.Size/2 (which I don't think would work anyway, this would be 6 on 32bit and 8 on 64bit but on every 32 bit machine I have tried (XP, 2003 and Vista), 5 is required and on every 64 bit machine I have tried (XP, 2003 and Vista), 8 is required), you still need to know whether it should be rounding up to 8, for example.

I think ideally it would just be something like "Round 5 (or maybe 6) up to GetSetupApiPackSizeFromDll()" to work on all current and future systems at runtime.

modified on Sunday, July 27, 2008 4:31 AM

RantRe: Does not work on 64 bit Vista? Pin
sophtware27-Jul-08 2:52
sophtware27-Jul-08 2:52 
GeneralRe: Does not work on 64 bit Vista? Pin
NiteShdw11-Aug-08 15:02
NiteShdw11-Aug-08 15:02 
GeneralRe: Does not work on 64 bit Vista? Pin
George Helyar11-Aug-08 16:18
George Helyar11-Aug-08 16:18 
AnswerRe: Does not work on 64 bit Vista? Pin
therearefartoomanybens29-Sep-08 3:19
therearefartoomanybens29-Sep-08 3:19 
GeneralRe: Does not work on 64 bit Vista? Pin
George Helyar29-Sep-08 5:56
George Helyar29-Sep-08 5:56 
GeneralRe: Does not work on 64 bit Vista? Pin
therearefartoomanybens29-Sep-08 10:45
therearefartoomanybens29-Sep-08 10:45 
GeneralCan't find my usb flash drive Pin
eythimis15-Oct-07 5:09
eythimis15-Oct-07 5:09 
GeneralRe: Can't find my usb flash drive Pin
wimar22-Oct-07 7:18
wimar22-Oct-07 7:18 
GeneralRe: Can't find my usb flash drive Pin
Doug Squires24-Oct-07 6:07
Doug Squires24-Oct-07 6:07 
GeneralFindSpecifiedDevice Problem Pin
Iwan Budihalim26-Aug-07 23:55
Iwan Budihalim26-Aug-07 23:55 
GeneralRe: FindSpecifiedDevice Problem Pin
Iwan Budihalim27-Aug-07 1:53
Iwan Budihalim27-Aug-07 1:53 
GeneralRe: FindSpecifiedDevice Problem Pin
taithien22-Jan-08 20:44
taithien22-Jan-08 20:44 
GeneralRe: FindSpecifiedDevice Problem Pin
sbeaubien18-Mar-08 7:41
sbeaubien18-Mar-08 7:41 
GeneralRe: FindSpecifiedDevice Problem Pin
Z789512316-Jul-09 16:04
Z789512316-Jul-09 16:04 
GeneralRe: FindSpecifiedDevice Problem Pin
tolgakayahan23-Aug-09 0:36
tolgakayahan23-Aug-09 0:36 
GeneralRe: FindSpecifiedDevice Problem Pin
wjjwlhk28-Apr-10 0:08
wjjwlhk28-Apr-10 0:08 
GeneralRe: FindSpecifiedDevice Problem Pin
SPI9-Jan-12 6:45
SPI9-Jan-12 6:45 

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.