By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,345 Members | 1,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,345 IT Pros & Developers. It's quick & easy.

Retrieving dbcc_name string from a DEV_BROADCAST_DEVICEINTERFACE structure

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies

P: n/a
Hi Jan,

Thanks for posting in the community.

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you do not know how to declare the
dbcc_name to return the device name from the LParam in the WinProc.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

I am researching the problem, and I will update you with new information
ASAP.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '05 #2

P: n/a
>From your description, I understand that you do not know how to declare
the dbcc_name to return the device name from the LParam in the WinProc.
Have I fully understood you?

Yes, I'm trying to retrieve the dbcc_name string.

Thanks!

Jan Axelson
www.Lvr.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #3

P: n/a
Thanks for responding. I've made some progress but still don't have
the dbcc_name string in a String variable.

strsize = 83, which equals the number of characters in the device name
and is thus correct.

But this:

Dim str As New String(b.dbcc_name, 0, strsize)

results in str = "/", only the first character.

This code:

Dim count As Integer
For count = 0 To (strsize - 1) * 2
lstResults.Items.Add(o.dbcc_name(count))
Next count

displays all of the characters in the device name, with each character
followed by an extra null character. So the string is there, but in an
incorrect format.

dbcc_name is declared as a Char array, as in your example.

How can I get the complete and correct string into str?

***

A couple of other things:

At this line:

Marshal.PtrToStructure(msg.LParam, o)

I got the exception:

An unhandled exception of type 'System.ArgumentException' occurred
in mscorlib.dll

Additional information: The structure must not be a value class.

I got rid of the exception by turning Option Strict Off and using:

o = Marshal.PtrToStructure(m.LParam, GetType(DEV_BROADCAST_HDR))

Same thing for Marshal.PtrToStructure(msg.LParam, b)

Is there a way to accomplish this with Option Strict On?

***

And the dbcc in your example:

Dim strsize As Integer = (o.dbcc_size - 28) / 2

should be dbch:

Dim strsize As Integer = (o.dbch_size - 28) / 2

***

Jan Axelson
www.Lvr.com

On Tue, 10 Feb 2004 06:06:45 GMT, v-******@online.microsoft.com (Peter
Huang) wrote:
Hi Jan,

Thanks for posting in the community.

Based on my research, since the dbcc_name is a variable length member. I
think you may need to declare it as a char array.
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=128)> _
Public dbcc_name() As Char

You may set the SizeConst long enough to get the string.
Here is my test sample ,you may have a try.
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Text
Public Class Form1
Inherits System.Windows.Forms.Form

Public Class Win32
Public Const WM_DEVICECHANGE = &H219
Public Const DBT_DEVICEARRIVAL = &H8000
Public Const DBT_DEVICEREMOVECOMPLETE = &H8004
Public Const DEVICE_NOTIFY_WINDOW_HANDLE = 0
Public Const DEVICE_NOTIFY_SERVICE_HANDLE = 1
Public Const DBT_DEVTYP_DEVICEINTERFACE = 5
Public Shared GUID_IO_MEDIA_ARRIVAL As Guid = New
Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED")

<StructLayout(LayoutKind.Sequential)> _
Public Class DEV_BROADCAST_DEVICEINTERFACE
Public dbcc_size As Integer
Public dbcc_devicetype As Integer
Public dbcc_reserved As Integer
Public dbcc_classguid As Guid
Public dbcc_name As Short
End Class
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Public Class DEV_BROADCAST_DEVICEINTERFACE1
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
<StructLayout(LayoutKind.Sequential)> _
Public Class DEV_BROADCAST_HDR
Public dbcc_size As Integer
Public dbcc_devicetype As Integer
Public dbcc_reserved As Integer
End Class

<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("kernel32.DLL")> _
Public Shared Function _
GetLastError() As Integer
End Function
End Class

Public Sub New()
MyBase.New()
InitializeComponent()
RegisterHidNotification()
End Sub

Public Sub RegisterHidNotification()
Dim dbi As Win32.DEV_BROADCAST_DEVICEINTERFACE = New
Win32.DEV_BROADCAST_DEVICEINTERFACE
Dim size As Integer
size = Marshal.SizeOf(dbi)
Dim gd As Guid
' MsgBox(Marshal.SizeOf(gd))
' MsgBox(Marshal.SizeOf(New
Win32.DEV_BROADCAST_DEVICEINTERFACE1))
dbi.dbcc_size = size
dbi.dbcc_devicetype = Win32.DBT_DEVTYP_DEVICEINTERFACE
dbi.dbcc_reserved = 0
dbi.dbcc_classguid = Win32.GUID_IO_MEDIA_ARRIVAL
Dim Buffer As IntPtr
Buffer = Marshal.AllocHGlobal(size)
Marshal.StructureToPtr(dbi, Buffer, True)
Dim r As IntPtr
r = Win32.RegisterDeviceNotification(Handle, Buffer,
Win32.DEVICE_NOTIFY_WINDOW_HANDLE)
Marshal.PtrToStructure(Buffer, dbi)
If r.ToInt32 = IntPtr.Zero.ToInt32 Then
MessageBox.Show(Win32.GetLastError().ToString())
End If
End Sub

Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = Win32.WM_DEVICECHANGE Then
OnDeviceChange(m)
End If
MyBase.WndProc(m)
End Sub

