472,143 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,143 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 13392
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by zfeld | last post: by
1 post views Thread by barbara_dave | last post: by
3 posts views Thread by geskerrett | last post: by
reply views Thread by anitajose | last post: by
ashitpro
1 post views Thread by ashitpro | last post: by

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.