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 7605139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:05:26+00:00 2026-05-31T00:05:26+00:00

I want to get a notification when a usb device is inserted/removed/enabled/disabled. I am

  • 0

I want to get a notification when a usb device is inserted/removed/enabled/disabled.
I am registering to “__InstanceOperationEvent” for Win32_USBControllerDevice.

The problem is that although I get the insert/removed notifications for all the devices.
I get the enabled/disabled events only for USB storage devices.

What am I doing wrong?

Thanks in advance

  • 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-31T00:05:27+00:00Added an answer on May 31, 2026 at 12:05 am

    It’s a little hard to grasp what you are talking about without providing a code sample or telling us what language you are working in.

    The Win32_USBController class is an associator of the Win32_PnPEntity class which does have an instance of all USB devices regardless of type. So an __InstanceOperationEvent for this class DOES provide notifications for all devices, not just USB storage devices. More appropriately, it will work for any PnP-compatible USB device.

    The following script is a point of concept written in VBScript. It will notify you of all USB components when a device is connected or disconnected. I tested with USB storage devices, USB mouse and keyboard, and various other devices I had lying around. All of them worked. I tested on Vista x64 but this should work with any version of Windows.

    vbscript Sample:

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    
    Set colEvents = objWMIService.ExecNotificationQuery _
        ("Select * From __InstanceOperationEvent Within 1 " _
            & "Where TargetInstance Isa 'Win32_USBControllerDevice'")
    
    While True
        Set objEvent = colEvents.NextEvent
    
        Select Case objEvent.Path_.Class
        Case "__InstanceCreationEvent"
            strOutput = "Device inserted:" & vbCr
        Case "__InstanceDeletionEvent"
            strOutput = "Device removed:" & vbCr
        Case Default
            strOutput = objEvent.Path_.Class & vbCr
        End Select
    
        strDeviceName = objEvent.TargetInstance.Dependent
        strDeviceName = Replace(strDeviceName, Chr(34), "")
        arrDevicePart = Split(strDeviceName, "=")
        strDeviceName = arrDevicePart(1)
    
        Set colUSBDevices = objWMIService.ExecQuery _
            ("Select * From Win32_PnPEntity Where DeviceID = '" & strDeviceName & "'")
        For Each objUSBDevice in colUSBDevices
            strOutput = strOutput & objUSBDevice.Description & vbCr
        Next
    
        WScript.Echo strOutput
    Wend
    

    Determining when a devices is enabled or disabled is similar but has a few distinct differences. You’ll want to use the Win32_PnPEntity class which is basically an enumeration of all installed PnP devices. You’ll want be focusing on the ConfigManagerErrorCode property for each instance. This provides contains the same information that you will find in the Device status portion of Device Manager. You can see why it would be useful to watch this propery for any given device. When a device changes to show the code (0) for normal operation, we can safely assume that a device has been enabled. When this code changes to code 22, we know that a devices has been disabled. (All other codes indicate error states.) The __InstanceModificationEvent is a good choice to watch for changes in any instances of the Win32_PnPEntity class.

    If you’re interested in the possible error codes you can throw in a WScript.Echo statement near the beginning of the script where it enumerates all of the possible values.

    vbscript Sample:

    Const wbemFlagUseAmendedQualifiers = &H20000
    
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    
    Set objPnPEntityClass = objWMIService.Get("Win32_PnPEntity", wbemFlagUseAmendedQualifiers)
    arrValues = objPnPEntityClass.properties_("ConfigManagerErrorCode").qualifiers_("values")
    valueMap = objPnPEntityClass.properties_("ConfigManagerErrorCode").qualifiers_("valuemap")
    
    WScript.Echo "Gathering Device Manager error codes..."
    Set dictErrorCodes = CreateObject("Scripting.Dictionary")
    For i = 0 to Ubound(arrValues)
        dictErrorCodes.add valueMap(i), arrValues(i)
        '  0, This device is working properly.
        ' 22, This device is disabled.
    Next
    
    Set objPnPEntityClass = Nothing
    
    WScript.Echo "Setting up event notification..."
    Set colEvents = objWMIService.ExecNotificationQuery _
        ("Select * From __InstanceModificationEvent Within 1 " _
            & "Where TargetInstance Isa 'Win32_PnPEntity'")
    
    WScript.Echo "Waiting..."
    While True
        Set objEvent = colEvents.NextEvent
        Set objDevice = objEvent.TargetInstance
    
        Select Case objDevice.ConfigManagerErrorCode
        Case 0      ' This device is working properly.
            strOutput = objDevice.Caption & " is working properly." & vbCr
        Case 22     ' This device is disabled.
            strOutput = objDevice.Caption & " has been disabled." & vbCr
        Case Default
            strDeviceStatus = arrValues(objDevice.ConfigManagerErrorCode)
            strOutput = "(" & objDevice.Caption & ") " &  strDeviceStatus & vbCr
        End Select
    
        WScript.Echo strOutput
    Wend
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get notification for a new record inserted into the database with
I want get all of the Geom objects that are related to a certain
I want to get an overview of files that are updated in TFS (that
Is it possible to get a notification (that launches the app) when the network
I am developing a iPhone application in which i want to get notification in
I want to get a notification when object attribute is changed by using reflection.
I want to be able to detect and get notification, when the user is
I have a notification system that works with the following codes jQuery: $(document).ready(function(){$.get('/codes/php/nf.php', function(data)
I am working on iphone push notification. I can able to get my device
So, the question is: I get some notifications I don't want to get. But

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.