Author Topic: WMI/WQL  (Read 13060 times)

vanweric

  • Member
  • ***
  • Posts: 24
WMI/WQL
« on: June 17, 2011, 12:32:57 pm »
I've been looking into using WMI/WQL to register for USB insertion/removal events.  I am getting events, but I'm not sure how to interpret them.  I've registered for __InstanceCreationEvent and __InstanceDeletionEvent for 'Win32_USBDevice'.  The Win32_USBDevice object contains three relevant fields:

DeviceID: USB\VID_1234&PID_5678\12345           (the 12345 is the serial number, and the vid/pid are temporary)
ClassGuid: {745a17a0-74d3-11d0-b6fe-00a0c90f57da}
Service:  HidUsb

How do I translate this to a path that I can use to open a file handle?

The ClassGuid doesn't match what I've been getting from Jan's GetDevicePaths, so I might be registering for the wrong events...


The registration code follows:

Code: [Select]
private ManagementEventWatcher CreateWatcher(string EventClass)
        {
            WqlEventQuery query = new WqlEventQuery();
            query.EventClassName = EventClass;
            query.WithinInterval = new TimeSpan(0, 0, 1);
            query.Condition = String.Format(@"TargetInstance ISA 'Win32_USBDevice'");

            ConnectionOptions opt = new ConnectionOptions();
            //opt.EnablePrivileges = true;
            ManagementScope scope = new ManagementScope("root\\CIMV2", opt);


            ManagementEventWatcher watcher = new ManagementEventWatcher(scope, query);
            //watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
            watcher.Query = query;
            //watcher.Start();
            return watcher;
        }

I run this for __InstanceCreationEvent and __InstanceDeletionEvent, and register with separate event handlers.  EventArrived+= and watcher.Start() are actually called elsewhere due to how I factored my code, so I put them here just for completeness.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: WMI/WQL
« Reply #1 on: June 17, 2011, 06:22:51 pm »