On 7/29/2013 12:19 PM, Stephen Clower wrote:
You can use WMI to watch for the event, and assuming the synthesizer
is functioning properly, you can act on it.
Speaking of that, back in the day we had a program that assisted in
managing a USB device, and one of it's features was that it
automatically detected when the device was plugged in and unplugged
through WMI, and then acted accordingly. I pulled out the meat of that
code if anyone is interested. It's written in VB.NET.
Aaron
-----------------------------------------------------
' Begin
' Global variables
Private Const deviceName As String = "My Favorite Device"
Private Const deviceManufacturer As String = "The Company"
Private WithEvents m_MediaConnectWatcher As ManagementEventWatcher
' The main function instantiates the ManagementEventWatcher, and queries
for the device
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim query As String = "SELECT * FROM __InstanceOperationEvent
WITHIN 10 WHERE TargetInstance ISA ""Win32_DiskDrive"""
m_MediaConnectWatcher = New ManagementEventWatcher(query)
m_MediaConnectWatcher.Start()
QueryForDevice()
End Sub
' This is the event handler that fires for MediaConnectWatcher
Public Sub m_MediaConnectWatcher_EventArrived(ByVal sender As Object,
ByVal e As System.Management.EventArrivedEventArgs) Handles
m_MediaConnectWatcher.EventArrived
Dim mbObj, mbObjCopy As ManagementBaseObject
mbObj = CType(e.NewEvent, ManagementBaseObject)
mbObjCopy = CType(mbObj("TargetInstance"), ManagementBaseObject)
' If the InterfaceType of the drive is USB, then check whether or
not it's the device
' or manufacturer that we're interested in.
Select Case mbObj.ClassPath.ClassName
Case "__InstanceCreationEvent"
If mbObjCopy("InterfaceType") = "USB" Then
If InStr(mbObjCopy("Caption"), deviceName) Or
InStr(mbObjCopy("Caption"), deviceManufacturer) Then
QueryForDevice()
End If
End If
Case "__InstanceDeletionEvent"
If mbObjCopy("InterfaceType") = "USB" Then
If InStr(mbObjCopy("Caption"), deviceName) Or
InStr(mbObjCopy("Caption"), deviceManufacturer) Then
QueryForDevice()
End If
End If
End Select
End Sub
' The form is closing, so remove the MediaConnectWatcher object
Private Sub Main_FormClosing(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.FormClosing
If Not m_MediaConnectWatcher Is Nothing Then
m_MediaConnectWatcher.Stop()
End If
End Sub
' QueryForDevice checks for existing devices that match our device
Private Sub QueryForDevice()
Dim bsDrives As OrderedDictionary = GetDeviceDriveLetters()
If bsDrives.Count > 0 Then
For Each bsDrive As DictionaryEntry In bsDrives
Dim driveLetter As String = bsDrive.Key
Dim sizes As ArrayList = bsDrive.Value
' Device is plugged in. Here you can get the drive letter,
the free space (size(0)),
' and total space (size(1))
Next
Else
' Device is unplugged
End If
End Sub
' GetDeviceDriveLetters creates a list of drive letters for the device
Public Function GetDeviceDriveLetters() As OrderedDictionary
On Error Resume Next
Dim bsDrives As New OrderedDictionary
Dim scope As New ManagementScope("\\.\root\CIMV2")
Dim mQuery As String = "SELECT Caption, DeviceID FROM Win32_DiskDrive"
Dim dd As ManagementObjectSearcher = New
ManagementObjectSearcher(scope.Path.ToString, mQuery)
If dd.Get.Count > 0 Then
For Each d As ManagementObject In dd.Get()
Dim pQuery As String = "ASSOCIATORS OF
{Win32_DiskDrive.DeviceID='" & d("DeviceID") & "'} WHERE AssocClass =
Win32_DiskDriveToDiskPartition"
Dim pp As ManagementObjectSearcher = New
ManagementObjectSearcher(scope.Path.ToString, pQuery)
If pp.Get.Count > 0 Then
For Each p As ManagementObject In pp.Get()
Dim lQuery As String = "ASSOCIATORS OF
{Win32_DiskPartition.DeviceID='" & p("DeviceID") & "'} WHERE AssocClass
= Win32_LogicalDiskToPartition"
Dim ll As ManagementObjectSearcher = New
ManagementObjectSearcher(scope.Path.ToString, lQuery)
If ll.Get.Count > 0 Then
Dim index As Integer = 0
For Each l As ManagementObject In ll.Get()
Dim caption As String = d("Caption")
If caption.Contains(deviceName) Or
caption.Contains(deviceManufacturer) Then
Dim driveLetter As String = l("DeviceID")
Dim driveSize As Array =
GetDriveSpace(driveLetter)
Dim sizes As ArrayList = New ArrayList
sizes.Add(driveSize(0))
sizes.Add(driveSize(1))
bsDrives.Insert(index, l("caption"), sizes)
End If
index += 1
Next
End If
Next
End If
Next
End If
On Error GoTo 0
Return bsDrives
End Function
' End
--
Aaron Smith
Web Development * App Development * Product Support Specialist
GW Micro, Inc. * 725 Airport North Office Park, Fort Wayne, IN 46825
260-489-3671 * gwmicro.com
To insure that you receive proper support, please include all past
correspondence (where applicable), and any relevant information
pertinent to your situation when submitting a problem report to the GW
Micro Technical Support Team.