Using WMI to Monitor for USB IrDA Devices

I decided to play around with WMI to see if I could monitor the insertion and removal of a USB IrDA device as well as scan for any that may be attached when the application is started. Below is the code I used to get it to work. The next step would be to figure out how to send messages to it ;).
 

using System;

using

System.Text;

using

System.Windows.Forms;

using System.Management;

class

UsbInfraredHelper {

     private ManagementEventWatcher watcher;

     public UsbInfraredHelper() { }

     public void Scan() {

          WqlObjectQuery objectQuery = new WqlObjectQuery("Select * from CIM_InfraredController");

          ManagementScope scope = new ManagementScope("root\cimv2");

          ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, objectQuery);

          StringBuilder sb = new StringBuilder();

          int count = 0;

          foreach (ManagementObject managementObject in searcher.Get()) {

               string deviceId = managementObject.Properties["DeviceId"].Value as string;

               sb.Append(

String.Format("Controller #{0}: {1}", ++count, deviceId));

          }

          MessageBox.Show((sb.Length == 0) ? "No devices found" : sb.ToString());

     }

     public void StartMonitor() {

          WqlEventQuery eventQuery;

          ManagementOperationObserver observer = new ManagementOperationObserver();

          ManagementScope scope = new ManagementScope("root\CIMV2");

          scope.Options.EnablePrivileges =

true;

          try {

               eventQuery =

new WqlEventQuery();

               eventQuery.EventClassName =

"__InstanceOperationEvent";

               eventQuery.WithinInterval =

new TimeSpan(0, 0, 3);

               eventQuery.Condition =

"TargetInstance ISA ‘Win32_InfraredDevice’";

               watcher = new ManagementEventWatcher(scope, eventQuery);

               watcher.EventArrived +=

new EventArrivedEventHandler(MonitoredEventHandler);

               watcher.Start();

               MessageBox.Show("Observing changes to Infrared connection status");

          } catch (Exception ex) {

               MessageBox.Show(ex.Message);

          }

     }

     public void MonitoredEventHandler(object sender, EventArrivedEventArgs e) {

          //Get the Event object and display its properties (all)

          foreach (PropertyData propertyData in e.NewEvent.Properties) {

               ManagementBaseObject mbo = null;

               StringBuilder sb = new StringBuilder();

               if ((mbo = propertyData.Value as ManagementBaseObject) != null) {

                    string deviceId = mbo.Properties["DeviceId"].Value as string;

                    string statusInfo = mbo.Properties["StatusInfo"].Value as string;

                    MessageBox.Show(String.Format("{0} – {1}", deviceId, statusInfo));

               }

          }

     }

     public void StopMonitor() {

          if (watcher != null) {

               watcher.Stop();

          }

          MessageBox.Show("No longer monitoring for USB Infrared devices.");

     }

}

Advertisements

One thought on “Using WMI to Monitor for USB IrDA Devices

  1. Hi, J,This one looks pretty good right now!!P

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s