Portable Device Detection

2020-04-19 19:02发布

I need a functionality in my program (written in VB.NET) that detects whether the USB Portable Device (Windows CE 5.0) is inserted or removed. I have found a VB.NET code from the internet but it only works with USB Storage Devices... I only found codes and sample programs (written in C++) that does this USB Portable Device detection, but I can't understand the logic/program flow so I can't convert it to VB.NET

Here are the VB.NET codes that detects USB Storage Devices (lacks detection for USB Portable Devices):

Public Class Form1

    Private WM_DEVICECHANGE As Integer = &H219

    Public Enum WM_DEVICECHANGE_WPPARAMS As Integer
        DBT_CONFIGCHANGECANCELED = &H19
        DBT_CONFIGCHANGED = &H18
        DBT_CUSTOMEVENT = &H8006
        DBT_DEVICEARRIVAL = &H8000
        DBT_DEVICEQUERYREMOVE = &H8001
        DBT_DEVICEQUERYREMOVEFAILED = &H8002
        DBT_DEVICEREMOVECOMPLETE = &H8004
        DBT_DEVICEREMOVEPENDING = &H8003
        DBT_DEVICETYPESPECIFIC = &H8005
        DBT_DEVNODES_CHANGED = &H7
        DBT_QUERYCHANGECONFIG = &H17
        DBT_USERDEFINED = &HFFFF
    End Enum

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_DEVICECHANGE Then
            Select Case m.WParam
                Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEARRIVAL
                    lblMessage.Text = "USB Inserted"
                Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEREMOVECOMPLETE
                    lblMessage.Text = "USB Removed"
                End Select
        End If
        MyBase.WndProc(m)
    End Sub
End Class

What must I add to this code so it could detect Windows USB Portable Devices as well? I need the codes to be in VB.Net...

BTW, Using the program written in C++, it says that my USB Portable Device has the following Properties:

VID - 045E
PID - 00CE

Thanks for helping out! :)

1条回答
在下西门庆
2楼-- · 2020-04-19 19:52
Imports System.Runtime.InteropServices
Public Class Form1
'Used to detected if any of the messages are any of these constants values.
Private Const WM_DEVICECHANGE As Integer = &H219
Private Const DBT_DEVICEARRIVAL As Integer = &H8000
Private Const DBT_DEVICEREMOVECOMPLETE As Integer = &H8004
Private Const DBT_DEVTYP_VOLUME As Integer = &H2  '
'
'Get the information about the detected volume.
Private Structure DEV_BROADCAST_VOLUME
    Dim Dbcv_Size As Integer
    Dim Dbcv_Devicetype As Integer
    Dim Dbcv_Reserved As Integer
    Dim Dbcv_Unitmask As Integer
    Dim Dbcv_Flags As Short
End Structure

Protected Overrides Sub WndProc(ByRef M As System.Windows.Forms.Message)
    '
    'These are the required subclassing codes for detecting device based removal and arrival.
    '
    If M.Msg = WM_DEVICECHANGE Then
        Select Case M.WParam
            '
            'Check if a device was added.
            Case DBT_DEVICEARRIVAL
                Dim DevType As Integer = Runtime.InteropServices.Marshal.ReadInt32(M.LParam, 4)
                If DevType = DBT_DEVTYP_VOLUME Then
                    Dim Vol As New DEV_BROADCAST_VOLUME
                    Vol = Runtime.InteropServices.Marshal.PtrToStructure(M.LParam, GetType(DEV_BROADCAST_VOLUME))
                    If Vol.Dbcv_Flags = 0 Then
                        For i As Integer = 0 To 20
                            If Math.Pow(2, i) = Vol.Dbcv_Unitmask Then
                                Dim Usb As String = Chr(65 + i) + ":\"
                                MsgBox("Looks like a USB device was plugged in!" & vbNewLine & vbNewLine & "The drive letter is: " & Usb.ToString)
                                Exit For
                            End If
                        Next
                    End If
                End If
                '
                'Check if the message was for the removal of a device.
            Case DBT_DEVICEREMOVECOMPLETE
                Dim DevType As Integer = Runtime.InteropServices.Marshal.ReadInt32(M.LParam, 4)
                If DevType = DBT_DEVTYP_VOLUME Then
                    Dim Vol As New DEV_BROADCAST_VOLUME
                    Vol = Runtime.InteropServices.Marshal.PtrToStructure(M.LParam, GetType(DEV_BROADCAST_VOLUME))
                    If Vol.Dbcv_Flags = 0 Then
                        For i As Integer = 0 To 20
                            If Math.Pow(2, i) = Vol.Dbcv_Unitmask Then
                                Dim Usb As String = Chr(65 + i) + ":\"
                                MsgBox("Looks like a volume device was removed!" & vbNewLine & vbNewLine & "The drive letter is: " & Usb.ToString)
                                Exit For
                            End If
                        Next
                    End If
                End If
        End Select
    End If
    MyBase.WndProc(M)
End Sub
End Class
查看更多
登录 后发表回答