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 Marshal.PtrToStructure()
call.
Thanks all for any help.
Hopefully this VB->VC translation can help someone out there as well
Jeff
Visual Basic Code:
==================
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
Visual CSharp Equivilent
========================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
RegisterDeviceNotification();
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
[STAThread] static void Main()
{
Application.Run(new Form1());
}
void RegisterDeviceNotification()
{
Win32.DEV_BROADCAST_DEVICEINTERFACE dbi = new
Win32.DEV_BROADCAST_DEVICEINTERFACE();
int size = Marshal.SizeOf(dbi);
dbi.dbcc_size = size;
dbi.dbcc_devicetype = Win32.DBT_DEVTYP_DEVICEINTERFACE;
dbi.dbcc_reserved = 0;
dbi.dbcc_classguid = Win32.GUID_DEVINTERFACE_USB_DEVICE;
dbi.dbcc_name = 0;
IntPtr buffer = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(dbi, buffer, true);
IntPtr r = Win32.RegisterDeviceNotification(Handle, buffer,
Win32.DEVICE_NOTIFY_WINDOW_HANDLE);
if(r == IntPtr.Zero)
MessageBox.Show(Win32.GetLastError().ToString());
}
protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case Win32.WM_DEVICECHANGE: OnDeviceChange(ref m); break;
}
base.WndProc (ref m);
}
void OnDeviceChange(ref Message msg)
{
int wParam = (int)msg.WParam;
if((wParam == Win32.DBT_DEVICEARRIVAL) || (wParam ==
Win32.DBT_DEVICEREMOVECOMPLETE))
{
// Read the dhdr.dbcc_devicetype - The Following code could also
be used
////////////////////////////////////////////////////////////////////////
//Win32.DEV_BROADCAST_HDR dhdr;
//dhdr = (Win32.DEV_BROADCAST_HDR)
Marshal.PtrToStructure(msg.LParam,typeof(Win32.DEV _BROADCAST_HDR));
//if (dhdr.dbcc_devicetype == Win32.DBT_DEVTYP_DEVICEINTERFACE)
int dbccSize = Marshal.ReadInt32(msg.LParam,0);
int devType = Marshal.ReadInt32(msg.LParam,4);
if(devType == Win32.DBT_DEVTYP_DEVICEINTERFACE)
{
Win32.DEV_BROADCAST_DEVICEINTERFACE1 dip;
dip = (Win32.DEV_BROADCAST_DEVICEINTERFACE1)
Marshal.PtrToStructure(msg.LParam,typeof(Win32.DEV _BROADCAST_DEVICEINTERFACE1));
string csTemp = new string(dip.dbcc_name);
MessageBox.Show(csTemp + " arrived/removed");
}
}
}
}
public class Win32
{
public const int WM_DEVICECHANGE = 0x0219;
public const int DBT_DEVICEARRIVAL = 0x8000, // system
detected a new device
DBT_DEVICEREMOVECOMPLETE = 0x8004; // device is gone
public const int DEVICE_NOTIFY_WINDOW_HANDLE = 0,
DEVICE_NOTIFY_SERVICE_HANDLE = 1;
public const int DBT_DEVTYP_DEVICEINTERFACE = 0x00000005; // device
interface class
public static Guid GUID_DEVINTERFACE_USB_DEVICE = new
Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED");
[StructLayout(LayoutKind.Sequential)] public class DEV_BROADCAST_HDR
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
}
[StructLayout(LayoutKind.Sequential)] public class
DEV_BROADCAST_DEVICEINTERFACE
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
public Guid dbcc_classguid;
public short dbcc_name;
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public class DEV_BROADCAST_DEVICEINTERFACE1
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType=UnmanagedType.U1,
SizeConst=16)] public byte [] dbcc_classguid;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=128)] public char []
dbcc_name;
}
[DllImport("user32.dll", SetLastError=true)] public static extern
IntPtr RegisterDeviceNotification( IntPtr hRecipient, IntPtr
NotificationFilter, Int32 Flags);
[DllImport("kernel32.dll")] public static extern int GetLastError();
}
}