Solved

wmi ManagementObject and WM_DEVICECHANGE vb.net

Posted on 2003-12-12
48
3,111 Views
Last Modified: 2007-12-19
I have a program that uses wmi to list my combobox with the cd drive and the volume name.  Iam using the WM_DEVICECHANGE to identfy when the user inserts a new cd.  When this happens I want to reload the combo box using wmi and i get an exception error every time in the system.msanagement.dll.  anyone know why?  If I put a button to load the combobox after a cd chnage it is okay, it just won't reload the box automatically.


this is sample code I used:

This populates my combo box:

public sub LoadComboDrives()

        Combo1.Items.Clear()
        Dim wmi As ManagementClass
        Dim objs As ManagementObjectCollection
        Dim obj As ManagementObject
        Dim mgmtpath As String

        wmi = New ManagementClass("Win32_LogicalDisk")

        objs = wmi.GetInstances()
        For Each obj In objs
            ' only CDROMs
            If Convert.ToInt32(obj("drivetype")) = 5 Then
                lblSize.Text = CLng(Convert.ToDouble(obj("Size")) / (1024.0# * 1024.0#))
                lblDeviceId.Text = obj("DeviceID")
                lblVolumeName.Text = obj("VolumeName")
                lblSerialNumber.Text = obj("VolumeSerialNumber")
                lblFilesystem.Text = obj("FileSystem")
                Combo1.Items.Add(obj("VolumeName") & " (" & obj("DeviceID") & ")")
            End If
        Next

        objs.Dispose()
        wmi.Dispose()
        Combo1.SelectedIndex = 0
end sub

This is capturing the media change:

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)

        Select Case m.Msg
            Case WM_DEVICECHANGE
                Select Case m.WParam.ToInt32
                    Case DBT_DEVICEARRIVAL
                        ' New CD Inserted
                        LoadComboDrives()
                    Case DBT_DEVICEREMOVECOMPLETE
                        ' CD Removed
                       LoadComboDrives()

                End Select
        End Select
    End Sub

0
Comment
Question by:edfrazier
  • 24
  • 24
48 Comments
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
What is the full text of the exception message?
0
 

Author Comment

by:edfrazier
Comment Utility
An unhandled exception of type 'System.ExecutionEngineException' occurred in system.management.dll
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Use Try...Catch...End Try block to capture the full text:

Try

....

Catch ex As System.ExecutionEngineException

   MsgBox(ex.ToString)

End Try
0
 

Author Comment

by:edfrazier
Comment Utility
I added that and it still only shows that message, there is nothing additional
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
I hate when that happens!  What does ex.StackTrace and ex.Source return?
0
 

Author Comment

by:edfrazier
Comment Utility
Everything says the same message.  It doesn't seem like it is ever catching the message, this is what I have:

        wmi = New ManagementClass("Win32_LogicalDisk")
        Try
            objs = wmi.GetInstances()
        Catch ex As System.ExecutionEngineException

            MsgBox("here" & ex.ToString)

        End Try

0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Are you still get an unhandled exception?

      Catch engineex As System.ExecutionEngineException
           MsgBox("here" & engineex.ToString)

      Catch ex As Exception
           MsgBox("here" & ex.ToString)
     
0
 

Author Comment

by:edfrazier
Comment Utility
yes, same message
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Okay, then, what line does this occur at?
0
 

Author Comment

by:edfrazier
Comment Utility
No matter what I do this is the only message, I get:

An unhandled exception of type 'System.ExecutionEngineException' occurred in system.management.dll

T
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Do you know when this error occurs?  What line was being processed?  Can you debug this?  Can you help me out trying to at least get a peek into your problems?
0
 

Author Comment

by:edfrazier
Comment Utility
I created a simple form that causes my problem:

I open the form and I load all my cdrom drives in a combobox (with wmi)  I then I open the cdROM door, which should reload my combobox but it does not, it gives me the error that I described.
It blows up in lookup() at this line:
        "For Each disk In searcher.Get()"

Imports System.Management

Public Class Form2
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents lblSerialNumber As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents lblSize As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents lblDeviceId As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents lblVolumeName As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents lblFilesystem As System.Windows.Forms.Label
    Friend WithEvents ListView2 As System.Windows.Forms.ListView
    Friend WithEvents Combo1 As System.Windows.Forms.ComboBox
    Friend WithEvents Title As System.Windows.Forms.ColumnHeader
    Friend WithEvents length As System.Windows.Forms.ColumnHeader
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
    Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
    Friend WithEvents Timer1 As System.Timers.Timer
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label2 = New System.Windows.Forms.Label
        Me.lblSerialNumber = New System.Windows.Forms.Label
        Me.Label3 = New System.Windows.Forms.Label
        Me.lblSize = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.lblDeviceId = New System.Windows.Forms.Label
        Me.Label5 = New System.Windows.Forms.Label
        Me.lblVolumeName = New System.Windows.Forms.Label
        Me.Label6 = New System.Windows.Forms.Label
        Me.lblFilesystem = New System.Windows.Forms.Label
        Me.ListView2 = New System.Windows.Forms.ListView
        Me.Title = New System.Windows.Forms.ColumnHeader
        Me.length = New System.Windows.Forms.ColumnHeader
        Me.Combo1 = New System.Windows.Forms.ComboBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.ComboBox1 = New System.Windows.Forms.ComboBox
        Me.CheckBox1 = New System.Windows.Forms.CheckBox
        Me.Timer1 = New System.Timers.Timer
        CType(Me.Timer1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(24, 88)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(80, 16)
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "Serial Number:"
        '
        'lblSerialNumber
        '
        Me.lblSerialNumber.Location = New System.Drawing.Point(104, 88)
        Me.lblSerialNumber.Name = "lblSerialNumber"
        Me.lblSerialNumber.Size = New System.Drawing.Size(96, 16)
        Me.lblSerialNumber.TabIndex = 4
        Me.lblSerialNumber.Text = "Label3"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(24, 112)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(72, 16)
        Me.Label3.TabIndex = 5
        Me.Label3.Text = "Size"
        '
        'lblSize
        '
        Me.lblSize.Location = New System.Drawing.Point(104, 112)
        Me.lblSize.Name = "lblSize"
        Me.lblSize.Size = New System.Drawing.Size(80, 16)
        Me.lblSize.TabIndex = 6
        Me.lblSize.Text = "Label4"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(24, 136)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(88, 16)
        Me.Label4.TabIndex = 7
        Me.Label4.Text = "Device Id"
        '
        'lblDeviceId
        '
        Me.lblDeviceId.Location = New System.Drawing.Point(104, 136)
        Me.lblDeviceId.Name = "lblDeviceId"
        Me.lblDeviceId.Size = New System.Drawing.Size(120, 16)
        Me.lblDeviceId.TabIndex = 8
        Me.lblDeviceId.Text = "Label5"
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(24, 64)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(80, 16)
        Me.Label5.TabIndex = 9
        Me.Label5.Text = "Volume Name"
        '
        'lblVolumeName
        '
        Me.lblVolumeName.Location = New System.Drawing.Point(104, 64)
        Me.lblVolumeName.Name = "lblVolumeName"
        Me.lblVolumeName.Size = New System.Drawing.Size(88, 16)
        Me.lblVolumeName.TabIndex = 10
        Me.lblVolumeName.Text = "Label6"
        '
        'Label6
        '
        Me.Label6.Location = New System.Drawing.Point(24, 160)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(72, 16)
        Me.Label6.TabIndex = 11
        Me.Label6.Text = "File System"
        '
        'lblFilesystem
        '
        Me.lblFilesystem.Location = New System.Drawing.Point(104, 160)
        Me.lblFilesystem.Name = "lblFilesystem"
        Me.lblFilesystem.TabIndex = 12
        Me.lblFilesystem.Text = "Label7"
        '
        'ListView2
        '
        Me.ListView2.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.ListView2.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.Title, Me.length})
        Me.ListView2.FullRowSelect = True
        Me.ListView2.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable
        Me.ListView2.Location = New System.Drawing.Point(216, 32)
        Me.ListView2.Name = "ListView2"
        Me.ListView2.Scrollable = False
        Me.ListView2.Size = New System.Drawing.Size(312, 184)
        Me.ListView2.TabIndex = 14
        Me.ListView2.View = System.Windows.Forms.View.Details
        '
        'Title
        '
        Me.Title.Text = "Song Title"
        Me.Title.Width = 154
        '
        'length
        '
        Me.length.Text = "Song Length"
        Me.length.Width = 184
        '
        'Combo1
        '
        Me.Combo1.Location = New System.Drawing.Point(8, 8)
        Me.Combo1.Name = "Combo1"
        Me.Combo1.Size = New System.Drawing.Size(200, 21)
        Me.Combo1.TabIndex = 17
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(48, 184)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(112, 24)
        Me.Button1.TabIndex = 18
        Me.Button1.Text = "Check CDDB"
        '
        'ComboBox1
        '
        Me.ComboBox1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.ComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.ComboBox1.Location = New System.Drawing.Point(216, 8)
        Me.ComboBox1.Name = "ComboBox1"
        Me.ComboBox1.Size = New System.Drawing.Size(312, 21)
        Me.ComboBox1.TabIndex = 19
        '
        'CheckBox1
        '
        Me.CheckBox1.Checked = True
        Me.CheckBox1.CheckState = System.Windows.Forms.CheckState.Checked
        Me.CheckBox1.Location = New System.Drawing.Point(32, 32)
        Me.CheckBox1.Name = "CheckBox1"
        Me.CheckBox1.Size = New System.Drawing.Size(152, 16)
        Me.CheckBox1.TabIndex = 20
        Me.CheckBox1.Text = "Catalog CD Contents"
        '
        'Timer1
        '
        Me.Timer1.Enabled = True
        Me.Timer1.SynchronizingObject = Me
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(528, 221)
        Me.Controls.Add(Me.CheckBox1)
        Me.Controls.Add(Me.ComboBox1)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.Combo1)
        Me.Controls.Add(Me.ListView2)
        Me.Controls.Add(Me.lblFilesystem)
        Me.Controls.Add(Me.Label6)
        Me.Controls.Add(Me.lblVolumeName)
        Me.Controls.Add(Me.Label5)
        Me.Controls.Add(Me.lblDeviceId)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.lblSize)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.lblSerialNumber)
        Me.Controls.Add(Me.Label2)
        Me.Name = "Form1"
        Me.Text = "Media Type"
        CType(Me.Timer1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    ' Constants used to determine if a new CD was put in
    Public Const WM_DEVICECHANGE As Long = &H219&
    Public Const DBT_DEVICEARRIVAL As Long = &H8000&
    Public Const DBT_DEVICEREMOVECOMPLETE As Long = &H8004&


    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)

        Select Case m.Msg
            Case WM_DEVICECHANGE
                Select Case m.WParam.ToInt32
                    Case DBT_DEVICEARRIVAL
                        lookup()
                    Case DBT_DEVICEREMOVECOMPLETE
                        lookup()
                End Select
        End Select

    End Sub
 
    Private Sub lookup()

        Dim query As New SelectQuery("Win32_LogicalDisk", "DriveType=5")

        ' Initialize an object searcher with this query
        Dim searcher As New ManagementObjectSearcher(query)

        ' Get the resulting collection and loop through it
        Dim disk As ManagementBaseObject

        For Each disk In searcher.Get()
            Combo1.Items.Add(disk("VolumeName"))
        Next
        Combo1.SelectedIndex = 0
       
    End Sub
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lookup()
    End Sub


    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If CheckBox1.Checked = True Then
            If Mid(Combo1.Text, 1, Combo1.Text.Length - 5) = "Audio CD" Then
                MsgBox("test")
            Else
                MsgBox("This is not a Music CD")
            End If
        Else
            MsgBox("You have selected to Not catalog your Cd")
        End If
    End Sub

End Class
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
With searcher.Get I don't get an error, but it locks my machine up HARD.  I have worked a lot with WMI, but not in this particular area.  

I am trying to use my experience to both help and learn from this question.  
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
I have 2 CD ROM drives (DVD + Burner), and I guess it was giving me an error when there wasn't anything in the drive.  I made the following changes, and got it to run (VB.NET 2003, if that makes a difference):

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

        Select Case m.Msg
            Case WM_DEVICECHANGE
                Select Case m.WParam.ToInt32
                    Case DBT_DEVICEARRIVAL
                        lookup()
                    Case DBT_DEVICEREMOVECOMPLETE
                        lookup()
                End Select
        End Select

      MyBase.WndProc(m)   <--- Moved this here.  Don't know if it helped, but I was saw in the trace WndProc and then Lookup at an unexpected time.

   End Sub


   Private Sub lookup()

      Dim query As New SelectQuery("Win32_LogicalDisk", "DriveType=5")

      ' Initialize an object searcher with this query
      Dim searcher As New ManagementObjectSearcher(query)

      ' Get the resulting collection and loop through it
      Dim disk As ManagementBaseObject
      Dim volumeName As String

      For Each disk In searcher.Get()

         If Not disk Is Nothing Then


            Try

               volumeName = disk("VolumeName").ToString

            Catch ex As Exception

               ' Don't do anything here.

            End Try

            If Not volumeName Is Nothing Then
               Combo1.Items.Add(volumeName)
            End If

         End If

      Next

      Combo1.SelectedIndex = 0

   End Sub
0
 

Author Comment

by:edfrazier
Comment Utility
The only was I can get it to work so far is to move the following out of the sub and put it at the top of the form.

  Dim query As New SelectQuery("Win32_LogicalDisk", "DriveType=5")
    Dim searcher As New ManagementObjectSearcher(query)
    Dim disk As ManagementBaseObject
    Dim cboIndex As Integer

Then I created a timer and within the timer I added this code:

    Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
        lookup()
        Timer1.Enabled = False
    End Sub

Then i called the timer when I received the cdinfo like so.

 Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)

        Select Case m.Msg
            Case WM_DEVICECHANGE
                Select Case m.WParam.ToInt32
                    Case DBT_DEVICEARRIVAL
                        ' CD Added
                        Timer1.Enabled = True
                    Case DBT_DEVICEREMOVECOMPLETE
                        ' CD Removed
                        Timer1.Enabled = True
                End Select
        End Select
    End Sub

This is Definately not the correct way to do it but is seems to work MOST of the time.  

I really need to try to figure out the correct way...  I don't have a very good feeling about what I coded here.  
0
 

Author Comment

by:edfrazier
Comment Utility
I will try what you did and see if it works!
0
 

Author Comment

by:edfrazier
Comment Utility
It still does not run for me.

did you fill the combobox on the load of the form first and then try opening the cd tray?
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Okay, now we are on the same page.  I didn't eject the CD before.  When I did, I got the unhandle exception, just like you did.
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
This really sounds like a bug in the DLL, but I don't have a workaround yet.
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Here is something that I have discovered, was interested in before, but didn't take the time to fully implement:

