Click here to Skip to main content
13,005,192 members (59,947 online)
Click here to Skip to main content
Add your own
alternative version


218 bookmarked
Posted 22 Mar 2007

A USB HID Component for C#

, 22 Mar 2007
Rate this:
Please Sign up or sign in to vote.
A component to communicate with a USB HID device


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.


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.

protected override void OnHandleCreated(EventArgs e)
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.


  • 22nd March, 2007: First version, currently in development

Updates will be posted if there is a need for them.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

AnswerRe: UsbLibrary in WPF application Pin
candude11-Apr-14 5:14
membercandude11-Apr-14 5:14 
QuestionLocking the USB Barcode Scanner Input Pin
Pasan Indeewara2-Jun-13 19:29
memberPasan Indeewara2-Jun-13 19:29 
QuestionUsbLibrary.UsbHidPort Pin
kainraz27-May-13 7:57
memberkainraz27-May-13 7:57 
GeneralMy vote of 5 Pin
Wolf_124-Apr-13 19:50
memberWolf_124-Apr-13 19:50 
Questionuse your application can't send data to HID device,how to solve? Pin
xuyunhai16-Apr-13 21:58
memberxuyunhai16-Apr-13 21:58 
AnswerRe: use your application can't send data to HID device,how to solve? Pin
Member 1070322423-Sep-14 23:16
memberMember 1070322423-Sep-14 23:16 
QuestionDoes anybody know how to disconnect from the SpecifiedDevice? Pin
MozgC20-Jan-13 3:39
memberMozgC20-Jan-13 3:39 
AnswerRe: Does anybody know how to disconnect from the SpecifiedDevice? Pin
showwei200410-Apr-13 18:45
membershowwei200410-Apr-13 18:45 
your program maybe you reference usb more time.
you can try modify following:
1.HIDDevice.cs ,function "Initialise"
m_hHandle = CreateFile(strPath,
                                GENERIC_READ | GENERIC_WRITE,
                                FILE_SHARE_WRITE | FILE_SHARE_READ, // changed from 0

protected void ReadCompleted(IAsyncResult iResult)
                byte[] arrBuff = (byte[])iResult.AsyncState;    // retrieve the read buffer
                    m_oFile.EndRead(iResult);   // call end read : this throws any exceptions that happened during the read
                        InputReport oInRep = CreateInputReport();   // Create the input report for the device
                        oInRep.SetData(arrBuff);    // and set the data portion - this processes the data received into a more easily understood format depending upon the report type
                        HandleDataReceived(oInRep); // pass the new input report on to the higher level handler
                        BeginAsyncRead();   // when all that is done, kick off another read for the next report
                catch (IOException ex)  // if we got an IO exception, the device was removed
                    if (OnDeviceRemoved != null)
                        OnDeviceRemoved(this, new EventArgs());

at the latest,I try it is success.
who can teach me english? i like english.

Questionproblem with reference in usb hid component for c hsrp . what is the possible solution of this kind of warning. plz help Pin
sefat_23571121-Dec-12 9:52
membersefat_23571121-Dec-12 9:52 
QuestionDataRecievedEventHandler Pin
Member 967835411-Dec-12 22:33
memberMember 967835411-Dec-12 22:33 
Questionmore than a device with same vid&pid? Pin
minkiaprof2-Nov-12 7:00
memberminkiaprof2-Nov-12 7:00 
QuestionC# - How to get the FriendlyName from USB Serial Port? Pin
silvio pontes29-Aug-12 18:24
membersilvio pontes29-Aug-12 18:24 
QuestionUse the component twice Pin
beattiet999-Aug-12 17:57
memberbeattiet999-Aug-12 17:57 
AnswerRe: Use the component twice Pin
beattiet999-Aug-12 19:02
memberbeattiet999-Aug-12 19:02 
QuestionI can not receive the responce from the device Pin
Member 43415761-Jun-12 11:02
memberMember 43415761-Jun-12 11:02 
GeneralRe: I can not receive the responce from the device Pin
Vladimir N.20-Jul-12 0:34
memberVladimir N.20-Jul-12 0:34 
GeneralRe: I can not receive the responce from the device Pin
arpizt14-Aug-12 16:24
memberarpizt14-Aug-12 16:24 
GeneralRe: I can not receive the responce from the device Pin
Vladimir N.14-Aug-12 16:54
memberVladimir N.14-Aug-12 16:54 
QuestionWhy is myPSP_DEVICE_INTERFACE_DETAIL_DATA.cbSize set to 5 Pin
__John_16-May-12 4:03
member__John_16-May-12 4:03 
Questionusing HID to invoke SendInput key press events Pin
murmand30-Apr-12 3:48
membermurmand30-Apr-12 3:48 
AnswerRe: using HID to invoke SendInput key press events Pin
Vladimir N.8-Jul-12 22:08
memberVladimir N.8-Jul-12 22:08 
GeneralMy vote of 4 Pin
amccombs14-Apr-12 2:57
memberamccombs14-Apr-12 2:57 
GeneralMy vote of 1 Pin
Clifford Nelson11-Apr-12 10:24
memberClifford Nelson11-Apr-12 10:24 
GeneralRe: My vote of 1 Pin
Clifford Nelson14-Apr-12 11:41
memberClifford Nelson14-Apr-12 11:41 
BugWindows 7. Failed to create device file WinEr:00000005 Pin
Member 253745610-Apr-12 20:44
memberMember 253745610-Apr-12 20:44 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170627.1 | Last Updated 22 Mar 2007
Article Copyright 2007 by wimar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid