PC Review


Reply
Thread Tools Rate Thread

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

 
 
Daniel Diehl
Guest
Posts: n/a
 
      14th Dec 2003
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_DEVICECHANGE: OnDeviceChange(ref m); break;
}

base.WndProc (ref m);
}

void OnDeviceChange(ref Message msg)
{
int wParam = (int)msg.WParam;



if(wParam == Win32.DBT_DEVICEARRIVAL)
{
statusBar.Text = "Hardware attached";
frmLockScreen.Close();
}
else if(wParam == Win32.DBT_DEVICEREMOVECOMPLETE)
{
statusBar.Text = "Hardware entfernt";

frmLockScreen.Show();
}
}


void RegisterHidNotification()
{
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_HID;
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)
Console.WriteLine(Win32.GetLastError().ToString());
}

class Win32
{
public const int
WM_DEVICECHANGE = 0x0219;
public const int
DBT_DEVICEARRIVAL = 0x8000,
DBT_DEVICEREMOVECOMPLETE = 0x8004;
public const int
DEVICE_NOTIFY_WINDOW_HANDLE = 0,
DEVICE_NOTIFY_SERVICE_HANDLE = 1;
public const int
DBT_DEVTYP_DEVICEINTERFACE = 5;
public static Guid
GUID_DEVINTERFACE_HID = new
Guid("4D1E55B2-F16F-11CF-88CB-001111000030");

[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;
}

[DllImport("user32.dll", SetLastError=true)]
public static extern IntPtr RegisterDeviceNotification(
IntPtr hRecipient,
IntPtr NotificationFilter,
Int32 Flags);

[DllImport("kernel32.dll")]
public static extern int GetLastError();
}


Thanks for any hints,
daniel
 
Reply With Quote
 
 
 
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      18th Dec 2003
Much better is to use the System.Management classes and register event
listeners for Win32_Diskdrive class operations.

Here's a small console sample.

// This code demonstrates how to monitor the UsbControllerDevice for
// the arrival of creation/operation events
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Management;
class WMIEvent {
public static void Main() {
WMIEvent we = new WMIEvent();
ManagementEventWatcher w= null;
WqlEventQuery q;
ManagementOperationObserver observer = new ManagementOperationObserver();
// Bind to local machine
ManagementScope scope = new ManagementScope("root\\CIMV2");
scope.Options.EnablePrivileges = true; //set required privilege
try {
q = new WqlEventQuery();
q.EventClassName = "__InstanceOperationEvent";
q.WithinInterval = new TimeSpan(0,0,3);
q.Condition = @"TargetInstance ISA 'Win32_DiskDrive' ";
w = new ManagementEventWatcher(scope, q);

w.EventArrived += new EventArrivedEventHandler(we.DiskEventArrived);
w.Start();
Console.ReadLine(); // block main thread for test purposes
}
catch(Exception e) {
Console.WriteLine(e.Message);
}
finally {
w.Stop();
}
}
public void DiskEventArrived(object sender, EventArrivedEventArgs e) {
//Get the Event object and display its properties (all)
foreach(PropertyData pd in e.NewEvent.Properties) {
ManagementBaseObject mbo = null;
if(( mbo = pd.Value as ManagementBaseObject) != null) {
Console.WriteLine("--------------Properties------------------");
foreach(PropertyData prop in mbo.Properties)
Console.WriteLine("{0} - {1}", prop.Name, prop.Value);
}
}
}

}


Willy.

"Daniel Diehl" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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_DEVICECHANGE: OnDeviceChange(ref m); break;
> }
>
> base.WndProc (ref m);
> }
>
> void OnDeviceChange(ref Message msg)
> {
> int wParam = (int)msg.WParam;
>
>
>
> if(wParam == Win32.DBT_DEVICEARRIVAL)
> {
> statusBar.Text = "Hardware attached";
> frmLockScreen.Close();
> }
> else if(wParam == Win32.DBT_DEVICEREMOVECOMPLETE)
> {
> statusBar.Text = "Hardware entfernt";
>
> frmLockScreen.Show();
> }
> }
>
>
> void RegisterHidNotification()
> {
> 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_HID;
> 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)
> Console.WriteLine(Win32.GetLastError().ToString());
> }
>
> class Win32
> {
> public const int
> WM_DEVICECHANGE = 0x0219;
> public const int
> DBT_DEVICEARRIVAL = 0x8000,
> DBT_DEVICEREMOVECOMPLETE = 0x8004;
> public const int
> DEVICE_NOTIFY_WINDOW_HANDLE = 0,
> DEVICE_NOTIFY_SERVICE_HANDLE = 1;
> public const int
> DBT_DEVTYP_DEVICEINTERFACE = 5;
> public static Guid
> GUID_DEVINTERFACE_HID = new
> Guid("4D1E55B2-F16F-11CF-88CB-001111000030");
>
> [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;
> }
>
> [DllImport("user32.dll", SetLastError=true)]
> public static extern IntPtr RegisterDeviceNotification(
> IntPtr hRecipient,
> IntPtr NotificationFilter,
> Int32 Flags);
>
> [DllImport("kernel32.dll")]
> public static extern int GetLastError();
> }
>
>
> Thanks for any hints,
> daniel



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Detecting Plug in =?Utf-8?B?U2hhcmtiYWl0?= Microsoft ASP .NET 1 11th Oct 2004 08:49 PM
where and what is plug in manager and plug in interface? =?Utf-8?B?bWFoazQ3?= Windows XP Help 0 29th Apr 2004 11:16 AM
Plug and Play not detecting USB Dou Njie Windows XP Hardware 1 20th Dec 2003 07:00 PM
USB Stick Plug in and Plug out (USB Harddrive) Daniel Diehl Microsoft C# .NET 1 18th Dec 2003 09:41 PM
Detecting plug'n'play Georges Berenger Windows XP Embedded 2 10th Sep 2003 10:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:48 AM.