Home All Groups Group Topic Archive Search About

USB Pen Drive Detection - Retrieving

Author
3 Mar 2006 8:18 PM
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

Author
4 Mar 2006 11:17 AM
Ken Tucker [MVP]
Hi,

        Why dont you let the wmi tell you when the usb drive is inserted?

http://www.vb-tips.com/default.aspx?ID=32ae3120-ae05-4860-989a-07a4ef9d9504

Ken
-------------------
Show quoteHide quote
"Chad" <chad.ca***@gmail.com> wrote in message
news:1141417097.173280.154130@e56g2000cwe.googlegroups.com...
> 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
>
Author
4 Mar 2006 3:17 PM
Ken Tucker [MVP]
Hi,

       You are getting the error with your code because you are calling the
wmi from the wrong thread.  Use control.invoke to get the wmi call on the
right thread.

Ken
--------------------------

Show quoteHide quote
"Ken Tucker [MVP]" wrote:

> Hi,
>
>         Why dont you let the wmi tell you when the usb drive is inserted?
>
> http://www.vb-tips.com/default.aspx?ID=32ae3120-ae05-4860-989a-07a4ef9d9504
>
> Ken
> -------------------
> "Chad" <chad.ca***@gmail.com> wrote in message
> news:1141417097.173280.154130@e56g2000cwe.googlegroups.com...
> > 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
> >
>
>
>
Author
6 Mar 2006 7:03 PM
Chad
I still get the same error when I call using the Invoke method.  I
added the following code:

'Declaring the Delegates for arrival and removal
    Delegate Sub DiskArrival()
    Delegate Sub DiskRemoval()

'For the calling of the Arrival and Removal Methods I modified to be
the following
   Me.Invoke(New DiskArrival(AddressOf Arrival))
   Me.Invoke(New DiskRemoval(AddressOf Removal))

I get (An unhandled exception of type 'System.ExecutionEngineException'
occurred in system.management.dll)

Am I calling the method wrong?  I have also tried invoking the
CheckDrive method the same way, and I get the same error.

Thanks
Author
7 Mar 2006 12:31 AM
Ken Tucker [MVP]
Chad,

         I figured it out.  When the checkdisk procedure is running the wmi
is still adding the new drive to the list.  That is the reason for the error.
I tried a bunch of differnet ways but wound up adding a timer to the form. 
I enable the timer in the arrive procedure.  In the timer event I call the
checkdrive and disable the timer.  The timer gives the wmi a chance to do its
thing.

   Private Sub Arrival()
        MsgBox("Arrival")
        Timer1.Enabled = True
    End Sub


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
        CheckDrive()
        Timer1.Enabled = False
    End Sub

Ken
------------------
Show quoteHide quote
"Chad" wrote:

> I still get the same error when I call using the Invoke method.  I
> added the following code:
>
> 'Declaring the Delegates for arrival and removal
>     Delegate Sub DiskArrival()
>     Delegate Sub DiskRemoval()
>
> 'For the calling of the Arrival and Removal Methods I modified to be
> the following
>    Me.Invoke(New DiskArrival(AddressOf Arrival))
>    Me.Invoke(New DiskRemoval(AddressOf Removal))
>
> I get (An unhandled exception of type 'System.ExecutionEngineException'
> occurred in system.management.dll)
>
> Am I calling the method wrong?  I have also tried invoking the
> CheckDrive method the same way, and I get the same error.
>
> Thanks
>
>
Author
7 Mar 2006 10:55 PM
Chad
Excellent!  It works like a charm.
Thanks for your help Ken.

Chad
Author
30 Mar 2006 9:21 AM
Chris Lewis
Hi Chad,

I'm trying to do something very similar. Earlier in the thread Ken Tucker
asked why you don't use a WMI 'ManagementEventWatcher' to watch for new
devices? I was wondering the same thing. Do you have a specific reason?

Chris

Show quoteHide quote
"Chad" wrote:

> Excellent!  It works like a charm.
> Thanks for your help Ken.
>
> Chad
>
>
Author
5 Mar 2006 6:07 PM
LOL @ The MVPs
That VB Tips website is so full of bugs & the code examples don't compile.
Take the webcam capture example: In the click event of button 1, it hasn't a
correct API signature for it

As for clicking a slow opening VB tip, then click the LINKS option on the
left & it shows a tip & the links. If you click back on the tips then you
get two tips.

The site was written by someone who 1) cannot write VB (so-called MVP's) &
2) has no concept of ASPX coding

A MVP should effectively know what they are talking about, but this site
demonstrates that these two people obviously don't






"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%23H%233G13PGHA.2300@TK2MSFTNGP15.phx.gbl...
> Hi,
>
>         Why dont you let the wmi tell you when the usb drive is inserted?
>
>
http://www.vb-tips.com/default.aspx?ID=32ae3120-ae05-4860-989a-07a4ef9d9504
Show quoteHide quote
>
> Ken
> -------------------
> "Chad" <chad.ca***@gmail.com> wrote in message
> news:1141417097.173280.154130@e56g2000cwe.googlegroups.com...
> > 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
> >
>
>
Author
28 Mar 2006 5:21 AM
HaKiSaK MCSE
Instead of being a pontificating braggadocio, why don't you try and be
constructive with your criticism and offer solutions as opposed to just being
a blow hard.
--
Steve Kemp MCSE


Show quoteHide quote
"LOL @ The MVPs" wrote:

> That VB Tips website is so full of bugs & the code examples don't compile.
> Take the webcam capture example: In the click event of button 1, it hasn't a
> correct API signature for it
>
> As for clicking a slow opening VB tip, then click the LINKS option on the
> left & it shows a tip & the links. If you click back on the tips then you
> get two tips.
>
> The site was written by someone who 1) cannot write VB (so-called MVP's) &
> 2) has no concept of ASPX coding
>
> A MVP should effectively know what they are talking about, but this site
> demonstrates that these two people obviously don't
>
>
>
>
>
>
> "Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
> news:%23H%233G13PGHA.2300@TK2MSFTNGP15.phx.gbl...
> > Hi,
> >
> >         Why dont you let the wmi tell you when the usb drive is inserted?
> >
> >
> http://www.vb-tips.com/default.aspx?ID=32ae3120-ae05-4860-989a-07a4ef9d9504
> >
> > Ken
> > -------------------
> > "Chad" <chad.ca***@gmail.com> wrote in message
> > news:1141417097.173280.154130@e56g2000cwe.googlegroups.com...
> > > 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
> > >
> >
> >
>
>
>