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

 
GeneralRe: receive data doesn't fire Pin
Ayman Alterawi28-May-11 2:45
Ayman Alterawi28-May-11 2:45 
GeneralRe: receive data doesn't fire Pin
RichRobinson20-Sep-11 7:32
RichRobinson20-Sep-11 7:32 
GeneralYou Rock My Face Pin
blackey024-Aug-09 15:55
blackey024-Aug-09 15:55 
GeneralAwesome! Thanks :) Pin
omie654125-Jul-09 7:02
omie654125-Jul-09 7:02 
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 
I just figured it out. I added the source from this link to determine the # of bits for the OS:
http://www.csharp411.com/determine-windows-version-and-edition-with-c/[^]

I changed the DeviceInterfaceData Reserved member datatype from an int to an Intptr:
[StructLayout(LayoutKind.Sequential, Pack = 1)]<br />
protected struct DeviceInterfaceData<br />
{<br />
    public int Size;<br />
    public Guid InterfaceClassGuid;<br />
    public int Flags;<br />
    public IntPtr Reserved;<br />
}


Then I added the following code to HIDDevice.GetDevicePath:

DeviceInterfaceDetailData oDetail = new DeviceInterfaceDetailData();<br />
if (OSInfo.Bits == 64)<br />
{<br />
    oDetail.Size = 8; // 64 bit OSes need this<br />
}<br />
else<br />
{<br />
    oDetail.Size = 5;	// hardcoded to 5! Sorry, but this works and trying more future proof versions by setting the size to the struct sizeof failed miserably. If you manage to sort it, mail me! Thx<br />
}

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 
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 

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.