Skip to content
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
41 changes: 15 additions & 26 deletions comtypes/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
between different threading appartments.
"""

from ctypes import *
from ctypes import POINTER
from ctypes.wintypes import DWORD
from typing import Type, TypeVar

from comtypes import (
CLSCTX_INPROC_SERVER,
COMMETHOD,
GUID,
HRESULT,
STDMETHOD,
CoCreateInstance,
IUnknown,
)

_T_IUnknown = TypeVar("_T_IUnknown", bound=IUnknown)


class IGlobalInterfaceTable(IUnknown):
_iid_ = GUID("{00000146-0000-0000-C000-000000000046}")
Expand All @@ -34,18 +36,22 @@ class IGlobalInterfaceTable(IUnknown):
),
]

def RegisterInterfaceInGlobal(self, obj, interface=IUnknown):
def RegisterInterfaceInGlobal(
self, obj: IUnknown, interface: Type[_T_IUnknown] = IUnknown
) -> int:
cookie = DWORD()
self.__com_RegisterInterfaceInGlobal(obj, interface._iid_, cookie)
self.__com_RegisterInterfaceInGlobal(obj, interface._iid_, cookie) # type: ignore
return cookie.value

def GetInterfaceFromGlobal(self, cookie, interface=IUnknown):
def GetInterfaceFromGlobal(
self, cookie: int, interface: Type[_T_IUnknown] = IUnknown
) -> _T_IUnknown:
ptr = POINTER(interface)()
self.__com_GetInterfaceFromGlobal(cookie, interface._iid_, ptr)
return ptr
self.__com_GetInterfaceFromGlobal(cookie, interface._iid_, ptr) # type: ignore
return ptr # type: ignore

def RevokeInterfaceFromGlobal(self, cookie):
self.__com_RevokeInterfaceFromGlobal(cookie)
def RevokeInterfaceFromGlobal(self, cookie: int) -> None:
self.__com_RevokeInterfaceFromGlobal(cookie) # type: ignore


# It was a pain to get this CLSID: it's neither in the registry, nor
Expand All @@ -69,20 +75,3 @@ def RevokeInterfaceFromGlobal(self, cookie):
"GetInterfaceFromGlobal",
]
# fmt: on


if __name__ == "__main__":
from comtypes.typeinfo import CreateTypeLib, ICreateTypeLib

tlib = CreateTypeLib("foo.bar") # we don not save it later
assert (tlib.AddRef(), tlib.Release()) == (2, 1)

cookie = RegisterInterfaceInGlobal(tlib)
assert (tlib.AddRef(), tlib.Release()) == (3, 2)

GetInterfaceFromGlobal(cookie, ICreateTypeLib)
GetInterfaceFromGlobal(cookie, ICreateTypeLib)
GetInterfaceFromGlobal(cookie)
assert (tlib.AddRef(), tlib.Release()) == (3, 2)
RevokeInterfaceFromGlobal(cookie)
assert (tlib.AddRef(), tlib.Release()) == (2, 1)
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ ignore = ["E402"]
"comtypes/_npsupport.py" = ["F401"]
"comtypes/_vtbl.py" = ["E722"]
"comtypes/automation.py" = ["F401", "F403", "F405"]
"comtypes/git.py" = ["F401", "F403", "F405"]
"comtypes/viewobject.py" = ["F403", "F405"]
"comtypes/client/_constants.py" = ["F401"]
"comtypes/server/automation.py" = ["F403", "F405"]
Expand Down
Loading