wmi ManagementObject and WM_DEVICECHANGE vb.net

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

edfrazierAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
What is the full text of the exception message?
0
edfrazierAuthor Commented:
An unhandled exception of type 'System.ExecutionEngineException' occurred in system.management.dll
0
Bob LearnedCommented:
Use Try...Catch...End Try block to capture the full text:

Try

....

Catch ex As System.ExecutionEngineException

   MsgBox(ex.ToString)

End Try
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

edfrazierAuthor Commented:
I added that and it still only shows that message, there is nothing additional
0
Bob LearnedCommented:
I hate when that happens!  What does ex.StackTrace and ex.Source return?
0
edfrazierAuthor Commented:
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
Bob LearnedCommented:
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
edfrazierAuthor Commented:
yes, same message
0
Bob LearnedCommented:
Okay, then, what line does this occur at?
0
edfrazierAuthor Commented:
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
Bob LearnedCommented:
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
edfrazierAuthor Commented:
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
Bob LearnedCommented:
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
Bob LearnedCommented:
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
edfrazierAuthor Commented:
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
edfrazierAuthor Commented:
I will try what you did and see if it works!
0
edfrazierAuthor Commented:
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
Bob LearnedCommented:
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
Bob LearnedCommented:
This really sounds like a bug in the DLL, but I don't have a workaround yet.
0
Bob LearnedCommented:
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
edfrazierAuthor Commented:
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
Bob LearnedCommented:
Did you get a chance to look at this.  I am interested in how to get it to work for myself.
0
edfrazierAuthor Commented:
I started looking at it, but did not come up with anything.  I am not sure if I understood the atrucle 100%
0
Bob LearnedCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
edfrazierAuthor Commented:
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
Bob LearnedCommented:
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
edfrazierAuthor Commented:
Disk Volumes in not a node
0
Bob LearnedCommented:
Give me a clue as to what kind of nodes that you find under root\CIMV2?  What VB.NET version (2002 or 2003)?
0
edfrazierAuthor Commented:
I am running 2003.


































































































































































































0
Bob LearnedCommented:
I don't see anything for the node examples, just a lot of white space.
0
edfrazierAuthor Commented:
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
Bob LearnedCommented:
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
edfrazierAuthor Commented:
I found it, for some reason, custom was checked.  sorry!
0
Bob LearnedCommented:
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
edfrazierAuthor Commented:
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
edfrazierAuthor Commented:
I changed the interval to 2 and I hear something churning every 2 seconds...
0
Bob LearnedCommented:
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
edfrazierAuthor Commented:
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
Bob LearnedCommented:
Is the control ManagementEventWatcher1 or ManagementEventWatcher12?  

The Handles for the event says ManagementEventWatcher12, I would think it should read ManagementEventWatcher1.
0
edfrazierAuthor Commented:
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
Bob LearnedCommented:
And still no message box?
0
edfrazierAuthor Commented:
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
Bob LearnedCommented:
No, I don't hear anything.  What is the query text from the watcher?  Do you still have the old code for WndProc?
0
edfrazierAuthor Commented:
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
Bob LearnedCommented:
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
edfrazierAuthor Commented:
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
Bob LearnedCommented:
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
edfrazierAuthor Commented:
yup, this is a cool solution.  Thanks again!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.