473,379 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Event/Notification when a USB device plugged in

Is there an event or notification that my application can receive when a USB
device (or other) is plugged in?

I have looked at WMI as this appears to be the right area but cannot see how
to get notification when this particular event occurs. The same goes for
removal as well.

TIA

Charles
Nov 20 '05 #1
4 13686
Hi,

This will only work on Windows XP!!! Here is a real simple
example. Add a reference to system.management

Imports System.Management

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Dim WithEvents w As ManagementEventWatcher

Dim q As WqlEventQuery

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

q = New WqlEventQuery("Select * from Win32_DeviceChangeEvent")

w = New ManagementEventWatcher(q)

w.Start()

End Sub

Private Sub w_EventArrived(ByVal sender As Object, ByVal e As
System.Management.EventArrivedEventArgs) Handles w.EventArrived

MessageBox.Show("Device Event")

End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

w.Stop()

End Sub

End Class

Ken

---------------------

"Charles Law" <bl***@nowhere.com> wrote in message
news:uX**************@TK2MSFTNGP09.phx.gbl...
Is there an event or notification that my application can receive when a USB device (or other) is plugged in?

I have looked at WMI as this appears to be the right area but cannot see how to get notification when this particular event occurs. The same goes for
removal as well.

TIA

Charles

Nov 20 '05 #2
Hi Ken

Thanks very much. I have run it and it does what I hoped. I am having
difficulty interpreting the System.Management.EventArrivedEventArgs though.

Is there a resource that I can go to in order to find out how to read this
class?

I can see that the NewEvent property is of type ManagementBaseObject, but I
do not know what to do with this. For example, I would like to determine
what has been inserted/connected, such as a serial port device, and allow my
user to use it on its arrival. Is that a possibility?

Charles
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:um**************@TK2MSFTNGP12.phx.gbl...
Hi,

This will only work on Windows XP!!! Here is a real simple
example. Add a reference to system.management

Imports System.Management

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Dim WithEvents w As ManagementEventWatcher

Dim q As WqlEventQuery

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

q = New WqlEventQuery("Select * from Win32_DeviceChangeEvent")

w = New ManagementEventWatcher(q)

w.Start()

End Sub

Private Sub w_EventArrived(ByVal sender As Object, ByVal e As
System.Management.EventArrivedEventArgs) Handles w.EventArrived

MessageBox.Show("Device Event")

End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

w.Stop()

End Sub

End Class

Ken

---------------------

"Charles Law" <bl***@nowhere.com> wrote in message
news:uX**************@TK2MSFTNGP09.phx.gbl...
Is there an event or notification that my application can receive when a

USB
device (or other) is plugged in?

I have looked at WMI as this appears to be the right area but cannot see

how
to get notification when this particular event occurs. The same goes for
removal as well.

TIA

Charles


Nov 20 '05 #3
Hi,

Unfortunately it doesn't give you very much information back. You
can use the wmi to get a list of devices and check for any changes.

Private Sub GetDevices()

Dim moReturn As Management.ManagementObjectCollection

Dim moSearch As Management.ManagementObjectSearcher

Dim mo As Management.ManagementObject

