def __init__(self): super(ConnectableObjectMixin, self).__init__() self.__connections = {} tlib = LoadRegTypeLib(*self._reg_typelib_) for itf in self._outgoing_interfaces_: typeinfo = tlib.GetTypeInfoOfGuid(itf._iid_) self.__connections[itf] = ConnectionPointImpl(itf, typeinfo)
def test_com_refcounts(self): # typelib for oleaut32 tlb = LoadRegTypeLib(GUID("{00020430-0000-0000-C000-000000000046}"), 2, 0, 0) rc = get_refcnt(tlb) p = tlb.QueryInterface(IUnknown) self.failUnlessEqual(get_refcnt(tlb), rc+1) del p self.failUnlessEqual(get_refcnt(tlb), rc)
def test_LoadTypeLibEx(self): # IE 6 uses shdocvw.dll, IE 7 uses ieframe.dll if os.path.exists( os.path.join(os.environ["SystemRoot"], "system32", "ieframe.dll")): dllname = "ieframe.dll" else: dllname = "shdocvw.dll" self.assertRaises(WindowsError, lambda: LoadTypeLibEx("<xxx.xx>")) tlib = LoadTypeLibEx(dllname) self.assertTrue(tlib.GetTypeInfoCount()) tlib.GetDocumentation(-1) self.assertEqual(tlib.IsName("iwebbrowser"), "IWebBrowser") self.assertEqual(tlib.IsName("IWEBBROWSER"), "IWebBrowser") self.assertTrue(tlib.FindName("IWebBrowser")) self.assertEqual(tlib.IsName("Spam"), None) tlib.GetTypeComp() attr = tlib.GetLibAttr() info = attr.guid, attr.wMajorVerNum, attr.wMinorVerNum other_tlib = LoadRegTypeLib(*info) self.assertEqual(tlib, other_tlib) ## for n in dir(attr): ## if not n.startswith("_"): ## print "\t", n, getattr(attr, n) for i in range(tlib.GetTypeInfoCount()): ti = tlib.GetTypeInfo(i) ti.GetTypeAttr() tlib.GetDocumentation(i) tlib.GetTypeInfoType(i) c_tlib, index = ti.GetContainingTypeLib() self.assertEqual(c_tlib, tlib) self.assertEqual(index, i) guid_null = GUID() self.assertRaises(COMError, lambda: tlib.GetTypeInfoOfGuid(guid_null)) self.assertTrue( tlib.GetTypeInfoOfGuid( GUID("{EAB22AC1-30C1-11CF-A7EB-0000C05BAE0B}"))) path = QueryPathOfRegTypeLib(*info) path = path.split("\0")[0] self.assertTrue(path.lower().endswith(dllname))
def test_com_pointers(self): # Storing a COM interface pointer in a VARIANT increments the refcount, # changing the variant to contain something else decrements it tlb = LoadRegTypeLib(GUID("{00020430-0000-0000-C000-000000000046}"), 2, 0, 0) rc = get_refcnt(tlb) v = VARIANT(tlb) self.failUnlessEqual(get_refcnt(tlb), rc+1) p = v.value self.failUnlessEqual(get_refcnt(tlb), rc+2) del p self.failUnlessEqual(get_refcnt(tlb), rc+1) v.value = None self.failUnlessEqual(get_refcnt(tlb), rc)
def __prepare_comobject(self): # When a CoClass instance is created, COM pointers to all # interfaces are created. Also, the CoClass must be kept alive as # until the COM reference count drops to zero, even if no Python # code keeps a reference to the object. # # The _com_pointers_ instance variable maps string interface iids # to C compatible COM pointers. self._com_pointers_ = {} # COM refcount starts at zero. self._refcnt = c_long(0) # Some interfaces have a default implementation in COMObject: # - ISupportErrorInfo # - IPersist (if the subclass has a _reg_clsid_ attribute) # - IProvideClassInfo (if the subclass has a _reg_clsid_ attribute) # - IProvideClassInfo2 (if the subclass has a _outgoing_interfaces_ # attribute) # # Add these if they are not listed in _com_interfaces_. interfaces = tuple(self._com_interfaces_) if ISupportErrorInfo not in interfaces: interfaces += (ISupportErrorInfo, ) if hasattr(self, "_reg_typelib_"): from comtypes.typeinfo import LoadRegTypeLib self._COMObject__typelib = LoadRegTypeLib(*self._reg_typelib_) if hasattr(self, "_reg_clsid_"): if IProvideClassInfo not in interfaces: interfaces += (IProvideClassInfo, ) if hasattr(self, "_outgoing_interfaces_") and \ IProvideClassInfo2 not in interfaces: interfaces += (IProvideClassInfo2, ) if hasattr(self, "_reg_clsid_"): if IPersist not in interfaces: interfaces += (IPersist, ) for itf in interfaces[::-1]: self.__make_interface_pointer(itf)