0

I'm trying to control my laptop fan speed using WMI in python.

This is my python code:

import wmi

c = wmi.WMI ()
cim_fan = c.CIM_Fan()
fan_speed = 4000
cim_fan[0].SetSpeed(fan_speed)

But it throws this error:

Traceback (most recent call last):
  File "C:\Users\AliEnt\Desktop\Athena Codes\control_fan\main.py", line 7, in <module>
    cim_fan[0].SetSpeed(fan_speed)
  File "C:\Users\AliEnt\AppData\Roaming\Python\Python39\site-packages\wmi.py", line 473, in __call__
    handle_com_error()
  File "C:\Users\AliEnt\AppData\Roaming\Python\Python39\site-packages\wmi.py", line 258, in handle_com_error
    raise klass(com_error=err)
wmi.x_wmi: <x_wmi: Unexpected COM Error (-2147352567, 'Exception occurred.', (0, 'SWbemObjectEx', 'This method is not implemented in any class ', None, 0, -2147217323), None)>

Then I took a closer look at the CIM_Fan class documentation and saw this:

Method Description
Reset Requests a reset of the logical device. Not implemented by WMI.
SetPowerState Defines the desired power state for a logical device and when a device should be put into that state. Not implemented by WMI.
SetSpeed Sets the fan speed. Not implemented by WMI.

Here it is said that I have to implement it on my own provider. I don't understand what does it mean by saying provider.

I'm familiar with C++ but I'm pretty new with Microsoft APIs. So I prefer to do it by using python instead of compiling a C++ program. Or at least with a minimum C++ code and a wrapper.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Ali Ent
  • 1,420
  • 6
  • 17
  • 2
    A WMI Provider is a COM object (https://learn.microsoft.com/en-us/windows/win32/wmisdk/creating-wmi-providers). You can implement COM objects with python but it seems really overkill (the only benefit would be anyone else could use it using WMI). If it's "just for you" (ie: if you don't plan to sell such a fan-aware component), then it'll be easier to do it in pure python with interop or write your own python code using native C/C++ interop. – Simon Mourier May 21 '23 at 14:12
  • Thank you @SimonMourier, I've searched about the keywords "COM" and "InterOp" you hinted. I was thinking that CIM provider method equals fan speed control and it was wrong. As I understood, fan control is completely different thing and provider method is just an interface to access the backend code from WMI or COM. – Ali Ent May 22 '23 at 19:48
  • 2
    yes, that's it, windows has no fan control API. You can have a loot at that code to check how they doing it but it's pretty complex and very hardware-specific https://github.com/LibreHardwareMonitor/LibreHardwareMonitor – Simon Mourier May 22 '23 at 20:54

0 Answers0