moSearch = New Management.ManagementObjectSearcher("Select * from
Win32_PnPEntity")

moReturn = moSearch.Get

For Each mo In moReturn

Dim strOut As String

strOut = String.Format("{0} - Status {1}", mo("Name").ToString,
mo("Status").ToString)

ListBox1.Items.Add(strOut)

Next

End Sub

Ken

---------------------

"Charles Law" <bl***@nowhere.com> wrote in message
news:O2**************@TK2MSFTNGP10.phx.gbl...
Hi Ken

Thanks very much. I have run it and it does what I hoped. I am having
difficulty interpreting the System.Management.EventArrivedEventArgs though.
Is there a resource that I can go to in order to find out how to read this
class?

I can see that the NewEvent property is of type ManagementBaseObject, but I do not know what to do with this. For example, I would like to determine
what has been inserted/connected, such as a serial port device, and allow my user to use it on its arrival. Is that a possibility?

Charles
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:um**************@TK2MSFTNGP12.phx.gbl...
Hi,

This will only work on Windows XP!!! Here is a real simple
example. Add a reference to system.management

Imports System.Management

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Dim WithEvents w As ManagementEventWatcher

Dim q As WqlEventQuery

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

q = New WqlEventQuery("Select * from Win32_DeviceChangeEvent")

w = New ManagementEventWatcher(q)

w.Start()

End Sub

Private Sub w_EventArrived(ByVal sender As Object, ByVal e As
System.Management.EventArrivedEventArgs) Handles w.EventArrived

MessageBox.Show("Device Event")

End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

w.Stop()

End Sub

End Class

Ken

---------------------

"Charles Law" <bl***@nowhere.com> wrote in message
news:uX**************@TK2MSFTNGP09.phx.gbl...
Is there an event or notification that my application can receive when a
USB
device (or other) is plugged in?

I have looked at WMI as this appears to be the right area but cannot
see how
to get notification when this particular event occurs. The same goes

for removal as well.

TIA

Charles



Nov 20 '05 #4
Thanks again Ken. I have found Win32_PnpEntity and others in MSDN so I will
investigate further.

Cheers

Charles
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:OL**************@tk2msftngp13.phx.gbl...
Hi,

Unfortunately it doesn't give you very much information back. You can use the wmi to get a list of devices and check for any changes.

Private Sub GetDevices()

Dim moReturn As Management.ManagementObjectCollection

Dim moSearch As Management.ManagementObjectSearcher

Dim mo As Management.ManagementObject

moSearch = New Management.ManagementObjectSearcher("Select * from
Win32_PnPEntity")

moReturn = moSearch.Get

For Each mo In moReturn

Dim strOut As String

strOut = String.Format("{0} - Status {1}", mo("Name").ToString,
mo("Status").ToString)

ListBox1.Items.Add(strOut)

Next

End Sub

Ken

---------------------

"Charles Law" <bl***@nowhere.com> wrote in message
news:O2**************@TK2MSFTNGP10.phx.gbl...
Hi Ken

Thanks very much. I have run it and it does what I hoped. I am having
difficulty interpreting the System.Management.EventArrivedEventArgs though.

Is there a resource that I can go to in order to find out how to read this
class?

I can see that the NewEvent property is of type ManagementBaseObject,

but I
do not know what to do with this. For example, I would like to determine
what has been inserted/connected, such as a serial port device, and
allow my
user to use it on its arrival. Is that a possibility?

Charles
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:um**************@TK2MSFTNGP12.phx.gbl...
Hi,

This will only work on Windows XP!!! Here is a real simple example. Add a reference to system.management

Imports System.Management

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Dim WithEvents w As ManagementEventWatcher

Dim q As WqlEventQuery

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

q = New WqlEventQuery("Select * from Win32_DeviceChangeEvent")

w = New ManagementEventWatcher(q)

w.Start()

End Sub

Private Sub w_EventArrived(ByVal sender As Object, ByVal e As
System.Management.EventArrivedEventArgs) Handles w.EventArrived

MessageBox.Show("Device Event")

End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

w.Stop()

End Sub

End Class

Ken

---------------------

"Charles Law" <bl***@nowhere.com> wrote in message
news:uX**************@TK2MSFTNGP09.phx.gbl...
> Is there an event or notification that my application can receive
when a USB
> device (or other) is plugged in?
>
> I have looked at WMI as this appears to be the right area but cannot see how
> to get notification when this particular event occurs. The same goes for > removal as well.
>
> TIA
>
> Charles
>
>



Nov 20 '05 #5

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

Similar topics

3
by: Bruno G. | last post by:
Hello! Is it possible for IIS to send some sort of notification when an ASP error occurs on a page? I can see them in the web logs, but I was wondering if there was a way to receive an email...
2
by: dubian | last post by:
I would like to propagate an event signal to an external application when a table in my MSSql2000 server is updated. Prog A; I have an external application adding records to a table. Prog B; I...
1
by: zfeld | last post by:
I need an event notification when the user types anything in the combobox textfield? I know about the TextChanged Event but the problem is this is also fired when the user selects an item in the...
3
by: SB | last post by:
Having gone through couple of books on .NET web services, I could not find any details event notification. They all cover in detail the coomon request/response web services implementation. I would...
1
by: barbara_dave | last post by:
Hi all, I want do Event/Notification when a USB device plugged in. I am working on a Windows 2000 computer. Is there a way to do that? Thanks a lot!
3
by: geskerrett | last post by:
We have been asked to develop and application for a client that is a 'notification" system. We would like to use python, but are struggling to find the right starting point. Any suggestions, tips...
0
by: anitajose | last post by:
Hi, I have a PHP program and a VC++ program. Is it possible, when i click on a button in PHP user screen, I should get an event notification in VC++ service program and do the appropriate action...
0
by: Shival | last post by:
Hi, I have a Device that will be used by dentist to take their paitient teaath pics. this Device is having a click button from which the device takes pics. The Device is configured to my OS...
1
ashitpro
by: ashitpro | last post by:
Hi there, I am working on a distributed service detection project, written in C/C++ Platform is Linux. Each system under network has mysql database for general storage. If first node queries to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.