A USB Library To Detect USB Devices - CodeProject
A USB Library To Detect USB Devices - CodeProject
AUSBLibrarytoDetectUSBDevicesCodeProject
articles
Q&A
forums
Sign in
lounge
Searchforarticles,questions,tips
CPOL
Rate this:
4.89 90 votes
A USB library to detect USB devices, and manage Attach and Detach events
http://www.codeproject.com/Articles/60579/AUSBLibrarytoDetectUSBDevices
1/10
3/23/2016
AUSBLibrarytoDetectUSBDevicesCodeProject
Introduction
This article is about a USB library which enables you to manage Attach and Detach events of USB devices and detect your own
device. I was not able to find a working code written in C# and which runs under both Windows XP and Windows 7 x64. I
therefore decided to write my own code. I read various articles about USB Attach and Detach detection, and got some help from
both the Microsoft website and the PINVOKE.NET website http://www.pinvoke.net.
This code is a separate module that you can link to your own project. The code explains how to add additional properties.
usingUSBClassLibrary;
Declare an instance of USBClass.
Hide Copy Code
privateUSBClassLibrary.USBClassUSBPort;
http://www.codeproject.com/Articles/60579/AUSBLibrarytoDetectUSBDevices
2/10
3/23/2016
AUSBLibrarytoDetectUSBDevicesCodeProject
Declare an instance List<T> of DeviceProperties if you want to read the properties of your devices.
Hide Copy Code
privateList<USBClassLibrary.USBClass.DeviceProperties>ListOfUSBDeviceProperties;
Create an instance of the USBClass class.
Hide Copy Code
USBPort=newUSBClass();
Create an instance List<T> of DeviceProperties class.
Hide Copy Code
ListOfUSBDeviceProperties=newList<USBClassLibrary.USBClass.DeviceProperties>();
Add handlers for the events exposed by the USBClass class.
Hide Copy Code
USBPort.USBDeviceAttached+=newUSBClass.USBDeviceEventHandler(USBPort_USBDeviceAttached);
USBPort.USBDeviceRemoved+=newUSBClass.USBDeviceEventHandler(USBPort_USBDeviceRemoved);
Register your form to receive Windows messages when devices are added or removed.
Hide Copy Code
USBPort.RegisterForDeviceChange(true,this.Handle);
Then, check if your device is not already connected:
Hide Copy Code
if(USBClass.GetUSBDevice(MyDeviceVID,MyDevicePID,
refListOfUSBDeviceProperties,false))
{
//MyDeviceisconnected
MyUSBDeviceConnected=true;
}
Implement Attach and Detach handlers:
Hide Copy Code
privatevoidUSBPort_USBDeviceAttached(objectsender,
USBClass.USBDeviceEventArgse)
{
if(!MyUSBDeviceConnected)
{
if(USBClass.GetUSBDevice(MyDeviceVID,MyDevicePID,
refListOfUSBDeviceProperties,false))
{
//MyDeviceisconnected
MyUSBDeviceConnected=true;
}
}
}
privatevoidUSBPort_USBDeviceRemoved(objectsender,
USBClass.USBDeviceEventArgse)
{
if(!USBClass.GetUSBDevice(MyDeviceVID,MyDevicePID,
refListOfUSBDeviceProperties,false))
{
//MyDeviceisremoved
MyUSBDeviceConnected=false;
}
}
Handle Windows message in your form to pass them to the USBClass class:
http://www.codeproject.com/Articles/60579/AUSBLibrarytoDetectUSBDevices
3/10
3/23/2016
AUSBLibrarytoDetectUSBDevicesCodeProject
protectedoverridevoidWndProc(refMessagem)
{
boolIsHandled=false;
USBPort.ProcessWindowsMessage(m.Msg,m.WParam,m.LParam,refIsHandled);
base.WndProc(refm);
}
WPF
Add a reference to your project.
Add theusingdirective in your code:
Hide Copy Code
usingUSBClassLibrary;
Declare an instance ofUSBClass.
Hide Copy Code
privateUSBClassLibrary.USBClassUSBPort;
Declare an instance List<T> of theDevicePropertiesclass if you want to read the properties of your devices.
Hide Copy Code
privateList<USBClassLibrary.USBClass.DeviceProperties>ListOfUSBDeviceProperties;
Create an instance of theUSBClassclass.
Hide Copy Code
USBPort=newUSBClass();
Create an instance List<T> of theDevicePropertiesclass.
Hide Copy Code
ListOfUSBDeviceProperties=newList<USBClassLibrary.USBClass.DeviceProperties>();
Add handlers for the events exposed by theUSBClassclass.
Hide Copy Code
USBPort.USBDeviceAttached+=newUSBClass.USBDeviceEventHandler(USBPort_USBDeviceAttached);
USBPort.USBDeviceRemoved+=newUSBClass.USBDeviceEventHandler(USBPort_USBDeviceRemoved);
Override OnSourceInitializedin order to:
Retrieve the Windows Handle
Register your form to receive Windows messages when devices are added or removed
Hide Copy Code
protectedoverridevoidOnSourceInitialized(EventArgse)
{
base.OnSourceInitialized(e);
HwndSourcesource=PresentationSource.FromVisual(this)asHwndSource;
source.AddHook(WndProc);
//USBConnection
USBPort.RegisterForDeviceChange(true,source.Handle);
USBTryMyDeviceConnection();
MyUSBDeviceConnected=false;
}
http://www.codeproject.com/Articles/60579/AUSBLibrarytoDetectUSBDevices
4/10
3/23/2016
AUSBLibrarytoDetectUSBDevicesCodeProject
privateIntPtrWndProc(IntPtrhwnd,intmsg,IntPtrwParam,IntPtrlParam,refboolhandled)
{
USBPort.ProcessWindowsMessage(msg,wParam,lParam,refhandled);
returnIntPtr.Zero;
}
Then, check if your device is not already connected:
Hide Copy Code
if(USBClass.GetUSBDevice(MyDeviceVID,MyDevicePID,
refListOfUSBDeviceProperties,false))
{
//MyDeviceisconnected
MyUSBDeviceConnected=true;
}
Implement Attach and Detach handlers:
Hide Copy Code
privatevoidUSBPort_USBDeviceAttached(objectsender,
USBClass.USBDeviceEventArgse)
{
if(!MyUSBDeviceConnected)
{
if(USBClass.GetUSBDevice(MyDeviceVID,MyDevicePID,
refListOfUSBDeviceProperties,false))
{
//MyDeviceisconnected
MyUSBDeviceConnected=true;
}
}
}
privatevoidUSBPort_USBDeviceRemoved(objectsender,
USBClass.USBDeviceEventArgse)
{
if(!USBClass.GetUSBDevice(MyDeviceVID,MyDevicePID,
refListOfUSBDeviceProperties,false))
{
//MyDeviceisremoved
MyUSBDeviceConnected=false;
}
}
publicstaticboolGetUSBDevice(UInt32VID,UInt32PID,refList<deviceproperties>ListOfDP,bool
GetCOMPort,Nullable<uint32>MI=null)
Set its value to Truein your connection code and retrieve the COM Port from the DeviceProperties structure:
Hide Copy Code
if(USBClass.GetUSBDevice(MyDeviceVID,MyDevicePID,
refListOfUSBDeviceProperties,true))
http://www.codeproject.com/Articles/60579/AUSBLibrarytoDetectUSBDevices
5/10
3/23/2016
AUSBLibrarytoDetectUSBDevicesCodeProject
{
StringCOMPort;
//MyDeviceisconnected
MyUSBDeviceConnected=true;
COMPort=DP.COMPort;
}
Compiling
In the project properties, Build tab, do not select Any CPU in the "Platform target" drop down list, pick up x86 or x64.
publicstructDeviceProperties
{
publicstringFriendlyName;
publicstringDeviceDescription;
publicstringDeviceType;
publicstringDeviceManufacturer;
publicstringDeviceClass;
publicstringDeviceLocation;
publicstringDevicePath;
publicstringDevicePhysicalObjectName;
publicstringCOMPort;
}
Update the GetUSBDevice function:
Hide Copy Code
publicstaticboolGetUSBDevice(UInt32VID,UInt32PID,refList<deviceproperties>ListOfDP,
boolGetCOMPort,Nullable<uint32>MI=null)
{
...
DP.DevicePhysicalObjectName=String.Empty;
if(Win32Wrapper.SetupDiGetDeviceRegistryProperty(h,refDevInfoData,
(UInt32)Win32Wrapper.SPDRP.SPDRP_PHYSICAL_DEVICE_OBJECT_NAME,
refRegType,intptrBuffer,BUFFER_SIZE,refRequiredSize))
{
DP.DevicePhysicalObjectName=Marshal.PtrToStringAuto(intptrBuffer);
}
...
}
History
Feb 22, 2010
Article first published
Feb 26, 2010
Added code to retrieve the COM Port associated to USB Devices emulating a serial port
http://www.codeproject.com/Articles/60579/AUSBLibrarytoDetectUSBDevices
6/10
3/23/2016
AUSBLibrarytoDetectUSBDevicesCodeProject
License
This article, along with any associated source code and files, is licensed under The Code Project Open License CPOL
Share
EMAIL
3/23/2016
AUSBLibrarytoDetectUSBDevicesCodeProject
using C#
Go
First Prev Next
15Dec15 9:46
Re: Disconnected!
pip010 20Jan16 0:44
USBClassLibrary Device ID or Serial Number resubmitted
Member 11983109 21Sep15 3:39
Error Compiling
Member 11972365
9Sep15 14:36
http://www.codeproject.com/Articles/60579/AUSBLibrarytoDetectUSBDevices
8/10
3/23/2016
AUSBLibrarytoDetectUSBDevicesCodeProject
14Nov14 5:31
Re: NuGet
slelong 16Nov14 9:45
Re: NuGet
Robin Jones
16Nov14 11:04
1 2 3 4 5 6 7 8 Next
9/10
3/23/2016
General
AUSBLibrarytoDetectUSBDevicesCodeProject
News
Suggestion
Question
Bug
Answer
Joke
Praise
Rant
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
Web02 | 2.8.160311.1 | Last Updated 21 Sep 2014
Select Language
Layout: fixed | fluid
http://www.codeproject.com/Articles/60579/AUSBLibrarytoDetectUSBDevices
10/10