Receiving Synchronous and Semisynchronous Event Notifications
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/receiving_synchronous_and_semisynchronous_event_notifications.asp

Excerpt:  The following example shows how to monitor insertion and removal of disks from a floppy disk drive on a local machine...

I didn't have time to test it out, and I am going home for the weekend, and leaving this far behind.  Will pick it back up on Monday, since I am very interested in this.
0
 

Author Comment

by:edfrazier
Comment Utility
Great thanks!  I will look into this and see if I can make any sense of it!  Have a good weekend!  Thanks for the help!
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Did you get a chance to look at this.  I am interested in how to get it to work for myself.
0
 

Author Comment

by:edfrazier
Comment Utility
I started looking at it, but did not come up with anything.  I am not sure if I understood the atrucle 100%
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
Comment Utility
Okay, this can be classified as a good Monday.  I found this:

WMI:  System.Management Lets You Take Advantage of WMI APIs within Managed Code
http://msdn.microsoft.com/msdnmag/issues/02/05/WMIMan/default.aspx

Within the article was a reference to WMI Extensions for Visual Studio .NET Server Explorer:

Management (WMI) Extensions for Visual Studio .NET 2002 Server Explorer (RTM)
http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=EF7594D3-4907-4AF6-B7D8-6E22115FFAF0

