diff --git a/comtypes/server/localserver.py b/comtypes/server/localserver.py index 62907792..6ecbb19c 100644 --- a/comtypes/server/localserver.py +++ b/comtypes/server/localserver.py @@ -1,11 +1,13 @@ import logging import queue -import sys -from ctypes import byref, c_ulong, c_void_p, oledll +from ctypes import HRESULT, POINTER, OleDLL, Structure, byref, c_ulong, c_void_p +from ctypes.wintypes import DWORD, LPDWORD from typing import TYPE_CHECKING, Any, Literal, Optional, Sequence, Type import comtypes from comtypes import hresult +from comtypes._post_coinit.unknwn import IUnknown +from comtypes.GUID import GUID from comtypes.server import IClassFactory if TYPE_CHECKING: @@ -21,6 +23,17 @@ REGCLS_SUSPENDED = 4 # register it as suspended, will be activated REGCLS_SURROGATE = 8 # must be used when a surrogate process +_ole32 = OleDLL("ole32") + +REFCLSID = POINTER(GUID) +_CoRegisterClassObject = _ole32.CoRegisterClassObject +_CoRegisterClassObject.argtypes = [REFCLSID, POINTER(Structure), DWORD, DWORD, LPDWORD] +_CoRegisterClassObject.restype = HRESULT + +_CoRevokeClassObject = _ole32.CoRevokeClassObject +_CoRevokeClassObject.argtypes = [DWORD] +_CoRevokeClassObject.restype = HRESULT + def run(classes: Sequence[Type[comtypes.COMObject]]) -> None: classobjects = [ClassFactory(cls) for cls in classes] @@ -52,7 +65,7 @@ def _register_class(self) -> None: ptr = self._com_pointers_[comtypes.IUnknown._iid_] clsctx = self._cls._reg_clsctx_ clsctx &= ~comtypes.CLSCTX_INPROC # reset the inproc flags - oledll.ole32.CoRegisterClassObject( + _CoRegisterClassObject( byref(comtypes.GUID(self._cls._reg_clsid_)), ptr, clsctx, @@ -62,7 +75,7 @@ def _register_class(self) -> None: self.cookie = cookie def _revoke_class(self) -> None: - oledll.ole32.CoRevokeClassObject(self.cookie) + _CoRevokeClassObject(self.cookie) def CreateInstance( self,