Detecting USB Stick Plug in and Plug out (USB Harddrive)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Diehl

    #1

    Detecting USB Stick Plug in and Plug out (USB Harddrive)

    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
  • Willy Denoyette [MVP]

    #2
    Re: Detecting USB Stick Plug in and Plug out (USB Harddrive)

    Much better is to use the System.Manageme nt classes and register event
    listeners for Win32_Diskdrive class operations.

    Here's a small console sample.

    // This code demonstrates how to monitor the UsbControllerDe vice for
    // the arrival of creation/operation events
    using System;
    using System.Componen tModel;
    using System.Runtime. InteropServices ;
    using System.Manageme nt;
    class WMIEvent {
    public static void Main() {
    WMIEvent we = new WMIEvent();
    ManagementEvent Watcher w= null;
    WqlEventQuery q;
    ManagementOpera tionObserver observer = new ManagementOpera tionObserver();
    // Bind to local machine
    ManagementScope scope = new ManagementScope ("root\\CIMV2") ;
    scope.Options.E nablePrivileges = true; //set required privilege
    try {
    q = new WqlEventQuery() ;
    q.EventClassNam e = "__InstanceOper ationEvent";
    q.WithinInterva l = new TimeSpan(0,0,3) ;
    q.Condition = @"TargetInstanc e ISA 'Win32_DiskDriv e' ";
    w = new ManagementEvent Watcher(scope, q);

    w.EventArrived += new EventArrivedEve ntHandler(we.Di skEventArrived) ;
    w.Start();
    Console.ReadLin e(); // block main thread for test purposes
    }
    catch(Exception e) {
    Console.WriteLi ne(e.Message);
    }
    finally {
    w.Stop();
    }
    }
    public void DiskEventArrive d(object sender, EventArrivedEve ntArgs e) {
    //Get the Event object and display its properties (all)
    foreach(Propert yData pd in e.NewEvent.Prop erties) {
    ManagementBaseO bject mbo = null;
    if(( mbo = pd.Value as ManagementBaseO bject) != null) {
    Console.WriteLi ne("--------------Properties------------------");
    foreach(Propert yData prop in mbo.Properties)
    Console.WriteLi ne("{0} - {1}", prop.Name, prop.Value);
    }
    }
    }

    }


    Willy.

    "Daniel Diehl" <daniel@mediawo rker.net> wrote in message
    news:33f45170.0 312132247.43f19 a42@posting.goo gle.com...[color=blue]
    > 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[/color]


    Comment

    Working...