Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[__init__.py] Use WinDLL/OleDLL instead of windll/oledll #770

Merged
merged 3 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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