1

我需要在我的程序(用 VB.NET 编写)中检测是否插入或移除 USB 便携式设备(Windows CE 5.0)的功能。我从互联网上找到了一个 VB.NET 代码,但它只适用于 USB 存储设备......我只找到了执行此 USB 便携式设备检测的代码和示例程序(用 C++ 编写),但我无法理解逻辑/program flow 所以我无法将其转换为 VB.NET

以下是检测 USB 存储设备的 VB.NET 代码(缺少对 USB 便携式设备的检测):

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

我必须在此代码中添加什么以便它也可以检测 Windows USB 便携式设备?我需要将代码放在 VB.Net 中...

顺便说一句,使用用 C++ 编写的程序,它说我的 USB 便携式设备具有以下属性:

VID - 045E
PID - 00CE

感谢您的帮助!:)

4

1 回答 1

1
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
于 2015-02-24T08:17:36.397 回答