Management (WMI) Extensions for Visual Studio .NET 2003 Server Explorer
http://www.microsoft.com/downloads/details.aspx?FamilyID=62d91a63-1253-4ea6-8599-68fb3ef77de1&displaylang=en

It adds WMI instrumentation to the server explorer.  I tested it out, and go this it work.  The server explorer allows you to create events for WMI, and then drag/drop them on to the form.  When I did that I got a ManagementEventWatcher added to the form with the following query taken from the article in the previous comment:

Select * from __InstanceModificationEvent within 10 WHERE TargetInstance ISA 'Win32_LogicalDisk'

In Form_Load, there is a ManagementEventWatcher.Start to  subscribe to the event, and in Form_Closing ManagementEventWatcher.Stop to cancel subscription.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:edfrazier
Comment Utility
I loaded the extensions.  I am not sure how to create the query?  I see the Management Classes and Managment Events.  I went to events but I am not sure what to pick?  Am I in the right place?
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
This is a learning experience as well as a teaching experience, so bear with me here.

(1)  Under Servers find the local server, and expand the node.
(2)  Find Management Events.
(3)  Right-click and select 'Add Event Query'.
(4)  Find the root\CIMV2 node in the 'Available Classes' node.
(5)  Select 'Disk Volumes' node in the tree.
(6)  Click on OK (You get this query:  SELECT * FROM __InstanceCreationEvent WITHIN 60 WHERE TargetInstance ISA "Win32_LogicalDisk")
(7)  Drag/drop this new event from the explorer to the form.  You get the ManagementEventWatcher control on the form with the event query you dragged.
(8)  Add ManagementEventWatcher1.Start/Stop in the form events.
(9)  Add an event sink for ManagementEventWatcher1_EventArrived.

Let me know if I left anything out.
0
 

Author Comment

by:edfrazier
Comment Utility
Disk Volumes in not a node
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Give me a clue as to what kind of nodes that you find under root\CIMV2?  What VB.NET version (2002 or 2003)?
0
 

Author Comment

by:edfrazier
Comment Utility
I am running 2003.


































































































































































































0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
I don't see anything for the node examples, just a lot of white space.
0
 

Author Comment

by:edfrazier
Comment Utility
oops, My desk pad was on my enter key...

Application and System Events
Event Comsumer Failure Events
Event Dropped Events
Event Queue overfolw events
Power managament events
WMI System Events

That's all I see under root\CIMV2


0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
I have Directories, Directory Specifications (MSI), Disk Drives, Disk Partitions, Disk Volume Quota Settings, Disk Volumes, etc.

Also what operating system do you have (Windows XP Professional SP1 here).
0
 

Author Comment

by:edfrazier
Comment Utility
I found it, for some reason, custom was checked.  sorry!
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Another point to remember is the event polling interval (default = 60 seconds).  In the example "Select * from __InstanceModificationEvent within 10 WHERE TargetInstance ISA 'Win32_LogicalDisk'", the polling interval is 10 seconds.

