473,503 Members | 3,045 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
Nov 15 '05 #1
4 4066
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" <ja**@mrolinux.com> wrote in message
news:eM**************@TK2MSFTNGP11.phx.gbl...
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

Nov 15 '05 #2
Hello,

Great, any example in VB.NET?

Thanks,

Jack
"Greg Ewing [MVP]" <gewing@_NO_SPAM_gewing.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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" <ja**@mrolinux.com> wrote in message
news:eM**************@TK2MSFTNGP11.phx.gbl...
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


Nov 15 '05 #3
YOU ARE THE MAN!
"Greg Ewing [MVP]" <gewing@_NO_SPAM_gewing.com> wrote in message
news:#1**************@TK2MSFTNGP12.phx.gbl...
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" <ja**@mrolinux.com> wrote in message
news:eM**************@TK2MSFTNGP11.phx.gbl...
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


Nov 15 '05 #4

"jack" <ja**@mrolinux.com> wrote in message news:eM**************@TK2MSFTNGP11.phx.gbl...
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


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.

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
7152
by: Wouter van Ooijen | last post by:
I want to use Python to interface with an USB HID device (not a keyboard or mouse, just something that uses the HID driver to avoid the need for a specific driver). Is this possible in pure Python...
1
2902
by: John Rauhe | last post by:
Hello, Does anybody know how to detect if an mass-storage device has been added to the system ? I am making a program that will (should) detect when a CompactFlash memory card has been inserted...
6
24503
by: sanjana | last post by:
hi i wanna detect if a anything is connected to the usb port I am using system.management class for tht purpose this is my code class usbdetect { public static void Main() { usbdetect we =...
5
9502
by: Al | last post by:
Hi, Is there any way to detected if a device is connected? How would I query to see the device is connected to network. My application is mobile and mostly on wireless connection but it could be...
4
6995
by: Sara | last post by:
Hi I want to code a program which could send SMS through web page to mobile phone, but because there is no Service here such as SMPP , therefore I have GSM mobile phone which connected directly to...
9
2011
by: erickwan88 | last post by:
I am doing a final year project for my school and is going to provide for an organization, so I am asking for some help on here. Indeed, I have no idea on how to get the input from my pen driver...
5
2151
by: SungHyun Nam | last post by:
Hello, If there are two ethernet device, how I can select a device to send/receive? Actually I want to do loopback test between twe ethernet device. Send a UDP packet through eth0 and receives...
6
5390
by: Keith Lee | last post by:
All: I am attempting to compile the perl module Device::SerialPort and get this error during make -- Manifying blib/man3/Device::SerialPort.3pm Can't open blib/man3/Device::SerialPort.3pm for...
6
5367
by: =?Utf-8?B?bmNvbG9zaQ==?= | last post by:
Is it possible to render a winform directly to a memory device context instead of rendering to a display device context ?
4
4584
by: Eran.Yasso | last post by:
Hi all, I am trying to write application which runs on my PC that sends data to an application running on Windows mobile. The problem is that the tcp over activesync doesn't work. The device is...
0
7193
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.