From 0f03a9630d57378711f2ee76a4acf7a2a933c807 Mon Sep 17 00:00:00 2001 From: moi15moi Date: Sat, 25 Jan 2025 15:20:50 -0500 Subject: [PATCH 1/3] [__init__.py] Use WinDLL/OleDLL instead of windll/oledll --- comtypes/__init__.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/comtypes/__init__.py b/comtypes/__init__.py index 9646695d..33574462 100644 --- a/comtypes/__init__.py +++ b/comtypes/__init__.py @@ -24,8 +24,8 @@ from ctypes import * # noqa from ctypes import HRESULT # noqa from ctypes import _Pointer, _SimpleCData # noqa -from ctypes import c_int, c_ulong, oledll, windll -from ctypes.wintypes import DWORD # noqa +from ctypes import c_int, c_ulong, OleDLL, WinDLL +from ctypes.wintypes import DWORD, LPVOID # noqa import logging import sys from typing import TYPE_CHECKING @@ -121,8 +121,18 @@ class ReturnHRESULT(Exception): ################################################################ # Initialization and shutdown -_ole32 = oledll.ole32 -_ole32_nohresult = windll.ole32 # use this for functions that don't return a HRESULT +_ole32 = OleDLL("ole32") + +_CoInitializeEx = _ole32.CoInitializeEx +_CoInitializeEx.argtypes = [LPVOID, DWORD] +_CoInitializeEx.restype = HRESULT + +_ole32_nohresult = WinDLL("ole32") # use this for functions that don't return a HRESULT + +_CoUninitialize = _ole32_nohresult.CoUninitialize +_CoUninitialize.argtypes = [] +_CoUninitialize.restype = None + COINIT_MULTITHREADED = 0x0 COINIT_APARTMENTTHREADED = 0x2 @@ -138,7 +148,7 @@ def CoInitializeEx(flags=None): if flags is None: flags = getattr(sys, "coinit_flags", COINIT_APARTMENTTHREADED) logger.debug("CoInitializeEx(None, %s)", flags) - _ole32.CoInitializeEx(None, flags) + _CoInitializeEx(None, flags) # COM is initialized automatically for the thread that imports this @@ -156,7 +166,7 @@ def CoInitializeEx(flags=None): # in which we are using COM def CoUninitialize(): logger.debug("CoUninitialize()") - _ole32_nohresult.CoUninitialize() + _CoUninitialize() ################################################################ From 5ac0b70b6809fabbaf5fcc0f98e718644431a14d Mon Sep 17 00:00:00 2001 From: moi15moi Date: Sat, 25 Jan 2025 15:23:04 -0500 Subject: [PATCH 2/3] [_comobject.py] Import _CoUninitialize --- comtypes/_comobject.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/comtypes/_comobject.py b/comtypes/_comobject.py index e808b644..69b889f8 100644 --- a/comtypes/_comobject.py +++ b/comtypes/_comobject.py @@ -29,7 +29,7 @@ ) from typing import Union as _UnionT -from comtypes import GUID, IPersist, IUnknown, hresult +from comtypes import GUID, IPersist, IUnknown, _CoUninitialize, hresult from comtypes._vtbl import _MethodFinder, create_dispimpl, create_vtbl_mapping from comtypes.automation import DISPID, DISPPARAMS, EXCEPINFO, VARIANT from comtypes.errorinfo import ISupportErrorInfo @@ -124,10 +124,6 @@ def _InterlockedDecrement(ob: c_long) -> int: LONG # technically, it is a HRESULT, but we want to avoid the OSError ) -_CoUninitialize = _ole32_nohresult.CoUninitialize -_CoUninitialize.argtypes = [] -_CoUninitialize.restype = None - _CoAddRefServerProcess = _ole32.CoAddRefServerProcess _CoAddRefServerProcess.argtypes = [] _CoAddRefServerProcess.restype = ULONG From 46c3971713c96d52e0082fa13cff4622541d432e Mon Sep 17 00:00:00 2001 From: moi15moi Date: Sat, 25 Jan 2025 15:24:12 -0500 Subject: [PATCH 3/3] [_post_coinit\unknwn.py] Import _CoUninitialize --- comtypes/_post_coinit/unknwn.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/comtypes/_post_coinit/unknwn.py b/comtypes/_post_coinit/unknwn.py index 6de21da5..e1e29c2e 100644 --- a/comtypes/_post_coinit/unknwn.py +++ b/comtypes/_post_coinit/unknwn.py @@ -5,7 +5,7 @@ from ctypes import HRESULT, POINTER, byref, c_ulong, c_void_p from typing import TYPE_CHECKING, ClassVar, List, Optional, Type, TypeVar -from comtypes import GUID, _ole32_nohresult, com_interface_registry +from comtypes import GUID, _CoUninitialize, com_interface_registry from comtypes._memberspec import STDMETHOD, ComMemberGenerator, DispMemberGenerator from comtypes._post_coinit import _cominterface_meta_patcher as _meta_patch from comtypes._post_coinit.instancemethod import instancemethod @@ -17,7 +17,7 @@ def _shutdown( - func=_ole32_nohresult.CoUninitialize, + func=_CoUninitialize, _debug=logger.debug, _exc_clear=getattr(sys, "exc_clear", lambda: None), ):