There are a lot of event traps, so it can be both challenging and fun to try to find the right one.  I am always looking for the definitive resource on WMI references, since Micro$oft's is not very helpful (a lot of "geek speak").
0
 

Author Comment

by:edfrazier
Comment Utility
I added everything.  What is an event sink?  I added a msgbox to Event arrived but nothing seems to happen when I open and close the door.  It works for you?
0
 

Author Comment

by:edfrazier
Comment Utility
I changed the interval to 2 and I hear something churning every 2 seconds...
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
An event sink is code that can handle an event being raised.  

If you have a message box in the EventArrived section and nothing happens, then I would guess that you forgot the ManagementEventWatcher.Start in the Form_Load event.

I got it to work in the test form that I created using your form code.  It detects disk changes without error.
0
 

Author Comment

by:edfrazier
Comment Utility
I put this in the form load:

ManagementEventWatcher1.Start()

This in the form close

ManagementEventWatcher1.Stop()

and this is my event

  Private Sub ManagementEventWatcher1_EventArrived(ByVal sender As System.Object, ByVal e As System.Management.EventArrivedEventArgs) Handles ManagementEventWatcher12.EventArrived
        MsgBox("Event arrived")

    End Sub
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Is the control ManagementEventWatcher1 or ManagementEventWatcher12?  

The Handles for the event says ManagementEventWatcher12, I would think it should read ManagementEventWatcher1.
0
 

Author Comment

by:edfrazier
Comment Utility
It's 1, I added another control to try something else....  The actual watcher Event  has the correct handle...

 CDWatcher.Start()
 CDWatcher.Stop()

    Private Sub CDWatcher_EventArrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs) Handles CDWatcher.EventArrived
        MsgBox("Event")
    End Sub
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
And still no message box?
0
 

Author Comment

by:edfrazier
Comment Utility
Nope!  Also, as soon as I open up my project I can heard something churning every 2 seconds until I reboot my PC.  Does yours do this?  if I then run the project it churns twice but as soon as I cancel the debugger, it goes back to churning once until I reboot.  
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
No, I don't hear anything.  What is the query text from the watcher?  Do you still have the old code for WndProc?
0
 

Author Comment

by:edfrazier
Comment Utility
I commented out the WndProc...

I am not sure why the PC is making the noise.  I did find out what the problem why the event is not firing.  I did not select "object modification" when I created the event.  It was set at "object creation".  Duh on my part.  
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Understood.  

This is the query text:
Select * from __InstanceModificationEvent within 10 WHERE TargetInstance ISA 'Win32_LogicalDisk'

I guess what you had wasn't __InstanceModificationEvent.  I remember now that I had copied and pasted this text from one of the previous posts about the Microsoft article that talked about monitoring floppies.

So many things to keep straight here.  Now I know what is important, and what steps need to be done in the future.

I can think what the "noise" is, but it sounds like it is connected?  Is it a hard drive or CD-ROM noise?  Maybe I don't here it because my disk drive and CD-ROM are so quiet.
0
 

Author Comment

by:edfrazier
Comment Utility
Yup, I think your right.  This is working!  My last question is you used 'Win32_LogicalDisk', why not use 'Win32_CDROMDrive?  any specific reason?  Thank you for your help.  I just tried Win32_CDROMDrive and it seems to work...  
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
I just took a clue from your WMI code, where you were searching for Win32_LogicalDisk.  Didn't work outside the box on that one, but now that I know about Win32_CDROMDrive, then I can put it in the memory banks for later.  There is still so much to learn about WMI, it can be a very complex beast to find what you need.  

Currently I am trying to figure out how Windows XP monitors network cable disconnects, because I think that it might be possible with WMI.  I just haven't found a way yet.  I've got to find just the right query.
0
 

Author Comment

by:edfrazier
Comment Utility
yup, this is a cool solution.  Thanks again!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

9 Experts available now in Live!

Get 1:1 Help Now