Search code examples
pythonwindowswinapipython-2.7wmi

Replacing WMI calls with Winapi


I am currently looking to replace all my code that uses WMI with equivalent win32 api calls. I am doing this because i have noticed that WMI calls take much longer to perform than win32 api calls. Some examples can be seen below:

Listing services:

  • wmi: ~4 seconds
  • winapi: ~0.003 seconds

Listing Printers:

  • wmi: ~0.5 seconds
  • winapi: ~0.009 seconds

Listing Windows Users:

  • wmi: ~0.5 seconds
  • winapi: ~0.2 seconds

Is it possible to replace all wmi calls? As a general rule, is every task available in WMI possible to perform with winapi calls?

I have already managed to replace most of my wmi code but i have the following left:

  • Creating Restore Points
  • Enumerating network adapters
  • Enumerating partitions

I am having some difficulty with enumerating network adapters in particular.

Its also worth mentioning that i am using python (2.7) to perform all these calls (using ctypes, windll and win32 api libraries such as win32net, win32service etc...).


Solution

  • Q: Is it possible to replace all wmi calls?

    A: Most of the WMI queries can be replaced with WinAPI methods, with a few exceptions.

    Q: As a general rule, is every task available in WMI possible to perform with winapi calls?

    A: No, not all the WMI classes have WinAPI equivalents. For example, the IIS related tasks only can be performed using the IIS WMI Provider. Also, there are no WinAPI methods to enumerate the restore points, and so on.

    For you pending tasks you can use these WinAPI methods: