0

I'm trying to create a windows service to disable unwanted usb-devices and enable if it's allowed. To do so I need to monitor connect and disconnect events even for disabled devices.

Now I have the solution using wmi-events like this: Select * from __InstanceCreationEvent within 2 where TargetISA 'Win32_PnPDevice'. However it uses CPU a lot and in my opinion will bother users on weak PCs.

I heard about WndProc but as far as I understood it works only for programs with gui (window), not for services.

All in all I need the event system that will not be such resource-intensive as wmi to avoid permanent CPU overload. I need only connect / disconnect events. It is not a problem to get full info about new / removed device.

  • Are you using a WQL event listener for this, of polling the state of all USB devices? See [How do I get information about recently connected USB device?](https://stackoverflow.com/a/54298316/7444103) – Jimi Jul 11 '23 at 10:37
  • @Jimi thx for your comment! If i use class '*Win32_DiskDrive*' as you mention in your answer - performance is OK. The problem is i need to get events on **disabled** devices as well. That's why i need to use class '*Win32_PnPDevice*' or '*CIM_LogicalDevice*'. But when i use one of them my CPU gets permanent (*as program runs*) ~10% load. At weak PCs it will be ~20%. – Kryptonit364 Jul 11 '23 at 12:06
  • Maybe, but that's not exactly what I was asking. The point is: are you using events (as shown in that code) or polling (i.e., using a Timer to query the state of devices)? If you're polling, switch to WQL events – Jimi Jul 11 '23 at 12:39
  • Tried both methods. Events are little bit better in performance so I use events. – Kryptonit364 Jul 11 '23 at 13:34

0 Answers0