Detecting connected USB devices using WqlEventQuery and retrieving their descriptive information

I have a game around WqlEventQuery to identify the USB connected device

var query = new WqlEventQuery();
query.EventClassName = "__InstanceOperationEvent";
query.WithinInterval = new TimeSpan(0, 0, 2);
query.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'";

using (var watcher = new ManagementEventWatcher(query))
{
   watcher.EventArrived += WatcherEvent;
   watcher.Start();

   ... Wait condition ...

   watcher.Stop();
}

      

then I tried to find the device property in the watcher event handler

    foreach (var mbo in e.NewEvent.Properties.Cast<PropertyData>().Where(i => i.Value != null && i.Value is ManagementBaseObject).Select(pdData => (ManagementBaseObject)pdData.Value).Where(mbo => mbo != null))
    {
       if (mbo.ClassPath.ClassName == "Win32_USBControllerDevice")
       {
           foreach (var prop in mbo.SystemProperties)
           {
               ... look for the property content
           }
       }
    }

      

but couldn't find a place where I could retrieve information about the connected device. So when I connect my phone via USB. I want to extract information that this is a phone of a specific model from a specific manufacturer, etc.

Am I going to do the right thing to get this information? should I try something different or more effective in this regard?

Thank!

+3


source to share


3 answers


Try taking a look at Win32_PnPEntity. I believe Win32_USBControllerdevice is the "meta level" element.



+2


source


Below is the request: -We must use "Win32_PnPEntity" instead of USB in this case: -

WqlEventQuery insertQuery = new WqlEventQuery(SELECT * FROM __InstanceCreationEvent  WITHIN 2 WHERE TargetInstance ISA 'Win32_PnPEntity'"

              ManagementEventWatcher insertWatcher = new ManagementEventWatcher(insertQuery);
              insertWatcher.EventArrived += new EventArrivedEventHandler(DeviceInsertedEvent);

            insertWatcher.Start();

            insertWatcher.WaitForNextEvent();

      



So this is how the handler looks like: -

 private void DeviceInsertedEvent(object sender, EventArrivedEventArgs e)

 {

 ManagementBaseObject instance = (ManagementBaseObject)e.NewEvent["TargetInstance"];

        foreach (var property in instance.Properties)

 {

 try
     {
        string name = property.Value.ToString();//name of your device
        string deviceId = instance.GetPropertyValue("PNPDeviceID").ToString();
        if (name == "something")
                {
                 ....your code.....
                }
     }
 catch
     {
     }
 }

 }

      

+1


source


I know I am late to this party, but maybe my answer will be helpful to someone. I don't recommend looking at __InstanceCreationEvent, especially if you are using a complex query. I experienced very poor performance with queries like this. It wasn't my application that was slow, but it caused the WMI service on the computer to run on 80% CPU.

I recommend watching for some simpler events such as Win32_DeviceChangeEvent to detect the USB device to be changed. If this event is triggered, you use the ManagementObjectSearcher class to actually check if the required device is connected.

If you are unable to use a different event, try using the smallest scope needed and pinpoint your request.

0


source







All Articles