[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

WMI - Win32_DeviceChangeEvent - Help

Posted on 2006-03-23
1
Medium Priority
?
1,043 Views
Last Modified: 2008-01-09
Hi

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)

        w.Start()

    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

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_devicechangeevent.asp
End Code---------------------------------

According to this site: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_devicechangeevent.asp, 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 messagebox1.show("New Device Detected")
When the value is 3 (Device Removal) do messagebox1.show("Device Removed")

Anyone know how to do this?

Thanks
0
Comment
Question by:narmi2
1 Comment
 
LVL 41

Accepted Solution

by:
graye earned 2000 total points
ID: 16271827
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)
        m_MediaConnectWatcher.Start()
    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
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month18 days, 1 hour left to enroll

831 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