Skip to content

Commit

Permalink
Add type hints to server/__init__.py. (#707)
Browse files Browse the repository at this point in the history
* Improve import section.

* Add type hints to `RegisterActiveObject`.

* Add type hints to `RevokeActiveObject`.

* `comtypes.IUnknown` -> `IUnknown`

* `comtypes.GUID` -> `GUID`

* `comtypes.STDMETHOD` -> `STDMETHOD`

* `comtypes.HRESULT` -> `HRESULT`

* `ctypes.POINTER` -> `POINTER`

* `ctypes.byref` -> `byref`

* Add `TYPE_CHECKING` only `LockServer` method definition to `IClassFactory`.

* Add type hints to `IClassFactory.CreateInstance`.

* `comtypes.automation.IDispatch` -> `IDispatch`

* Add `import comtypes.client.dynamic`.

* Rename internal variables.
  • Loading branch information
junkmd authored Dec 18, 2024
1 parent ee8664e commit 1439e3b
Showing 1 changed file with 39 additions and 23 deletions.
62 changes: 39 additions & 23 deletions comtypes/server/__init__.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,48 @@
import comtypes.client, ctypes
import ctypes
from ctypes import HRESULT, POINTER, byref
from typing import TYPE_CHECKING, Any, Optional, Type

import comtypes
import comtypes.client
import comtypes.client.dynamic
from comtypes import GUID, STDMETHOD, IUnknown
from comtypes.automation import IDispatch

if TYPE_CHECKING:
from ctypes import _Pointer

from comtypes import hints # type: ignore


################################################################
# Interfaces
class IClassFactory(comtypes.IUnknown):
_iid_ = comtypes.GUID("{00000001-0000-0000-C000-000000000046}")
class IClassFactory(IUnknown):
_iid_ = GUID("{00000001-0000-0000-C000-000000000046}")
_methods_ = [
comtypes.STDMETHOD(
comtypes.HRESULT,
STDMETHOD(
HRESULT,
"CreateInstance",
[
ctypes.POINTER(comtypes.IUnknown),
ctypes.POINTER(comtypes.GUID),
ctypes.POINTER(ctypes.c_void_p),
],
[POINTER(IUnknown), POINTER(GUID), POINTER(ctypes.c_void_p)],
),
comtypes.STDMETHOD(comtypes.HRESULT, "LockServer", [ctypes.c_int]),
STDMETHOD(HRESULT, "LockServer", [ctypes.c_int]),
]

def CreateInstance(self, punkouter=None, interface=None, dynamic=False):
def CreateInstance(
self,
punkouter: Optional[Type["_Pointer[IUnknown]"]] = None,
interface: Optional[Type[IUnknown]] = None,
dynamic: bool = False,
) -> Any:
if dynamic:
if interface is not None:
raise ValueError("interface and dynamic are mutually exclusive")
realInterface = comtypes.automation.IDispatch
itf = IDispatch
elif interface is None:
realInterface = comtypes.IUnknown
itf = IUnknown
else:
realInterface = interface
obj = ctypes.POINTER(realInterface)()
self.__com_CreateInstance(punkouter, realInterface._iid_, ctypes.byref(obj))
itf = interface
obj = POINTER(itf)()
self.__com_CreateInstance(punkouter, itf._iid_, byref(obj)) # type: ignore
if dynamic:
return comtypes.client.dynamic.Dispatch(obj)
elif interface is None:
Expand All @@ -37,6 +51,10 @@ def CreateInstance(self, punkouter=None, interface=None, dynamic=False):
# An interface was specified and obj is already that interface.
return obj

if TYPE_CHECKING:

def LockServer(self, fLock: int) -> hints.Hresult: ...


# class IExternalConnection(IUnknown):
# _iid_ = GUID("{00000019-0000-0000-C000-000000000046}")
Expand All @@ -52,19 +70,17 @@ def CreateInstance(self, punkouter=None, interface=None, dynamic=False):
oleaut32 = ctypes.oledll.oleaut32


def RegisterActiveObject(comobj, weak=True):
punk = comobj._com_pointers_[comtypes.IUnknown._iid_]
def RegisterActiveObject(comobj: comtypes.COMObject, weak: bool = True) -> int:
punk = comobj._com_pointers_[IUnknown._iid_]
clsid = comobj._reg_clsid_
if weak:
flags = ACTIVEOBJECT_WEAK
else:
flags = ACTIVEOBJECT_STRONG
handle = ctypes.c_ulong()
oleaut32.RegisterActiveObject(
punk, ctypes.byref(clsid), flags, ctypes.byref(handle)
)
oleaut32.RegisterActiveObject(punk, byref(clsid), flags, byref(handle))
return handle.value


def RevokeActiveObject(handle):
def RevokeActiveObject(handle: int) -> None:
oleaut32.RevokeActiveObject(handle, None)

0 comments on commit 1439e3b

Please sign in to comment.