473,837 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retrieving dbcc_name string from a DEV_BROADCAST_D EVICEINTERFACE structure

My application is using RegisterDeviceN otification() to detect
attachment and removal of a USB HID-class device.

The form is receiving WM_DEVICECHANGE messages with wParam set to
DBT_DEVICEARRIV AL or DBT_DEVICEREMOV ECOMPLETE.

I want to identify the device that has arrived or been removed by
examining the dbcc_name member of the DEV_BROADCAST_D EVICEINTERFACE
structure.

This is my declaration for DEV_BROADCAST_D EVICEINTERFACE:

<StructLayout(L ayoutKind.Seque ntial)> _
Public Structure DEV_BROADCAST_D EVICEINTERFACE
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 RegisterDeviceN otification:

Dim dbi As New DEV_BROADCAST_D EVICEINTERFACE
Dim size As Integer
size = Marshal.SizeOf( dbi)
dbi.dbcc_size = size
dbi.dbcc_device type = DBT_DEVTYP_DEVI CEINTERFACE
dbi.dbcc_reserv ed = 0
dbi.dbcc_classg uid = HidGuid 'obtained with HidD_GetHidGuid ()
dbi.dbcc_name = 0
Dim buffer As IntPtr = Marshal.AllocHG lobal(size)
Marshal.Structu reToPtr(dbi, Buffer, True)
Dim r As IntPtr
r = RegisterDeviceN otification(frm My.Handle, buffer,
DEVICE_NOTIFY_W INDOW_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_H DR

Select Case m.Msg

'Look for a WM_DEVICECHANGE message.
Case WM_DEVICECHANGE

lstResults.Item s.Add("WM_DEVIC ECHANGE")

If (m.WParam.ToInt 32 = DBT_DEVICEARRIV AL) Then
lstResults.Item s.Add("DBT_DEVI CEARRIVAL")

'LParam is a pointer to a structure that begins with a
DEV_BROADCAST_H DR structure.

broadcastHeader = _
CType(m.GetLPar am(broadcastHea der.GetType),
DEV_BROADCAST_H DR)

'Is it a device interface?
If (broadcastHeade r.dbch_devicety pe =
DBT_DEVTYP_DEVI CEINTERFACE) Then

'LParam is a pointer to a DEV_BROADCAST_D EVICEINTERFACE
structure.
broadcastDevice Interface = _
CType(m.GetLPar am(broadcastDev iceInterface.Ge tType),
DEV_BROADCAST_D EVICEINTERFACE)

lstResults.Item s.Add("size = " &
CStr(broadcastD eviceInterface. dbcc_size))

The dbcc_size parameter returns 194, but I've been unsuccessful at
retrieving broadcastDevice Interface.dbcc_ name. I've also tried
declaring dbcc_name in DEV_BROADCAST_D EVICEINTERFACE as:

<MarshalAs(Unma nagedType.LPTSt r)> Dim dbcc_name As String
and:
Dim dbcc_name As String
and:
Dim dbcc_name as IntPtr
and using Marshal.PtrToSt ringAuto

Any suggestions welcome.

Jan Axelson
www.Lvr.com
Nov 20 '05 #1
5 7744
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_n ame, 0, strsize)

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

This code:

Dim count As Integer
For count = 0 To (strsize - 1) * 2
lstResults.Item s.Add(o.dbcc_na me(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.PtrToSt ructure(msg.LPa ram, o)

I got the exception:

An unhandled exception of type 'System.Argumen tException' 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.PtrToSt ructure(m.LPara m, GetType(DEV_BRO ADCAST_HDR))

Same thing for Marshal.PtrToSt ructure(msg.LPa ram, 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.m icrosoft.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(Unma nagedType.ByVal Array, 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_DEVICEARRIV AL = &H8000
Public Const DBT_DEVICEREMOV ECOMPLETE = &H8004
Public Const DEVICE_NOTIFY_W INDOW_HANDLE = 0
Public Const DEVICE_NOTIFY_S ERVICE_HANDLE = 1
Public Const DBT_DEVTYP_DEVI CEINTERFACE = 5
Public Shared GUID_IO_MEDIA_A RRIVAL As Guid = New
Guid("A5DCBF 10-6530-11D2-901F-00C04FB951ED")

<StructLayout(L ayoutKind.Seque ntial)> _
Public Class DEV_BROADCAST_D EVICEINTERFACE
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(L ayoutKind.Seque ntial, CharSet:=CharSe t.Unicode)> _
Public Class DEV_BROADCAST_D EVICEINTERFACE1
Public dbcc_size As Integer
Public dbcc_devicetype As Integer
Public dbcc_reserved As Integer
<MarshalAs(Unma nagedType.ByVal Array,
ArraySubType:= UnmanagedType.U 1, SizeConst:=16)> _
Public dbcc_classguid( ) As Byte
<MarshalAs(Unma nagedType.ByVal Array, SizeConst:=128) > _
Public dbcc_name() As Char
End Class
<StructLayout(L ayoutKind.Seque ntial)> _
Public Class DEV_BROADCAST_H DR
Public dbcc_size As Integer
Public dbcc_devicetype As Integer
Public dbcc_reserved As Integer
End Class

<DllImport("use r32.DLL", SetLastError:=T rue)> _
Public Shared Function _
RegisterDeviceN otification(ByV al IntPtr As IntPtr, ByVal
NotificationFi lter As IntPtr, ByVal Flags As Int32) As IntPtr
End Function

<DllImport("ker nel32.DLL")> _
Public Shared Function _
GetLastError() As Integer
End Function
End Class

Public Sub New()
MyBase.New()
InitializeCompo nent()
RegisterHidNoti fication()
End Sub

Public Sub RegisterHidNoti fication()
Dim dbi As Win32.DEV_BROAD CAST_DEVICEINTE RFACE = New
Win32.DEV_BROA DCAST_DEVICEINT ERFACE
Dim size As Integer
size = Marshal.SizeOf( dbi)
Dim gd As Guid
' MsgBox(Marshal. SizeOf(gd))
' MsgBox(Marshal. SizeOf(New
Win32.DEV_BROA DCAST_DEVICEINT ERFACE1))
dbi.dbcc_size = size
dbi.dbcc_device type = Win32.DBT_DEVTY P_DEVICEINTERFA CE
dbi.dbcc_reserv ed = 0
dbi.dbcc_classg uid = Win32.GUID_IO_M EDIA_ARRIVAL
Dim Buffer As IntPtr
Buffer = Marshal.AllocHG lobal(size)
Marshal.Structu reToPtr(dbi, Buffer, True)
Dim r As IntPtr
r = Win32.RegisterD eviceNotificati on(Handle, Buffer,
Win32.DEVICE_N OTIFY_WINDOW_HA NDLE)
Marshal.PtrToSt ructure(Buffer, dbi)
If r.ToInt32 = IntPtr.Zero.ToI nt32 Then
MessageBox.Show (Win32.GetLastE rror().ToString ())
End If
End Sub

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

Private Sub OnDeviceChange( ByVal msg As Message)
Dim wParam As Integer
wParam = msg.WParam.ToIn t32()
If wParam = Win32.DBT_DEVIC EARRIVAL Then
Dim o As New Win32.DEV_BROAD CAST_HDR
Dim b As New Win32.DEV_BROAD CAST_DEVICEINTE RFACE1
Dim gd As Guid
Marshal.PtrToSt ructure(msg.LPa ram, o)
If (o.dbcc_devicet ype = Win32.DBT_DEVTY P_DEVICEINTERFA CE) Then
Dim strsize As Integer = (o.dbcc_size - 28) / 2
ReDim b.dbcc_name(str size)
Marshal.PtrToSt ructure(msg.LPa ram, b)
MsgBox(New Guid(b.dbcc_cla ssguid).ToStrin g)
Dim str As New String(b.dbcc_n ame, 0, strsize)
MsgBox(str)
End If
MessageBox.Show ("Arrival")
ElseIf wParam = Win32.DBT_DEVIC EREMOVECOMPLETE 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.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er

'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.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeComp onent()
components = New System.Componen tModel.Containe r
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.publi c.dotnet.langua ges.vb
Subject: Re: Retrieving dbcc_name string from a DEV_BROADCAST_D EVICEINTERFACE 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.ph x.gbl!cpmsftngx a06.phx.gbl!TK2 MSFTNGP08.phx.g bl!newsfeed00.s u
l.t-online.de!t-online.de!news-spur1.maxwell.s yr.edu!news.max well.syr.edu!sn
-xit-03!sn-xit-04!sn-xit-01!sn-post-01!supernews.co m!corp.supernew s.com!not-
for-mailXref: cpmsftngxa07.ph x.gbl microsoft.publi c.dotnet.langua ges.vb:180311
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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_n ame, 0, strsize)

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

This code:

Dim count As Integer
For count = 0 To (strsize - 1) * 2
lstResults.Item s.Add(o.dbcc_na me(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.PtrToS tructure(msg.LP aram, o)

I got the exception:

An unhandled exception of type 'System.Argumen tException' 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.PtrToSt ructure(m.LPara m, GetType(DEV_BRO ADCAST_HDR))

Same thing for Marshal.PtrToSt ructure(msg.LPa ram, b)

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

***
It is somewhat strange that the
Marshal.PtrToSt ructure(msg.LPa ram, 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_H DR instance.
I defined it as below.
Public Class DEV_BROADCAST_H DR
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:=CharSe t.Unicode

in the declaration for:

Public Class DEV_BROADCAST_D EVICEINTERFACE1

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
3215
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 directory structure. A number of sounds have the same name, but are in a different directory (for example, our game has 5 races. Each race has the same set of sounds- button clicks, etc, but the actual sound is different).
1
11265
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 not know how to translate the ReDim that occurs there into C#. Or wither this will actually give me what I'm looking for. The code I'm posting seems to work although I don't know how to set the length of the name correctly in the...
2
1118
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 listbox. Here's my structure: Public Structure ItemData Dim Descriptiong As String Dim Cost As Single Dim Quantity As Integer Dim Frequency As Single
0
4237
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( The x and y properties of my POINT structure are always zero. It seems the structure isn't getting updated at all from the SendMessage function and so the code just displays the default values.
8
8500
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 for a specific folder on the pen drive I keep receiving an error message that reads: An unhandled exception of type 'System.ExecutionEngineException'
5
2660
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 simply working with objects in memory. So, it appears as though Microsoft requires that our datamapper classes reside inside a folder called "App_Code", and NO WHERE ELSE. So, has anyone successfully been able to place their datamappers in a...
1
1685
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 converted to a string. the problem now is that i have to retrieve certain values from that xml string in the request field and save the in some other databse please assist
3
2727
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
8757
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. Supposedly the window handle for an entry is in the dwData structure in the TBBUTTON structure but I'm having a hard time retrieving the handle from the structure correctly. It seems when I look at the data I've retrieved, it is not a handle (negative...
0
10871
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10564
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10621
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10268
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7806
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5668
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5846
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3123
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.