Device Manager listing in VB.Net

H

hayworth

Does anyone have any VB.Net code to get the devices listed / enumerated
in the Device Manager listing.


I'm trying to figure out if a device (it's actually a digital camera)
is connected to the computer. When it's plugged in (Firewire), it
shows up in Device Manager under the "Imaging Devices" catagory. When
it's not plugged in, it doesn't show up. When it's plugged in, there
is a registry key that says ReferenceCount = 1, and when it's not
plugged in, it says it = 0. The problem with checking that registry
entry is that it doesn't depend on the camera model -- it depends on
the specific camera. If I plug in a different Fuji camera of the very
same model, it will add a new registry entry with a different big long
number. So I got that working on my first camera, but when I plugged
in the second camera it no longer worked. Apparently it leaves
registry litter around for every camera you've ever plugged in even if
you've since unplugged it.


Is there someway to do it with the WMI? Or is there a simpler way?


Thanks,
Mark H.
 
H

hayworth

OK, I finally found an answer to my own question. Here is the code. I
hope someone else will find it useful, be able to adapt it, and save
the hours of time it took me to figure it out.
Mark H.


' Checks the Device Manager entry for the Fujifilm camera.
' If a Fuji S20 camera is detected, the function will return True.
' If it's not there, the function will return False.
' Requires a reference to "System.Management" to be added to your
project's references.
Public Function FujiCameraDetected() As Boolean
Try

' See if the Fujifilm camera shows up in the device manager.
Dim info As System.Management.ManagementObject
Dim search As System.Management.ManagementObjectSearcher
Dim deviceName As String
Dim result As DialogResult
Dim cameraIsSeenByWindows As Boolean = False
Dim showDebugPrompts As Boolean = False

search = New System.Management.ManagementObjectSearcher("SELECT *
From Win32_PnPEntity")
For Each info In search.Get()
' Go through each device detected.
deviceName = CType(info("Caption"), String) ' Get the name of
this particular device.
If showDebugPrompts Then
result = MessageBox.Show(deviceName, "Device Listing",
MessageBoxButtons.OKCancel)
If result = DialogResult.Cancel Then Exit For
End If
If InStr(deviceName, "Fujifilm", CompareMethod.Text) > 0 Then
' The device is the Fujifilm camera we're looking for.
cameraIsSeenByWindows = True
If showDebugPrompts Then
result = MessageBox.Show("The Fuji camera is seen by windows.",
"Camera Recognized", MessageBoxButtons.OKCancel)
If result = DialogResult.Cancel Then Exit For
End If
Exit For ' Bail out - no need to check the rest of the
devices.
End If
Next
If cameraIsSeenByWindows Then
If showDebugPrompts Then MsgBox("Windows does not see the Fuji
Camera.", MsgBoxStyle.Information)
Return True
Else
If showDebugPrompts Then MsgBox("Windows sees the Fuji Camera.",
MsgBoxStyle.Information)
Return False
End If

Catch ex As Exception
Call ShowErrorWithoutLog(ex.Message, ex.StackTrace)
End Try

End Function
 

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

Top