C#, detect USB device being added/removed

Posted on 2005-03-09
Medium Priority
Last Modified: 2008-03-17
I have a USB device (a Capture Board for an Xray sensor) that I want to be able to detect when it gets plugged or unplugged from the computer. The problem is it doesn't seem to be handled like other USB devices are (or I'm just doing something wrong, which is very possible).

If I insert/remove a USB memory stick into one of the USB ports, I receive the DeviceEvent.Arrival message. However, when I insert/remove the Capture Board from a USB port, I never get a DeviceEvent.Arrival message. I catch a few WM_DEVICECHANGE messages which, when it runs through the code I have below, gets printed out as having a value of "7". Not really sure what to do with that.

Below is the code I have so far. Any ideas on why this USB device wouldn't send a DeviceEvent.Arrival message? Could it just be something specific to this device?

The device software is already installed on the comptuer, and the device has been tested and works fine. The application communicates with it and does what it is intended to do. The problem so far is just detecting when it gets inserted/removed.

The value of 7 refers to the event DBT_DEVNODES_CHANGED. This is the only event I see when I insert/remove the device. I get it twice when I insert, once when I remove.

public enum DeviceEvent:int
      Arrival = 0x8000,            //DBT_DEVICEARRIVAL
      QueryRemove = 0x8001,      //DBT_DEVICEQUERYREMOVE
      QueryRemoveFailed = 0x8002,      //DBT_DEVICEQUERYREMOVEFAILED
      RemovePending = 0x8003,      //DBT_DEVICEREMOVEPENDING
      RemoveComplete = 0x8004,      //DBT_DEVICEREMOVECOMPLETE
      Specific = 0x8005,            //DBT_DEVICEREMOVECOMPLETE
      Custom = 0x8006            //DBT_CUSTOMEVENT

protected override void WndProc(ref Message m)
      base.WndProc(ref m);
      DeviceEvent     lEvent;

            lEvent = (DeviceEvent)m.WParam.ToInt32();
            MessageBox.Show("Device changed! Event: " + lEvent.ToString());

            if (lEvent==DeviceEvent.Arrival || lEvent==DeviceEvent.RemoveComplete)
                  // Do stuff


Question by:SEinarsson
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 96

Accepted Solution

Bob Learned earned 2000 total points
ID: 13500668
using System.Management;

string queryString =
FROM __InstanceOperationEvent
TargetInstance ISA 'Win32_USBHub'";

ManagementEventWatcher usbWatcher = new ManagementEventWatcher(queryString);
usbWatcher.EventArrived += new EventArrivedEventHandler(OnCreation);

      private void OnCreation(object sender, EventArrivedEventArgs e)


Author Comment

ID: 13534556
Thanks, that worked nicely.


Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question