473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Retrieving dbcc_name string from a DEV_BROADCAST_DEVICEINTERFACE structure

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
5 7696
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
>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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: BlackHawke | last post by:
Hello! In our game package (www.andromedaonline.net) we are having problems with sounds. We have placed them in a jar file, and are trying to access them from there. The jar file has a...
1
by: Jeffrey B. Holtz | last post by:
I'm trying to get the Name of the USB device pluged in from the RegisterDeviceNotification that I've used P/Invoke to marshal. I have seen a similar posting on the VisualBasic newgroups but I do...
2
by: Jay Banks | last post by:
I'm working on a project that lists a few hundred items in a checked listbox. The listbox entries are all backed up by a structure, and only the descriptiong of the structure is shown in the...
0
by: AvecFromage | last post by:
Hi, I'd like to be able to read the positions of all the Icons on my WinXP desktop. I've had a look around to see how to do it and I've come up with the code below...but it doesn't work. :o( ...
8
by: Chad | last post by:
To anyone who is smarter than I am when it comes to WMI: Here is what I am trying to do: 1) Detect a USB pen drive when it is inserted 2) Retrieve the drive letter of the pen drive 3) Check...
5
by: Randy Smith | last post by:
Hi ALL, I wonder if anyone has been using n-tier to bind to a GridView control by using the ObjectDataSource. This is our first OOP web application, and we have no tables. Right now we are...
1
by: lmwasisebe | last post by:
hie guys i having problems retrieving xml values, this is the structure of the problem: I have a database with the following fields, 1)id 2)state 3)request. the request field contains a xml file...
3
by: =?Utf-8?B?UHJpeWE=?= | last post by:
Hi, Could someone tell me how to retrieve a drive's drive letter from user mode given its device name? Any information is greatly appreciated. Thanks, Priya
3
by: BlackShadow33p1 | last post by:
I'm trying to write a program in C++ that gets the handles of all the visible entries in the windows taskbar. The method I've used so far is to send the TB_GETBUTTON message to the taskbar. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.