Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
827 views
in Technique[技术] by (71.8m points)

vb.net - How to detect insertion and removal of other USB Peripherals?

Why is it that this VB.NET code only works for detecting flash disks?

Select Case m.WParam
    Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEARRIVAL
        MsgBox("USB Inserted")
    Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEREMOVECOMPLETE
        MsgBox("USB Removed")
End Select

What would be the possible way to detect the insertion and removal of other USB peripherals, such as mouse and keyboard?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want additional devices, you have to call RegisterDeviceNotification with the classes of devices that you wish to detect.

<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("user32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function UnregisterDeviceNotification( _
   ByVal hHandle As IntPtr) As UInteger
End Function

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Public Class DEV_BROADCAST_DEVICEINTERFACE
    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

Public Const DEVICE_NOTIFY_ALL_INTERFACE_CLASSES As Integer = &H4
Public Const DEVICE_NOTIFY_WINDOW_HANDLE As Short = 0

Private Const WM_POWERBROADCAST As Integer = &H218
Private Const PBT_APMSUSPEND As Integer = &H4

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
Private Const DBT_DEVTYP_DEVICEINTERFACE As Integer = &H5
Private Const DBT_DEVTYP_HANDLE As Integer = &H6
Private Const DBT_DEVTYP_OEM As Integer = &H0
Private Const DBT_DEVTYP_PORT As Integer = &H3

Private Sub RegisterDeviceNotifications()
    'Registers the system to notify us about interfaces when they are plugged in and unplugged.
    'http://msdn.microsoft.com/en-us/library/aa363431(VS.85).aspx
    Dim deviceInterface As New DEV_BROADCAST_DEVICEINTERFACE()
    Dim size As Integer = Marshal.SizeOf(deviceInterface)
    deviceInterface.dbcc_size = size
    deviceInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE
    Dim buffer As IntPtr = Nothing
    buffer = Marshal.AllocHGlobal(size)
    Marshal.StructureToPtr(deviceInterface, buffer, True)
    Dim r As IntPtr = Nothing
    r = RegisterDeviceNotification(Me.Handle, buffer, DEVICE_NOTIFY_WINDOW_HANDLE Or DEVICE_NOTIFY_ALL_INTERFACE_CLASSES)

End Sub

You can find more info about how to interpret the data you'll get back here: http://msdn.microsoft.com/en-us/library/aa363431(VS.85).aspx


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...