Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

0 views
Skip to first unread message

jack

unread,
Sep 11, 2003, 9:28:52 PM9/11/03
to
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]

unread,
Sep 11, 2003, 10:53:28 PM9/11/03
to
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/247reference/msgs/32/164968.aspx

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

"jack" <ja...@mrolinux.com> wrote in message
news:eMj80xMe...@TK2MSFTNGP11.phx.gbl...

jack

unread,
Sep 11, 2003, 11:23:44 PM9/11/03
to
Hello,

Great, any example in VB.NET?

Thanks,

Jack
"Greg Ewing [MVP]" <gewing@_NO_SPAM_gewing.com> wrote in message
news:%231cQjkN...@TK2MSFTNGP12.phx.gbl...

William Ryan

unread,
Sep 11, 2003, 11:20:44 PM9/11/03
to
YOU ARE THE MAN!

"Greg Ewing [MVP]" <gewing@_NO_SPAM_gewing.com> wrote in message
news:#1cQjkNe...@TK2MSFTNGP12.phx.gbl...

Willy Denoyette [MVP]

unread,
Sep 12, 2003, 7:49:00 AM9/12/03
to

"jack" <ja...@mrolinux.com> wrote in message news:eMj80xMe...@TK2MSFTNGP11.phx.gbl...

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.

0 new messages