Connecting Tech Pros Worldwide Forums | Help | Site Map

How to detect a USB Device Being plugged-in or unplugged?

jack
Guest
 
Posts: n/a
#1: Jul 21 '05
Hello,

I'm trying to make a program what can let me know when a USB device is added
or removed from a system. If anyone has any ideas or help that would be
great!

Thanks in advnace,

Jack



Greg Ewing [MVP]
Guest
 
Posts: n/a
#2: Jul 21 '05

re: How to detect a USB Device Being plugged-in or unplugged?


Jack, you are going to have to use P/Invoke and RegisterDeviceNotification.
Here's a link to some code by Wei Mao at MS.

http://www.dotnet247.com/247referenc...32/164968.aspx

--
Greg Ewing [MVP]
http://www.citidc.com

"jack" <jack@mrolinux.com> wrote in message
news:eMj80xMeDHA.2472@TK2MSFTNGP11.phx.gbl...[color=blue]
> Hello,
>
> I'm trying to make a program what can let me know when a USB device is[/color]
added[color=blue]
> or removed from a system. If anyone has any ideas or help that would be
> great!
>
> Thanks in advnace,
>
> Jack
>
>[/color]


jack
Guest
 
Posts: n/a
#3: Jul 21 '05

re: How to detect a USB Device Being plugged-in or unplugged?


Hello,

Great, any example in VB.NET?

Thanks,

Jack
"Greg Ewing [MVP]" <gewing@_NO_SPAM_gewing.com> wrote in message
news:%231cQjkNeDHA.1732@TK2MSFTNGP12.phx.gbl...[color=blue]
> Jack, you are going to have to use P/Invoke and[/color]
RegisterDeviceNotification.[color=blue]
> Here's a link to some code by Wei Mao at MS.
>
> http://www.dotnet247.com/247referenc...32/164968.aspx
>
> --
> Greg Ewing [MVP]
> http://www.citidc.com
>
> "jack" <jack@mrolinux.com> wrote in message
> news:eMj80xMeDHA.2472@TK2MSFTNGP11.phx.gbl...[color=green]
> > Hello,
> >
> > I'm trying to make a program what can let me know when a USB device is[/color]
> added[color=green]
> > or removed from a system. If anyone has any ideas or help that would be
> > great!
> >
> > Thanks in advnace,
> >
> > Jack
> >
> >[/color]
>
>[/color]


William Ryan
Guest
 
Posts: n/a
#4: Jul 21 '05

re: How to detect a USB Device Being plugged-in or unplugged?


YOU ARE THE MAN!
"Greg Ewing [MVP]" <gewing@_NO_SPAM_gewing.com> wrote in message
news:#1cQjkNeDHA.1732@TK2MSFTNGP12.phx.gbl...[color=blue]
> Jack, you are going to have to use P/Invoke and[/color]
RegisterDeviceNotification.[color=blue]
> Here's a link to some code by Wei Mao at MS.
>
> http://www.dotnet247.com/247referenc...32/164968.aspx
>
> --
> Greg Ewing [MVP]
> http://www.citidc.com
>
> "jack" <jack@mrolinux.com> wrote in message
> news:eMj80xMeDHA.2472@TK2MSFTNGP11.phx.gbl...[color=green]
> > Hello,
> >
> > I'm trying to make a program what can let me know when a USB device is[/color]
> added[color=green]
> > or removed from a system. If anyone has any ideas or help that would be
> > great!
> >
> > Thanks in advnace,
> >
> > Jack
> >
> >[/color]
>
>[/color]


Willy Denoyette [MVP]
Guest
 
Posts: n/a
#5: Jul 21 '05

re: How to detect a USB Device Being plugged-in or unplugged?



"jack" <jack@mrolinux.com> wrote in message news:eMj80xMeDHA.2472@TK2MSFTNGP11.phx.gbl...[color=blue]
> Hello,
>
> I'm trying to make a program what can let me know when a USB device is added
> or removed from a system. If anyone has any ideas or help that would be
> great!
>
> Thanks in advnace,
>
> Jack
>
>[/color]

Here is a sample using the System.Management classes (and WMI).

// 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; //sets required privilege
try {
q = new WqlEventQuery();
q.EventClassName = "__InstanceCreationEvent";
q.WithinInterval = new TimeSpan(0,0,10);

q.Condition = @"TargetInstance ISA 'Win32_USBControllerDevice' ";
Console.WriteLine(q.QueryString);
w = new ManagementEventWatcher(scope, q);

w.EventArrived += new EventArrivedEventHandler(we.UsbEventArrived);
w.Start();
Console.ReadLine();
}
catch(Exception e) {
Console.WriteLine(e.Message);
}
finally {
w.Stop();
}
}
public void UsbEventArrived(object sender, EventArrivedEventArgs e) {
//Get the Event object and display it
foreach(PropertyData pd in e.NewEvent.Properties) {
Console.WriteLine("\n============================= =========");
Console.WriteLine("{0},{1},{2}, {3}",pd.Name, pd.Type, pd.Value, pd.Origin);

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.



Closed Thread