Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4062248
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:34:29+00:00 2026-05-20T15:34:29+00:00

I have been writing some code that detects add and removal of USB devices,

  • 0

I have been writing some code that detects add and removal of USB devices, and I’ve used the following WMI code to register for device change notifications:

watcher = new ManagementEventWatcher(query);
watcher.EventArrived += new EventArrivedEventHandler(DeviceChangeEventReceived);
watcher.Start();

This is the handler code:

void DeviceChangeEventReceived(object sender, EventArrivedEventArgs e)
{
   foreach (PropertyData pd in e.NewEvent.Properties)
   {
      Log.Debug("\t" + pd.Name + ":" + pd.Value + "\t" + pd.Value.GetType());
   }
}

This is great and all, it works for any USB device I plug in or remove from the system. The problem that I’m having is, how do I identify the the device specifically that caused the events?

Elsewhere in my program, I’m keeping a list of currently connected devices that I’m most interested in, so if a device-removed event comes through, I can check that list against WMI using “select * from Win32_PnPEntity” or some other similar query. BUT, this is a very inaccurate and cumbersome way of identifying the device that was removed. The added problem is, I have no way of accurately telling what device was added, unless I cache the entire list of Win32_PnPEntity ahead of time, and do really crazy comparisons/validations.

Am I missing something obvious here? How do I associate the device change events to a specific device?

UPDATE: I still haven’t come up with an ideal solution to this problem, but what I am doing is maintaining a list of currently connected devices (that I’m interested in) in memory, and every time an event is handled (see above), I query the Win32_PnPEntity to see if the devices I have stored in my connected device list are still connected. This is a sub-optimal solution, because it just seems weird that I can’t get any specific device identification information from the event that indicates “device change event”. Seems VERY strange, that this info is unavailable. sigh

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T15:34:30+00:00Added an answer on May 20, 2026 at 3:34 pm

    Okay, so after some further investigation and experimenting, I’ve discovered that I need to use a different WMI query in order to solve my problem, which is to associate a device change event with a specific device. In this case, I need to find what seems to be conventionally referred to in WMI as “TargetInstance”.

    So, I used the following WMI query code instead

                ManagementEventWatcher watcher;
                string queryStr =
                    "SELECT * FROM __InstanceCreationEvent " +
                    "WITHIN 2 "
                  + "WHERE TargetInstance ISA 'Win32_PnPEntity'"
    
                watcher = new ManagementEventWatcher(queryStr);
                watcher.EventArrived += new EventArrivedEventHandler(DeviceChangeEventReceived);
                watcher.Start();
    

    So the difference here is, that the __InstanceCreationEvent has a property called “TargetInstance”, which is EXACTLY what I was looking for. I cast the TargetInstance property to a ManagementBaseObject (which is of type “Win32_PnPEntity” (per the ISA clause in the query above), and Voila! I get the specific device that was created.

    It still sort of baffles me as to how my original query “Select * from Win32_DeviceChangeEvent” would be useful to anyone at all, since there’s no additional information provided after a generic event notification is fired. Either way, this new query a significantly cleaner solution to the my problem. WMI seems pretty powerful, but finding the correct query to use can be tricky, and requires some experimenting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been writing some code that creates a generic blog. Its features are
I have been writing some C# code for a training exercise, in which I
I have been writing code like the following enough a lot lately. I don't
I've been writing WinForms code that uses a BackgroundWorker to do some async work,
I've been writing PHP web applications for some time, and have come across very
I have been thinking of maybe writing a little tool that logs into SO
I have just been getting into low level programming (reading/writing to memory that sort
Lately I've been writing some JS code using jQuery and JavaScript as it is
I have been writing C code for many years, but I recently came accross
I have been assigned a project with a lot of poorly written code that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.