By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,526 Members | 1,888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,526 IT Pros & Developers. It's quick & easy.

Event/Notification when a USB device plugged in

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.