便携式设备检测 [英] Portable Device Detection

查看:32
本文介绍了便携式设备检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

我必须向此代码添加什么才能使其也能检测到 Windows USB 便携式设备?我需要将代码放在 VB.Net 中...

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...

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

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

VID - 045E
PID - 00CE

感谢您的帮助!:)

推荐答案

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

这篇关于便携式设备检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