Skip to content

Commit

Permalink
[__init__.py] Use WinDLL/OleDLL instead of windll/oledll (#770)
Browse files Browse the repository at this point in the history
* [__init__.py] Use WinDLL/OleDLL instead of windll/oledll

* [_comobject.py] Import _CoUninitialize

* [_post_coinit\unknwn.py] Import _CoUninitialize
  • Loading branch information
moi15moi authored Jan 26, 2025
1 parent 9e9a8aa commit f05c70d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
22 changes: 16 additions & 6 deletions comtypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -156,7 +166,7 @@ def CoInitializeEx(flags=None):
# in which we are using COM
def CoUninitialize():
logger.debug("CoUninitialize()")
_ole32_nohresult.CoUninitialize()
_CoUninitialize()


################################################################
Expand Down
6 changes: 1 addition & 5 deletions comtypes/_comobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions comtypes/_post_coinit/unknwn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,7 +17,7 @@


def _shutdown(
func=_ole32_nohresult.CoUninitialize,
func=_CoUninitialize,
_debug=logger.debug,
_exc_clear=getattr(sys, "exc_clear", lambda: None),
):
Expand Down

0 comments on commit f05c70d

Please sign in to comment.