Skip to content

Commit a9b1084

Browse files
committed
Add COM_FLAG constant for controlling how COM is initialised.
Issue #42 shows issues with initialising WMI in a pynput app. This appears to be down to how COM is initialised. Using MTA in this case appears to solve the error, but pythoncom defaults to STA and I don't know the ramifications of defaulting to MTA. I'm leaving the default behavious as is and adding a flag that users can change if they wish
1 parent b9b4756 commit a9b1084

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

screen_brightness_control/windows.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,29 @@
1919
__cache__ = __Cache()
2020
_logger = logging.getLogger(__name__)
2121

22+
COM_MODEL = None
23+
'''
24+
The concurrency model and flags used when calling `pythoncom.CoInitializeEx`.
25+
If left as `None` the library will call `CoInitialize` instead with whatever defaults it chooses.
26+
27+
Refer to the [MS docs](https://learn.microsoft.com/en-us/windows/win32/api/objbase/ne-objbase-coinit)
28+
for all the possible flags.
29+
30+
See also:
31+
- https://timgolden.me.uk/pywin32-docs/pythoncom__CoInitialize_meth.html
32+
- https://timgolden.me.uk/pywin32-docs/pythoncom__CoInitializeEx_meth.html
33+
- https://github.com/Crozzers/screen_brightness_control/issues/42
34+
'''
35+
2236

2337
def _wmi_init():
2438
'''internal function to create and return a wmi instance'''
2539
# WMI calls don't work in new threads so we have to run this check
2640
if threading.current_thread() != threading.main_thread():
27-
pythoncom.CoInitialize()
41+
if COM_MODEL is None:
42+
pythoncom.CoInitialize()
43+
else:
44+
pythoncom.CoInitializeEx(COM_MODEL)
2845
return wmi.WMI(namespace='wmi')
2946

3047

0 commit comments

Comments
 (0)