WMI - Win32_DeviceChangeEvent - Help

Posted on 2006-03-23
Last Modified: 2008-01-09

I am trying to detect when a device is plugged in to removed from my computer

Start Code------------------------------
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        q = New WqlEventQuery("Select * from Win32_DeviceChangeEvent")

        w = New ManagementEventWatcher(q)


    End Sub

    Private Sub w_EventArrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs) Handles w.EventArrived

        MessageBox.Show("New Arrival")

    End Sub
End Code---------------------------------

According to this site:, Device Arrival has a value of 2 and Device Removal has a value of 3!

How do I use this information to display a different message box for value? e.g.

When the value is 2 (Device Arrival) do"New Device Detected")
When the value is 3 (Device Removal) do"Device Removed")

Anyone know how to do this?

Question by:narmi2
    1 Comment
    LVL 41

    Accepted Solution

    Here is a similar example that detects a new USB-based disk drive...  

    Imports System.Management

        Private WithEvents m_MediaConnectWatcher As ManagementEventWatcher

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' __InstanceOperationEvent will trap both Creation and Deletion of class instances
            Dim query2 As String = "SELECT * FROM __InstanceOperationEvent WITHIN 10 WHERE TargetInstance ISA ""Win32_DiskDrive"""
            m_MediaConnectWatcher = New ManagementEventWatcher(query2)
        End Sub

        Private Sub Arrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs) Handles m_MediaConnectWatcher.EventArrived
            Dim mbo, obj As ManagementBaseObject

            ' the first thing we have to do is figure out if this is a creation or deletion event
            mbo = CType(e.NewEvent, ManagementBaseObject)
            ' next we need a copy of the instance that was either created or deleted
            obj = CType(mbo("TargetInstance"), ManagementBaseObject)

            Select Case mbo.ClassPath.ClassName
                Case "__InstanceCreationEvent"
                    If obj("InterfaceType") = "USB" Then
                        MsgBox(obj("Caption") & " has been plugged in")
                    End If
                Case "__InstanceDeletionEvent"
                    If obj("InterfaceType") = "USB" Then
                        MsgBox(obj("Caption") & " has been unplugged")
                    End If
            End Select

        End Sub

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now