From e1e06ae1897e9b7a5cd8106473fbd0dbaf3a8fbc Mon Sep 17 00:00:00 2001 From: moi15moi Date: Sat, 11 Jan 2025 17:16:20 -0500 Subject: [PATCH 1/6] [_comobject.py] Use WinDLL/OleDLL instead of windll/oledll --- comtypes/_comobject.py | 85 +++++++++++++++++++++++++++++++++++------- 1 file changed, 72 insertions(+), 13 deletions(-) diff --git a/comtypes/_comobject.py b/comtypes/_comobject.py index 74d0dbf7..150d5955 100644 --- a/comtypes/_comobject.py +++ b/comtypes/_comobject.py @@ -4,14 +4,16 @@ from ctypes import ( POINTER, FormatError, + OleDLL, Structure, + WinDLL, byref, c_long, c_void_p, - oledll, + c_wchar_p, pointer, - windll, ) +from ctypes.wintypes import INT, LONG, LPVOID, UINT, ULONG, WORD from typing import ( TYPE_CHECKING, Any, @@ -29,6 +31,7 @@ from comtypes import GUID, IPersist, IUnknown, hresult from comtypes._vtbl import _MethodFinder, create_dispimpl, create_vtbl_mapping +from comtypes.automation import DISPID, DISPPARAMS, EXCEPINFO, VARIANT from comtypes.errorinfo import ISupportErrorInfo from comtypes.typeinfo import IProvideClassInfo, IProvideClassInfo2, ITypeInfo @@ -51,9 +54,10 @@ ################################################################ +_kernel32_from_windll = WinDLL("kernel32") try: - _InterlockedIncrement = windll.kernel32.InterlockedIncrement - _InterlockedDecrement = windll.kernel32.InterlockedDecrement + _InterlockedIncrement = _kernel32_from_windll.InterlockedIncrement + _InterlockedDecrement = _kernel32_from_windll.InterlockedDecrement except AttributeError: import threading @@ -82,14 +86,69 @@ def _InterlockedDecrement(ob: c_long) -> int: _InterlockedIncrement.restype = c_long _InterlockedDecrement.restype = c_long +_oleaut32_from_windll = WinDLL("oleaut32") + +_DispGetIDsOfNames = _oleaut32_from_windll.DispGetIDsOfNames +_DispGetIDsOfNames.argtypes = [ + POINTER(ITypeInfo), + POINTER(c_wchar_p), + UINT, + POINTER(DISPID), +] +_DispGetIDsOfNames.restype = ( + LONG # technically, it is a HRESULT, but we want to avoid the OSError +) + +_DispInvoke = _oleaut32_from_windll.DispInvoke +_DispInvoke.argtypes = [ + LPVOID, + POINTER(ITypeInfo), + DISPID, + WORD, + POINTER(DISPPARAMS), + POINTER(VARIANT), + POINTER(EXCEPINFO), + POINTER(UINT), +] +_DispInvoke.restype = ( + LONG # technically, it is a HRESULT, but we want to avoid the OSError +) + + +_ole32_from_windll = WinDLL("ole32") +_ole32_from_oledll = OleDLL("ole32") + +_CoInitialize = _ole32_from_windll.CoInitialize +_CoInitialize.argtypes = [LPVOID] +_CoInitialize.restype = ( + LONG # technically, it is a HRESULT, but we want to avoid the OSError +) + +_CoUninitialize = _ole32_from_windll.CoUninitialize +_CoUninitialize.argtypes = [] +_CoUninitialize.restype = None + +_CoAddRefServerProcess = _ole32_from_oledll.CoAddRefServerProcess +_CoAddRefServerProcess.argtypes = [] +_CoAddRefServerProcess.restype = ULONG + +_CoReleaseServerProcess = _ole32_from_oledll.CoReleaseServerProcess +_CoReleaseServerProcess.argtypes = [] +_CoReleaseServerProcess.restype = ULONG + + +_user32_from_windll = WinDLL("user32") + +_PostQuitMessage = _user32_from_windll.PostQuitMessage +_PostQuitMessage.argtypes = [INT] +_PostQuitMessage.restype = None + class LocalServer(object): _queue: Optional[queue.Queue] = None def run(self, classobjects: Sequence["hints.localserver.ClassFactory"]) -> None: - # Use windll instead of oledll so that we don't get an - # exception on a FAILED hresult: - hr = windll.ole32.CoInitialize(None) + hr = _CoInitialize(None) if hresult.RPC_E_CHANGED_MODE == hr: # we're running in MTA: no message pump needed _debug("Server running in MTA") @@ -100,7 +159,7 @@ def run(self, classobjects: Sequence["hints.localserver.ClassFactory"]) -> None: if hr >= 0: # we need a matching CoUninitialize() call for a successful # CoInitialize(). - windll.ole32.CoUninitialize() + _CoUninitialize() self.run_sta() for obj in classobjects: @@ -116,15 +175,15 @@ def run_mta(self) -> None: self._queue.get() def Lock(self) -> None: - oledll.ole32.CoAddRefServerProcess() + _CoAddRefServerProcess() def Unlock(self) -> None: - rc = oledll.ole32.CoReleaseServerProcess() + rc = _CoReleaseServerProcess() if rc == 0: if self._queue: self._queue.put(42) else: - windll.user32.PostQuitMessage(0) + _PostQuitMessage(0) class InprocServer(object): @@ -401,7 +460,7 @@ def IDispatch_GetIDsOfNames(self, this, riid, rgszNames, cNames, lcid, rgDispId) tinfo = self.__typeinfo except AttributeError: return hresult.E_NOTIMPL - return windll.oleaut32.DispGetIDsOfNames(tinfo, rgszNames, cNames, rgDispId) + return _DispGetIDsOfNames(tinfo, rgszNames, cNames, rgDispId) def IDispatch_Invoke( self, @@ -432,7 +491,7 @@ def IDispatch_Invoke( # an error. interface = self._com_interfaces_[0] ptr = self._com_pointers_[interface._iid_] - return windll.oleaut32.DispInvoke( + return _DispInvoke( ptr, tinfo, dispIdMember, From 0ccc4d1b9bd9f1045c86f4029e0c5715f873d5f4 Mon Sep 17 00:00:00 2001 From: moi15moi <80980684+moi15moi@users.noreply.github.com> Date: Wed, 22 Jan 2025 08:27:31 -0500 Subject: [PATCH 2/6] Rename _kernel32_from_windll to _kernel32 --- comtypes/_comobject.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/comtypes/_comobject.py b/comtypes/_comobject.py index 150d5955..ec216413 100644 --- a/comtypes/_comobject.py +++ b/comtypes/_comobject.py @@ -54,10 +54,10 @@ ################################################################ -_kernel32_from_windll = WinDLL("kernel32") +_kernel32 = WinDLL("kernel32") try: - _InterlockedIncrement = _kernel32_from_windll.InterlockedIncrement - _InterlockedDecrement = _kernel32_from_windll.InterlockedDecrement + _InterlockedIncrement = _kernel32.InterlockedIncrement + _InterlockedDecrement = _kernel32.InterlockedDecrement except AttributeError: import threading From c77900a0fbbae8092570367c1e8f9728f184eb80 Mon Sep 17 00:00:00 2001 From: moi15moi <80980684+moi15moi@users.noreply.github.com> Date: Wed, 22 Jan 2025 08:28:07 -0500 Subject: [PATCH 3/6] Rename _oleaut32_from_windll to _oleaut32 --- comtypes/_comobject.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/comtypes/_comobject.py b/comtypes/_comobject.py index ec216413..61eba4e3 100644 --- a/comtypes/_comobject.py +++ b/comtypes/_comobject.py @@ -86,9 +86,9 @@ def _InterlockedDecrement(ob: c_long) -> int: _InterlockedIncrement.restype = c_long _InterlockedDecrement.restype = c_long -_oleaut32_from_windll = WinDLL("oleaut32") +_oleaut32 = WinDLL("oleaut32") -_DispGetIDsOfNames = _oleaut32_from_windll.DispGetIDsOfNames +_DispGetIDsOfNames = _oleaut32.DispGetIDsOfNames _DispGetIDsOfNames.argtypes = [ POINTER(ITypeInfo), POINTER(c_wchar_p), @@ -99,7 +99,7 @@ def _InterlockedDecrement(ob: c_long) -> int: LONG # technically, it is a HRESULT, but we want to avoid the OSError ) -_DispInvoke = _oleaut32_from_windll.DispInvoke +_DispInvoke = _oleaut32.DispInvoke _DispInvoke.argtypes = [ LPVOID, POINTER(ITypeInfo), From af044ce293e3b258f77d52921e3d38eee47cd70f Mon Sep 17 00:00:00 2001 From: moi15moi <80980684+moi15moi@users.noreply.github.com> Date: Wed, 22 Jan 2025 08:28:31 -0500 Subject: [PATCH 4/6] Rename _user32_from_windll to _user32 --- comtypes/_comobject.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/comtypes/_comobject.py b/comtypes/_comobject.py index 61eba4e3..f8260917 100644 --- a/comtypes/_comobject.py +++ b/comtypes/_comobject.py @@ -137,9 +137,9 @@ def _InterlockedDecrement(ob: c_long) -> int: _CoReleaseServerProcess.restype = ULONG -_user32_from_windll = WinDLL("user32") +_user32 = WinDLL("user32") -_PostQuitMessage = _user32_from_windll.PostQuitMessage +_PostQuitMessage = _user32.PostQuitMessage _PostQuitMessage.argtypes = [INT] _PostQuitMessage.restype = None From 5f42d2718d88be62ee1e4e20155d71d9d23dfd6d Mon Sep 17 00:00:00 2001 From: moi15moi <80980684+moi15moi@users.noreply.github.com> Date: Wed, 22 Jan 2025 08:29:20 -0500 Subject: [PATCH 5/6] Rename _ole32_from_windll to _ole32_nohresult --- comtypes/_comobject.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/comtypes/_comobject.py b/comtypes/_comobject.py index f8260917..5c10fb26 100644 --- a/comtypes/_comobject.py +++ b/comtypes/_comobject.py @@ -115,16 +115,16 @@ def _InterlockedDecrement(ob: c_long) -> int: ) -_ole32_from_windll = WinDLL("ole32") +_ole32_nohresult = WinDLL("ole32") _ole32_from_oledll = OleDLL("ole32") -_CoInitialize = _ole32_from_windll.CoInitialize +_CoInitialize = _ole32_nohresult.CoInitialize _CoInitialize.argtypes = [LPVOID] _CoInitialize.restype = ( LONG # technically, it is a HRESULT, but we want to avoid the OSError ) -_CoUninitialize = _ole32_from_windll.CoUninitialize +_CoUninitialize = _ole32_nohresult.CoUninitialize _CoUninitialize.argtypes = [] _CoUninitialize.restype = None From 7eaed94db5c73c5cca3a84560df5cfb6c8913b57 Mon Sep 17 00:00:00 2001 From: moi15moi <80980684+moi15moi@users.noreply.github.com> Date: Wed, 22 Jan 2025 08:29:43 -0500 Subject: [PATCH 6/6] Rename _ole32_from_oledll to _ole32 --- comtypes/_comobject.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/comtypes/_comobject.py b/comtypes/_comobject.py index 5c10fb26..e808b644 100644 --- a/comtypes/_comobject.py +++ b/comtypes/_comobject.py @@ -116,7 +116,7 @@ def _InterlockedDecrement(ob: c_long) -> int: _ole32_nohresult = WinDLL("ole32") -_ole32_from_oledll = OleDLL("ole32") +_ole32 = OleDLL("ole32") _CoInitialize = _ole32_nohresult.CoInitialize _CoInitialize.argtypes = [LPVOID] @@ -128,11 +128,11 @@ def _InterlockedDecrement(ob: c_long) -> int: _CoUninitialize.argtypes = [] _CoUninitialize.restype = None -_CoAddRefServerProcess = _ole32_from_oledll.CoAddRefServerProcess +_CoAddRefServerProcess = _ole32.CoAddRefServerProcess _CoAddRefServerProcess.argtypes = [] _CoAddRefServerProcess.restype = ULONG -_CoReleaseServerProcess = _ole32_from_oledll.CoReleaseServerProcess +_CoReleaseServerProcess = _ole32.CoReleaseServerProcess _CoReleaseServerProcess.argtypes = [] _CoReleaseServerProcess.restype = ULONG