Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

571 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