C
Chad
To anyone who is smarter than I am when it comes to WMI:
Here is what I am trying to do:
1) Detect a USB pen drive when it is inserted
2) Retrieve the drive letter of the pen drive
3) Check for a specific folder on the pen drive
I keep receiving an error message that reads:
An unhandled exception of type 'System.ExecutionEngineException'
occurred in system.management.dll
The detection of the USB drive is working correctly, and it calls a
method called Arrival(). Within that method I call a CheckDrive()
method that is supposed to loop through all of the logical disks and
find a removable drive that contains a specific folder. I have also
gotten a System.ExecutionEngineException when the application gets to
the following line of code:
Dim moc As ManagementObjectCollection = disks.GetInstances()
I have also received a "Catastrophic Failure" message when this
same code is used in another conceptual application I wrote.
Is there a better way to do this, maybe I am going about it the wrong
way. Any help would be greatly appreciated.
Here is my code, I am using the DotNetFramework 1.1:
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Text
Imports System.management
Imports System.IO
Public Class DetectorForm
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
RegisterHidNotification()
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.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
'
'DetectorForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(440, 333)
Me.Name = "DetectorForm"
Me.Text = "DetectorForm"
End Sub
#End Region
Private Sub Arrival()
MsgBox("Arrival")
CheckDrive()
End Sub
Private Sub Removal()
MsgBox("Removal")
End Sub
Public Sub CheckDrive()
Try
Dim disks As New ManagementClass("Win32_LogicalDisk")
Dim moc As ManagementObjectCollection =
disks.GetInstances()
Dim mo As ManagementObject
Dim DiskProperties As PropertyDataCollection
Dim DiskProperty As PropertyData
Dim removableDisk As ManagementObject
'For each LogicalDisk on the Computer
For Each mo In moc
'Retrieve the disk's properties
DiskProperties = mo.Properties
'Iterate through the disk's properties
For Each DiskProperty In DiskProperties
'Check whether the particular property is defined
for this drive
If Not IsNothing(DiskProperty.Value) Then
'Check if the drive is removable DriveType=2 -
Removable
If DiskProperty.Name.ToString = "DriveType" _
And DiskProperty.Value.ToString = "2" Then
'Check if this Removable disk is a Zoom
Drive
Dim drvLetter As String
drvLetter =
mo.Properties("DeviceId").Value.ToString()
'Check if a directory exists
If Directory.Exists(drvLetter &
"chadc3322") Then
MsgBox("chadc3322 Exists")
Exit For
End If
End If
End If
Next
Next mo
disks.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Sub RegisterHidNotification()
Dim dbi As DeviceDetector.DEV_BROADCAST_DEVICEINTERFACE = New
DeviceDetector.DEV_BROADCAST_DEVICEINTERFACE
Dim size As Integer
size = Marshal.SizeOf(dbi)
Dim gd As Guid
' MsgBox(Marshal.SizeOf(gd))
' MsgBox(Marshal.SizeOf(New
Win32.DEV_BROADCAST_DEVICEINTERFACE))
dbi.dbcc_size = size
dbi.dbcc_devicetype = DeviceDetector.DBT_DEVTYP_DEVICEINTERFACE
dbi.dbcc_reserved = 0
dbi.dbcc_classguid = DeviceDetector.GUID_IO_MEDIA_ARRIVAL
Dim Buffer As IntPtr
Buffer = Marshal.AllocHGlobal(size)
Marshal.StructureToPtr(dbi, Buffer, True)
Dim r As IntPtr
r = DeviceDetector.RegisterDeviceNotification(Handle, Buffer,
DeviceDetector.DEVICE_NOTIFY_WINDOW_HANDLE)
Marshal.PtrToStructure(Buffer, dbi)
If r.ToInt32 = IntPtr.Zero.ToInt32 Then
'MessageBox.Show(DeviceDetector.GetLastError().ToString())
End If
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = DeviceDetector.WM_DEVICECHANGE Then
OnDeviceChange(m)
End If
MyBase.WndProc(m)
End Sub
Private Sub OnDeviceChange(ByVal msg As Message)
Dim wParam As Integer
wParam = msg.WParam.ToInt32()
If wParam = DeviceDetector.DBT_DEVICEARRIVAL Then
Dim o As New DeviceDetector.DEV_BROADCAST_HDR
Dim b As New DeviceDetector.DEV_BROADCAST_DEVICEINTERFACE1
Dim gd As Guid
Marshal.PtrToStructure(msg.LParam, o)
If (o.dbcc_devicetype =
DeviceDetector.DBT_DEVTYP_DEVICEINTERFACE) Then
Dim strsize As Integer = (o.dbcc_size - 28) / 2
ReDim b.dbcc_name(strsize)
Marshal.PtrToStructure(msg.LParam, b)
Dim str As New String(b.dbcc_name, 0, strsize)
'***********************************************
' Device Arrival
'***********************************************
Arrival()
End If
ElseIf wParam = DeviceDetector.DBT_DEVICEREMOVECOMPLETE Then
'***********************************************
' Device Removal
'***********************************************
Removal()
End If
End Sub
Private Class DeviceDetector
Public Const WM_DEVICECHANGE = &H219
Public Const DBT_DEVICEARRIVAL = &H8000
Public Const DBT_DEVICEREMOVECOMPLETE = &H8004
Public Const DEVICE_NOTIFY_WINDOW_HANDLE = 0
Public Const DEVICE_NOTIFY_SERVICE_HANDLE = 1
Public Const DBT_DEVTYP_DEVICEINTERFACE = 5
Public Shared GUID_IO_MEDIA_ARRIVAL As Guid = New
Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED")
<StructLayout(LayoutKind.Sequential)> _
Public Class DEV_BROADCAST_DEVICEINTERFACE
Public dbcc_size As Integer
Public dbcc_devicetype As Integer
Public dbcc_reserved As Integer
Public dbcc_classguid As Guid
Public dbcc_name As Short
End Class
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
_
Public Class DEV_BROADCAST_DEVICEINTERFACE1
Public dbcc_size As Integer
Public dbcc_devicetype As Integer
Public dbcc_reserved As Integer
<MarshalAs(UnmanagedType.ByValArray,
ArraySubType:=UnmanagedType.U1, SizeConst:=16)> _
Public dbcc_classguid() As Byte
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=128)> _
Public dbcc_name() As Char
End Class
<StructLayout(LayoutKind.Sequential)> _
Public Class DEV_BROADCAST_HDR
Public dbcc_size As Integer
Public dbcc_devicetype As Integer
Public dbcc_reserved As Integer
End Class
<DllImport("user32.DLL", SetLastError:=True)> _
Public Shared Function _
RegisterDeviceNotification(ByVal IntPtr As IntPtr, ByVal
NotificationFilter As IntPtr, ByVal Flags As Int32) As IntPtr
End Function
<DllImport("kernel32.DLL")> _
Public Shared Function _
GetLastError() As Integer
End Function
End Class
End Class
Here is what I am trying to do:
1) Detect a USB pen drive when it is inserted
2) Retrieve the drive letter of the pen drive
3) Check for a specific folder on the pen drive
I keep receiving an error message that reads:
An unhandled exception of type 'System.ExecutionEngineException'
occurred in system.management.dll
The detection of the USB drive is working correctly, and it calls a
method called Arrival(). Within that method I call a CheckDrive()
method that is supposed to loop through all of the logical disks and
find a removable drive that contains a specific folder. I have also
gotten a System.ExecutionEngineException when the application gets to
the following line of code:
Dim moc As ManagementObjectCollection = disks.GetInstances()
I have also received a "Catastrophic Failure" message when this
same code is used in another conceptual application I wrote.
Is there a better way to do this, maybe I am going about it the wrong
way. Any help would be greatly appreciated.
Here is my code, I am using the DotNetFramework 1.1:
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Text
Imports System.management
Imports System.IO
Public Class DetectorForm
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
RegisterHidNotification()
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.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
'
'DetectorForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(440, 333)
Me.Name = "DetectorForm"
Me.Text = "DetectorForm"
End Sub
#End Region
Private Sub Arrival()
MsgBox("Arrival")
CheckDrive()
End Sub
Private Sub Removal()
MsgBox("Removal")
End Sub
Public Sub CheckDrive()
Try
Dim disks As New ManagementClass("Win32_LogicalDisk")
Dim moc As ManagementObjectCollection =
disks.GetInstances()
Dim mo As ManagementObject
Dim DiskProperties As PropertyDataCollection
Dim DiskProperty As PropertyData
Dim removableDisk As ManagementObject
'For each LogicalDisk on the Computer
For Each mo In moc
'Retrieve the disk's properties
DiskProperties = mo.Properties
'Iterate through the disk's properties
For Each DiskProperty In DiskProperties
'Check whether the particular property is defined
for this drive
If Not IsNothing(DiskProperty.Value) Then
'Check if the drive is removable DriveType=2 -
Removable
If DiskProperty.Name.ToString = "DriveType" _
And DiskProperty.Value.ToString = "2" Then
'Check if this Removable disk is a Zoom
Drive
Dim drvLetter As String
drvLetter =
mo.Properties("DeviceId").Value.ToString()
'Check if a directory exists
If Directory.Exists(drvLetter &
"chadc3322") Then
MsgBox("chadc3322 Exists")
Exit For
End If
End If
End If
Next
Next mo
disks.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Sub RegisterHidNotification()
Dim dbi As DeviceDetector.DEV_BROADCAST_DEVICEINTERFACE = New
DeviceDetector.DEV_BROADCAST_DEVICEINTERFACE
Dim size As Integer
size = Marshal.SizeOf(dbi)
Dim gd As Guid
' MsgBox(Marshal.SizeOf(gd))
' MsgBox(Marshal.SizeOf(New
Win32.DEV_BROADCAST_DEVICEINTERFACE))
dbi.dbcc_size = size
dbi.dbcc_devicetype = DeviceDetector.DBT_DEVTYP_DEVICEINTERFACE
dbi.dbcc_reserved = 0
dbi.dbcc_classguid = DeviceDetector.GUID_IO_MEDIA_ARRIVAL
Dim Buffer As IntPtr
Buffer = Marshal.AllocHGlobal(size)
Marshal.StructureToPtr(dbi, Buffer, True)
Dim r As IntPtr
r = DeviceDetector.RegisterDeviceNotification(Handle, Buffer,
DeviceDetector.DEVICE_NOTIFY_WINDOW_HANDLE)
Marshal.PtrToStructure(Buffer, dbi)
If r.ToInt32 = IntPtr.Zero.ToInt32 Then
'MessageBox.Show(DeviceDetector.GetLastError().ToString())
End If
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = DeviceDetector.WM_DEVICECHANGE Then
OnDeviceChange(m)
End If
MyBase.WndProc(m)
End Sub
Private Sub OnDeviceChange(ByVal msg As Message)
Dim wParam As Integer
wParam = msg.WParam.ToInt32()
If wParam = DeviceDetector.DBT_DEVICEARRIVAL Then
Dim o As New DeviceDetector.DEV_BROADCAST_HDR
Dim b As New DeviceDetector.DEV_BROADCAST_DEVICEINTERFACE1
Dim gd As Guid
Marshal.PtrToStructure(msg.LParam, o)
If (o.dbcc_devicetype =
DeviceDetector.DBT_DEVTYP_DEVICEINTERFACE) Then
Dim strsize As Integer = (o.dbcc_size - 28) / 2
ReDim b.dbcc_name(strsize)
Marshal.PtrToStructure(msg.LParam, b)
Dim str As New String(b.dbcc_name, 0, strsize)
'***********************************************
' Device Arrival
'***********************************************
Arrival()
End If
ElseIf wParam = DeviceDetector.DBT_DEVICEREMOVECOMPLETE Then
'***********************************************
' Device Removal
'***********************************************
Removal()
End If
End Sub
Private Class DeviceDetector
Public Const WM_DEVICECHANGE = &H219
Public Const DBT_DEVICEARRIVAL = &H8000
Public Const DBT_DEVICEREMOVECOMPLETE = &H8004
Public Const DEVICE_NOTIFY_WINDOW_HANDLE = 0
Public Const DEVICE_NOTIFY_SERVICE_HANDLE = 1
Public Const DBT_DEVTYP_DEVICEINTERFACE = 5
Public Shared GUID_IO_MEDIA_ARRIVAL As Guid = New
Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED")
<StructLayout(LayoutKind.Sequential)> _
Public Class DEV_BROADCAST_DEVICEINTERFACE
Public dbcc_size As Integer
Public dbcc_devicetype As Integer
Public dbcc_reserved As Integer
Public dbcc_classguid As Guid
Public dbcc_name As Short
End Class
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
_
Public Class DEV_BROADCAST_DEVICEINTERFACE1
Public dbcc_size As Integer
Public dbcc_devicetype As Integer
Public dbcc_reserved As Integer
<MarshalAs(UnmanagedType.ByValArray,
ArraySubType:=UnmanagedType.U1, SizeConst:=16)> _
Public dbcc_classguid() As Byte
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=128)> _
Public dbcc_name() As Char
End Class
<StructLayout(LayoutKind.Sequential)> _
Public Class DEV_BROADCAST_HDR
Public dbcc_size As Integer
Public dbcc_devicetype As Integer
Public dbcc_reserved As Integer
End Class
<DllImport("user32.DLL", SetLastError:=True)> _
Public Shared Function _
RegisterDeviceNotification(ByVal IntPtr As IntPtr, ByVal
NotificationFilter As IntPtr, ByVal Flags As Int32) As IntPtr
End Function
<DllImport("kernel32.DLL")> _
Public Shared Function _
GetLastError() As Integer
End Function
End Class
End Class