import struct import ctypes import functools from ctypes.wintypes import HRESULT, byref, pointer, cast import windows from windows import winproxy from windows.generated_def.winstructs import * from windows.generated_def import RPC_C_IMP_LEVEL_IMPERSONATE, CLSCTX_INPROC_SERVER from windows.generated_def import interfaces from windows.generated_def.interfaces import generate_IID, IID # Simple raw -> UUID # "-".join("{:02X}".format(c) for c in struct.unpack("index is not exposed to Python.. # repr is: '' rpr = repr(method) if not rpr.startswith(" implementing <{1}> has no method <{2}>".format(type(self).__name__, self.IMPLEMENT.__name__, func_name)) if not callable(implem): raise ValueError("{0} implementing <{1}>: <{2}> is not callable".format(type(self).__name__, self.IMPLEMENT.__name__, func_name)) return True def _create_vtable(self, interface): implems = [] names = [] for index, name, method in self.extract_methods_order(interface): func_implem = getattr(self, name) #'this' is a COM-interface of the type we are implementing types = [method.restype, interface] + list(method.argtypes) implems.append(ctypes.WINFUNCTYPE(*types)(func_implem)) names.append(name) class Vtable(ctypes.Structure): _fields_ = [(name, ctypes.c_void_p) for name in names] return Vtable(*[ctypes.cast(x, ctypes.c_void_p) for x in implems]), implems def __init__(self): self.verify_implem(self.IMPLEMENT) vtable, implems = self._create_vtable(self.IMPLEMENT) self.vtable = vtable self.implems = implems self.vtable_pointer = ctypes.pointer(self.vtable) self._as_parameter_ = ctypes.addressof(self.vtable_pointer) def QueryInterface(self, this, piid, result): if piid[0] in (IUnknown.IID, self.IMPLEMENT.IID): result[0] = this return 1 return E_NOINTERFACE def AddRef(self, *args): return 1 def Release(self, *args): return 0