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

 
QuestionUse the component twice Pin
beattiet999-Aug-12 17:57
beattiet999-Aug-12 17:57 
AnswerRe: Use the component twice Pin
beattiet999-Aug-12 19:02
beattiet999-Aug-12 19:02 
QuestionI can not receive the responce from the device Pin
innaw1-Jun-12 11:02
innaw1-Jun-12 11:02 
GeneralRe: I can not receive the responce from the device Pin
Vladimir N.20-Jul-12 0:34
Vladimir N.20-Jul-12 0:34 
GeneralRe: I can not receive the responce from the device Pin
arpizt14-Aug-12 16:24
arpizt14-Aug-12 16:24 
GeneralRe: I can not receive the responce from the device Pin
Vladimir N.14-Aug-12 16:54
Vladimir N.14-Aug-12 16:54 
QuestionWhy is myPSP_DEVICE_INTERFACE_DETAIL_DATA.cbSize set to 5 Pin
__John_16-May-12 4:03
__John_16-May-12 4:03 
Questionusing HID to invoke SendInput key press events Pin
murmand30-Apr-12 3:48
murmand30-Apr-12 3:48 
I don't fully understand the idea of HID devices, but here is my question (sorry if it's nonsense).

The only way to make key presses indistinguishable from the real key press - is by using custom (self-made) keyboard drivers.

Lets say we are using SendInput() method (scan codes) to inject key presses into programs/games. In some games - tools such as GameGuard will be blocking our injections. How so? That's because GameGuard blocks every input type except for Drivers themselves.

As far as I understand HID is used to communicate with the USB device, so if by any chance we could simulate key press event as a hardware press - driver will proceed it as a "real" and gameguard won't notice a thing.

is such thing possible with HID (C#)?

modified 3-May-12 8:44am.

AnswerRe: using HID to invoke SendInput key press events Pin
Vladimir N.8-Jul-12 22:08
Vladimir N.8-Jul-12 22:08 
GeneralMy vote of 4 Pin
amccombs14-Apr-12 2:57
amccombs14-Apr-12 2:57 
GeneralMy vote of 1 Pin
Clifford Nelson11-Apr-12 10:24
Clifford Nelson11-Apr-12 10:24 
GeneralRe: My vote of 1 Pin
Clifford Nelson14-Apr-12 11:41
Clifford Nelson14-Apr-12 11:41 
BugWindows 7. Failed to create device file WinEr:00000005 PinPopular
Member 253745610-Apr-12 20:44
Member 253745610-Apr-12 20:44 
GeneralRe: Windows 7. Failed to create device file WinEr:00000005 Pin
Jason Stern1-May-12 10:51
Jason Stern1-May-12 10:51 
GeneralRe: Windows 7. Failed to create device file WinEr:00000005 Pin
Glen Harvy30-May-12 23:36
Glen Harvy30-May-12 23:36 
GeneralRe: Windows 7. Failed to create device file WinEr:00000005 Pin
DeveloperDotNet14-Aug-12 18:45
DeveloperDotNet14-Aug-12 18:45 
GeneralRe: Windows 7. Failed to create device file WinEr:00000005 Pin
Glen Harvy15-Aug-12 6:55
Glen Harvy15-Aug-12 6:55 
GeneralRe: Windows 7. Failed to create device file WinEr:00000005 Pin
Member 95281496-Jul-17 0:49
Member 95281496-Jul-17 0:49 
Questionusing MDI Form... Pin
juhwan7525-Mar-12 20:34
juhwan7525-Mar-12 20:34 
QuestionChanges for Win7 64 bit Pin
Bernhard Hiller23-Mar-12 1:59
Bernhard Hiller23-Mar-12 1:59 
AnswerRe: Changes for Win7 64 bit Pin
mohsen2536new28-Apr-12 22:44
mohsen2536new28-Apr-12 22:44 
AnswerRe: Changes for Win7 64 bit Pin
lipengutd24-Jul-12 3:03
lipengutd24-Jul-12 3:03 
AnswerRe: Changes for Win7 64 bit Pin
parsa_21_3128-Aug-13 12:59
parsa_21_3128-Aug-13 12:59 
AnswerRe: Changes for Win7 64 bit Pin
Sergejl9-Jan-14 10:04
Sergejl9-Jan-14 10:04 
Questionyou are genius! Pin
juhwan7522-Mar-12 21:05
juhwan7522-Mar-12 21:05 

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.