Checking if USB device is plugged in

  • Thread starter Thread starter beaker
  • Start date Start date
B

beaker

Hi,

I'm looking for a way of checking whether a specific USB device (a
webcam) is plugged in. I have the Vendor and Product ID numbers for the
device.

I found an article (http://www.vsj.co.uk/articles/display.asp?id=600)
with code sample that does the trick for human input devices (mouse,
keyboard, ...) but am struggling for my webcam.

Any help appreciated, thanks.

Gary
 
beaker said:
Hi,

I'm looking for a way of checking whether a specific USB device (a webcam)
is plugged in. I have the Vendor and Product ID numbers for the device.

I found an article (http://www.vsj.co.uk/articles/display.asp?id=600) with
code sample that does the trick for human input devices (mouse, keyboard,
...) but am struggling for my webcam.

Any help appreciated, thanks.

Gary

Gary,

It's easy outside of .NET if you handle the WM_DEVICECHANGE message. Below
is a snippet of a MFC program that does that. It shouldn't be hard to map
this to .NET, here's a discussion of just that, I haven't tried it myself:

http://www.codecomments.com/archive291-2004-4-185490.html

My MFC (edited) code snippet:

BOOL CConfigWinDlg::OnDeviceChange(UINT nEventType, DWORD_PTR dwData)
{
DEV_BROADCAST_DEVICEINTERFACE *devInterface;
DEV_BROADCAST_HDR *brHeader = (DEV_BROADCAST_HDR *)dwData;

switch (nEventType)
{
case (DBT_DEVICEARRIVAL):
if (brHeader && brHeader->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
{
devInterface = (DEV_BROADCAST_DEVICEINTERFACE *)brHeader;
if (strstr(strupr(devInterface->dbcc_name), "VID_0403&PID_6006"))
{
// my device plugged in
// do something cool
}
else
{
// some other device plugged in
}
}
else
{
// unknown device plugged in
}
}
break;

// other cases
}
[snip]
 
andrew said:
Hi,

I'm looking for a way of checking whether a specific USB device (a webcam)
is plugged in. I have the Vendor and Product ID numbers for the device.

I found an article (http://www.vsj.co.uk/articles/display.asp?id=600) with
code sample that does the trick for human input devices (mouse, keyboard,
...) but am struggling for my webcam.

Any help appreciated, thanks.

Gary


Gary,

It's easy outside of .NET if you handle the WM_DEVICECHANGE message. Below
is a snippet of a MFC program that does that. It shouldn't be hard to map
this to .NET, here's a discussion of just that, I haven't tried it myself:

http://www.codecomments.com/archive291-2004-4-185490.html

My MFC (edited) code snippet:

BOOL CConfigWinDlg::OnDeviceChange(UINT nEventType, DWORD_PTR dwData)
{
DEV_BROADCAST_DEVICEINTERFACE *devInterface;
DEV_BROADCAST_HDR *brHeader = (DEV_BROADCAST_HDR *)dwData;

switch (nEventType)
{
case (DBT_DEVICEARRIVAL):
if (brHeader && brHeader->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
{
devInterface = (DEV_BROADCAST_DEVICEINTERFACE *)brHeader;
if (strstr(strupr(devInterface->dbcc_name), "VID_0403&PID_6006"))
{
// my device plugged in
// do something cool
}
else
{
// some other device plugged in
}
}
else
{
// unknown device plugged in
}
}
break;

// other cases
}
[snip]


Thanks, the article you gave the link to does half of what I need, and I
can now detect when the device is plugged in - but I still need to know
if the device is present when my app starts (and so misses the
appropriate event for that method.)

Any ideas?

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top