Private Sub OnDeviceChange(ByVal msg As Message)
Dim wParam As Integer
wParam = msg.WParam.ToInt32()
If wParam = Win32.DBT_DEVICEARRIVAL Then
Dim o As New Win32.DEV_BROADCAST_HDR
Dim b As New Win32.DEV_BROADCAST_DEVICEINTERFACE1
Dim gd As Guid
Marshal.PtrToStructure(msg.LParam, o)
If (o.dbcc_devicetype = Win32.DBT_DEVTYP_DEVICEINTERFACE) Then
Dim strsize As Integer = (o.dbcc_size - 28) / 2
ReDim b.dbcc_name(strsize)
Marshal.PtrToStructure(msg.LParam, b)
MsgBox(New Guid(b.dbcc_classguid).ToString)
Dim str As New String(b.dbcc_name, 0, strsize)
MsgBox(str)
End If
MessageBox.Show("Arrival")
ElseIf wParam = Win32.DBT_DEVICEREMOVECOMPLETE Then
MessageBox.Show("Remove")
End If
End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container
Me.Text = "Form1"
Dim t As New Guid("d07433c0-a98e-11d2-917a-00a0c9068ff3")
End Sub
End Class

If you have any concern on this issue please post here.
Best regards,

Peter Huang
Microsoft Online Partner Support


Nov 20 '05 #4

P: n/a
Hi Jan,

Thanks for your quickly reply!

Since the code runs well on my machine, can you try to create a new winform
application and copy and paste my code for test, to see if it has any
difference with yours.

Comments in lines.

If you have any concern on this issue,please post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: jan Axelson <ja*@lvr.com>
Newsgroups: microsoft.public.dotnet.languages.vb
Subject: Re: Retrieving dbcc_name string from a DEV_BROADCAST_DEVICEINTERFACE structureDate: Tue, 10 Feb 2004 20:35:01 -0600
Organization: Posted via Supernews, http://www.supernews.com
Message-ID: <a3********************************@4ax.com>
References: <so**************@cpmsftngxa07.phx.gbl> <Ol**************@TK2MSFTNGP11.phx.gbl>
<7J**************@cpmsftngxa07.phx.gbl>X-Newsreader: Forte Agent 1.7/32.534
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Complaints-To: ab***@supernews.com
Lines: 228
Path: cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTN GP08.phx.gbl!newsfeed00.su
l.t-online.de!t-online.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn
-xit-03!sn-xit-04!sn-xit-01!sn-post-01!supernews.com!corp.supernews.com!not-
for-mailXref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.languages.vb:180311
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Thanks for responding. I've made some progress but still don't have
the dbcc_name string in a String variable.

strsize = 83, which equals the number of characters in the device name
and is thus correct.

But this:

Dim str As New String(b.dbcc_name, 0, strsize)

results in str = "/", only the first character.

This code:

Dim count As Integer
For count = 0 To (strsize - 1) * 2
lstResults.Items.Add(o.dbcc_name(count))
Next count

displays all of the characters in the device name, with each character
followed by an extra null character. So the string is there, but in an
incorrect format.

dbcc_name is declared as a Char array, as in your example.

How can I get the complete and correct string into str?

***
I think the return string is correct, since the return string is encoded in
Unicode, in which way every char will occupy two byte, i.e. a english
character will be encoded as "5c 00" which means '\'. For a variable length
string in a structure, I think we may need to declare it as char array to
do the marshal ourself.

A couple of other things:

At this line:

Marshal.PtrToStructure(msg.LParam, o)

I got the exception:

An unhandled exception of type 'System.ArgumentException' occurred
in mscorlib.dll

Additional information: The structure must not be a value class.

I got rid of the exception by turning Option Strict Off and using:

o = Marshal.PtrToStructure(m.LParam, GetType(DEV_BROADCAST_HDR))

Same thing for Marshal.PtrToStructure(msg.LParam, b)

Is there a way to accomplish this with Option Strict On?

***
It is somewhat strange that the
Marshal.PtrToStructure(msg.LParam, o)
doesn't work for you.

And the dbcc in your example:

Dim strsize As Integer = (o.dbcc_size - 28) / 2

should be dbch:

Dim strsize As Integer = (o.dbch_size - 28) / 2

***

The o is a DEV_BROADCAST_HDR instance.
I defined it as below.
Public Class DEV_BROADCAST_HDR
Public dbcc_size As Integer
Public dbcc_devicetype As Integer
Public dbcc_reserved As Integer
End Class

so I use the o.dbcc_size, I am sorry if any confusion.
Jan Axelson
www.Lvr.com


Nov 20 '05 #5

P: n/a
It's working now. I missed the:

CharSet:=CharSet.Unicode

in the declaration for:

Public Class DEV_BROADCAST_DEVICEINTERFACE1

Thanks a million.

Jan Axelson
www.Lvr.com
Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.