Skip to content

Commit

Permalink
[_post_coinit\misc.py] Use WinDLL instead of oledll/windll
Browse files Browse the repository at this point in the history
Partially fix enthought#735
  • Loading branch information
moi15moi committed Jan 12, 2025
1 parent 5f64ea9 commit 5314443
Showing 1 changed file with 52 additions and 13 deletions.
65 changes: 52 additions & 13 deletions comtypes/_post_coinit/misc.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
from ctypes import c_ulong, c_ushort, c_void_p, c_wchar_p, HRESULT, Structure
from ctypes import byref, cast, _Pointer, POINTER, pointer
from ctypes import (
HRESULT,
POINTER,
Structure,
WinDLL,
_Pointer,
byref,
c_ulong,
c_ushort,
c_void_p,
c_wchar_p,
cast,
pointer
)
from ctypes.wintypes import DWORD, LPCWSTR, LPVOID

# fmt: off
from typing import ( # noqa
Any, overload, TYPE_CHECKING, TypeVar,
# instead of `builtins`. see PEP585
Type,
from typing import ( # noqa
TYPE_CHECKING,
Any,
# instead of `collections.abc`. see PEP585
Callable,
# instead of `A | B` and `None | A`. see PEP604
Optional,
# instead of `builtins`. see PEP585
Type,
TypeVar,
overload
)
# fmt: on
if TYPE_CHECKING:
from comtypes import hints as hints # noqa # type: ignore

from comtypes import GUID
from comtypes import CLSCTX_SERVER, CLSCTX_LOCAL_SERVER, CLSCTX_REMOTE_SERVER
from comtypes import _ole32, oledll, DWORD
from comtypes import (
CLSCTX_LOCAL_SERVER,
CLSCTX_REMOTE_SERVER,
CLSCTX_SERVER,
GUID
)
from comtypes._memberspec import COMMETHOD
from comtypes._post_coinit.unknwn import IUnknown

Expand Down Expand Up @@ -98,7 +117,7 @@ def CoGetObject(displayname: str, interface: Optional[Type[IUnknown]]) -> IUnkno
interface = IUnknown
punk = POINTER(interface)()
# Do we need a way to specify the BIND_OPTS parameter?
_ole32.CoGetObject(str(displayname), None, byref(interface._iid_), byref(punk))
_CoGetObject(str(displayname), None, byref(interface._iid_), byref(punk))
return punk # type: ignore


Expand Down Expand Up @@ -134,7 +153,7 @@ def CoCreateInstance(
interface = IUnknown
p = POINTER(interface)()
iid = interface._iid_
_ole32.CoCreateInstance(byref(clsid), punkouter, clsctx, byref(iid), byref(p))
_CoCreateInstance(byref(clsid), punkouter, clsctx, byref(iid), byref(p))
return p # type: ignore


Expand Down Expand Up @@ -178,7 +197,7 @@ def GetActiveObject(
) -> IUnknown:
"""Retrieves a pointer to a running object"""
p = POINTER(IUnknown)()
oledll.oleaut32.GetActiveObject(byref(clsid), None, byref(p))
_GetActiveObject(byref(clsid), None, byref(p))
if interface is not None:
p = p.QueryInterface(interface) # type: ignore
return p # type: ignore
Expand Down Expand Up @@ -235,6 +254,15 @@ class _COSERVERINFO(Structure):
pAuthInfo: _COAUTHINFO
dwReserved2: int

_oleaut32 = WinDLL("oleaut32")

REFCLSID = POINTER(GUID)
_GetActiveObject = _oleaut32.GetActiveObject
_GetActiveObject.argtypes = [REFCLSID, LPVOID, POINTER(POINTER(IUnknown))]
_GetActiveObject.restype = HRESULT


_ole32 = WinDLL("ole32")

COSERVERINFO = _COSERVERINFO
_CoGetClassObject = _ole32.CoGetClassObject
Expand All @@ -245,7 +273,15 @@ class _COSERVERINFO(Structure):
POINTER(GUID),
POINTER(c_void_p),
]
_CoGetClassObject.restype = HRESULT

_CoCreateInstance = _ole32.CoCreateInstance
_CoCreateInstance.argtypes = [REFCLSID, POINTER(IUnknown), DWORD, POINTER(GUID), POINTER(LPVOID)]
_CoCreateInstance.restype = HRESULT

_CoCreateInstanceEx = _ole32.CoCreateInstanceEx
_CoCreateInstanceEx.argtypes = [REFCLSID, POINTER(IUnknown), DWORD, POINTER(COSERVERINFO), DWORD, POINTER(MULTI_QI)]
_CoCreateInstanceEx.restype = HRESULT

class tagBIND_OPTS(Structure):
_fields_ = [
Expand All @@ -258,6 +294,9 @@ class tagBIND_OPTS(Structure):

# XXX Add __init__ which sets cbStruct?
BIND_OPTS = tagBIND_OPTS
_CoGetObject = _ole32.CoGetObject
_CoGetObject.argtypes = [LPCWSTR, POINTER(BIND_OPTS), POINTER(GUID), POINTER(LPVOID)]
_CoGetObject.restype = HRESULT


class tagBIND_OPTS2(Structure):
Expand Down Expand Up @@ -360,7 +399,7 @@ def CoCreateInstanceEx(
interface = IUnknown
multiqi = MULTI_QI()
multiqi.pIID = pointer(interface._iid_) # type: ignore
_ole32.CoCreateInstanceEx(
_CoCreateInstanceEx(
byref(clsid), None, clsctx, pServerInfo, 1, byref(multiqi)
)
return cast(multiqi.pItf, POINTER(interface)) # type: ignore

0 comments on commit 5314443

Please sign in to comment.