J
jan axelson
My application is using RegisterDeviceNotification() to detect
attachment and removal of a USB HID-class device.
The form is receiving WM_DEVICECHANGE messages with wParam set to
DBT_DEVICEARRIVAL or DBT_DEVICEREMOVECOMPLETE.
I want to identify the device that has arrived or been removed by
examining the dbcc_name member of the DEV_BROADCAST_DEVICEINTERFACE
structure.
This is my declaration for DEV_BROADCAST_DEVICEINTERFACE:
<StructLayout(LayoutKind.Sequential)> _
Public Structure DEV_BROADCAST_DEVICEINTERFACE
Dim dbcc_size As Integer
Dim dbcc_devicetype As Integer
Dim dbcc_reserved As Integer
Dim dbcc_classguid As Guid
Dim dbcc_name As Short
End Structure
This is my code to call RegisterDeviceNotification:
Dim dbi As New DEV_BROADCAST_DEVICEINTERFACE
Dim size As Integer
size = Marshal.SizeOf(dbi)
dbi.dbcc_size = size
dbi.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE
dbi.dbcc_reserved = 0
dbi.dbcc_classguid = HidGuid 'obtained with HidD_GetHidGuid()
dbi.dbcc_name = 0
Dim buffer As IntPtr = Marshal.AllocHGlobal(size)
Marshal.StructureToPtr(dbi, Buffer, True)
Dim r As IntPtr
r = RegisterDeviceNotification(frmMy.Handle, buffer,
DEVICE_NOTIFY_WINDOW_HANDLE)
This is (a portion of) the WndProc subroutine:
Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
Dim broadcastHeader As DEV_BROADCAST_HDR
Select Case m.Msg
'Look for a WM_DEVICECHANGE message.
Case WM_DEVICECHANGE
lstResults.Items.Add("WM_DEVICECHANGE")
If (m.WParam.ToInt32 = DBT_DEVICEARRIVAL) Then
lstResults.Items.Add("DBT_DEVICEARRIVAL")
'LParam is a pointer to a structure that begins with a
DEV_BROADCAST_HDR structure.
broadcastHeader = _
CType(m.GetLParam(broadcastHeader.GetType),
DEV_BROADCAST_HDR)
'Is it a device interface?
If (broadcastHeader.dbch_devicetype =
DBT_DEVTYP_DEVICEINTERFACE) Then
'LParam is a pointer to a DEV_BROADCAST_DEVICEINTERFACE
structure.
broadcastDeviceInterface = _
CType(m.GetLParam(broadcastDeviceInterface.GetType),
DEV_BROADCAST_DEVICEINTERFACE)
lstResults.Items.Add("size = " &
CStr(broadcastDeviceInterface.dbcc_size))
The dbcc_size parameter returns 194, but I've been unsuccessful at
retrieving broadcastDeviceInterface.dbcc_name. I've also tried
declaring dbcc_name in DEV_BROADCAST_DEVICEINTERFACE as:
<MarshalAs(UnmanagedType.LPTStr)> Dim dbcc_name As String
and:
Dim dbcc_name As String
and:
Dim dbcc_name as IntPtr
and using Marshal.PtrToStringAuto
Any suggestions welcome.
Jan Axelson
www.Lvr.com
attachment and removal of a USB HID-class device.
The form is receiving WM_DEVICECHANGE messages with wParam set to
DBT_DEVICEARRIVAL or DBT_DEVICEREMOVECOMPLETE.
I want to identify the device that has arrived or been removed by
examining the dbcc_name member of the DEV_BROADCAST_DEVICEINTERFACE
structure.
This is my declaration for DEV_BROADCAST_DEVICEINTERFACE:
<StructLayout(LayoutKind.Sequential)> _
Public Structure DEV_BROADCAST_DEVICEINTERFACE
Dim dbcc_size As Integer
Dim dbcc_devicetype As Integer
Dim dbcc_reserved As Integer
Dim dbcc_classguid As Guid
Dim dbcc_name As Short
End Structure
This is my code to call RegisterDeviceNotification:
Dim dbi As New DEV_BROADCAST_DEVICEINTERFACE
Dim size As Integer
size = Marshal.SizeOf(dbi)
dbi.dbcc_size = size
dbi.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE
dbi.dbcc_reserved = 0
dbi.dbcc_classguid = HidGuid 'obtained with HidD_GetHidGuid()
dbi.dbcc_name = 0
Dim buffer As IntPtr = Marshal.AllocHGlobal(size)
Marshal.StructureToPtr(dbi, Buffer, True)
Dim r As IntPtr
r = RegisterDeviceNotification(frmMy.Handle, buffer,
DEVICE_NOTIFY_WINDOW_HANDLE)
This is (a portion of) the WndProc subroutine:
Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
Dim broadcastHeader As DEV_BROADCAST_HDR
Select Case m.Msg
'Look for a WM_DEVICECHANGE message.
Case WM_DEVICECHANGE
lstResults.Items.Add("WM_DEVICECHANGE")
If (m.WParam.ToInt32 = DBT_DEVICEARRIVAL) Then
lstResults.Items.Add("DBT_DEVICEARRIVAL")
'LParam is a pointer to a structure that begins with a
DEV_BROADCAST_HDR structure.
broadcastHeader = _
CType(m.GetLParam(broadcastHeader.GetType),
DEV_BROADCAST_HDR)
'Is it a device interface?
If (broadcastHeader.dbch_devicetype =
DBT_DEVTYP_DEVICEINTERFACE) Then
'LParam is a pointer to a DEV_BROADCAST_DEVICEINTERFACE
structure.
broadcastDeviceInterface = _
CType(m.GetLParam(broadcastDeviceInterface.GetType),
DEV_BROADCAST_DEVICEINTERFACE)
lstResults.Items.Add("size = " &
CStr(broadcastDeviceInterface.dbcc_size))
The dbcc_size parameter returns 194, but I've been unsuccessful at
retrieving broadcastDeviceInterface.dbcc_name. I've also tried
declaring dbcc_name in DEV_BROADCAST_DEVICEINTERFACE as:
<MarshalAs(UnmanagedType.LPTStr)> Dim dbcc_name As String
and:
Dim dbcc_name As String
and:
Dim dbcc_name as IntPtr
and using Marshal.PtrToStringAuto
Any suggestions welcome.
Jan Axelson
www.Lvr.com