Hi,
I'm looking for a solution to detect if a USB Sticker (USB Drive) ist
plugged in or plugged out. Currently Im able to detect Hardware Plugin
and Plugout but don't get any information what type of device it is.
Is there a way to get this information ?
Currenty my solution work in that way (Also from this group):
protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case Win32.WM_DEVICE CHANGE: OnDeviceChange( ref m); break;
}
base.WndProc (ref m);
}
void OnDeviceChange( ref Message msg)
{
int wParam = (int)msg.WParam ;
if(wParam == Win32.DBT_DEVIC EARRIVAL)
{
statusBar.Text = "Hardware attached";
frmLockScreen.C lose();
}
else if(wParam == Win32.DBT_DEVIC EREMOVECOMPLETE )
{
statusBar.Text = "Hardware entfernt";
frmLockScreen.S how();
}
}
void RegisterHidNoti fication()
{
Win32.DEV_BROAD CAST_DEVICEINTE RFACE dbi = new
Win32.DEV_BROAD CAST_DEVICEINTE RFACE();
int size = Marshal.SizeOf( dbi);
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_DEVI NTERFACE_HID;
dbi.dbcc_name = 0;
IntPtr buffer = Marshal.AllocHG lobal(size);
Marshal.Structu reToPtr(dbi, buffer, true);
IntPtr r = Win32.RegisterD eviceNotificati on(Handle, buffer,
Win32.DEVICE_NO TIFY_WINDOW_HAN DLE);
if(r == IntPtr.Zero)
Console.WriteLi ne(Win32.GetLas tError().ToStri ng());
}
class Win32
{
public const int
WM_DEVICECHANGE = 0x0219;
public const int
DBT_DEVICEARRIV AL = 0x8000,
DBT_DEVICEREMOV ECOMPLETE = 0x8004;
public const int
DEVICE_NOTIFY_W INDOW_HANDLE = 0,
DEVICE_NOTIFY_S ERVICE_HANDLE = 1;
public const int
DBT_DEVTYP_DEVI CEINTERFACE = 5;
public static Guid
GUID_DEVINTERFA CE_HID = new
Guid("4D1E55B2-F16F-11CF-88CB-001111000030");
[StructLayout(La youtKind.Sequen tial)]
public class DEV_BROADCAST_D EVICEINTERFACE
{
public int dbcc_size;
public int dbcc_devicetype ;
public int dbcc_reserved;
public Guid dbcc_classguid;
public short dbcc_name;
}
[DllImport("user 32.dll", SetLastError=tr ue)]
public static extern IntPtr RegisterDeviceN otification(
IntPtr hRecipient,
IntPtr NotificationFil ter,
Int32 Flags);
[DllImport("kern el32.dll")]
public static extern int GetLastError();
}
Thanks for any hints,
daniel