def test_identity(self): # COM indentity rules # these should be identical a = POINTER(IUnknown)() b = POINTER(IUnknown)() self.assertEqual(a, b) self.assertEqual(hash(a), hash(b)) from comtypes.typeinfo import CreateTypeLib # we do not save the lib, so no file will be created. # these should NOT be identical a = CreateTypeLib("blahblah") b = CreateTypeLib("spam") self.assertNotEqual(a, b) self.assertNotEqual(hash(a), hash(b)) a = a.QueryInterface(IUnknown) b = b.QueryInterface(IUnknown) self.assertNotEqual(a, b) self.assertNotEqual(hash(a), hash(b)) # These must be identical c = a.QueryInterface(IUnknown) self.assertEqual(a, c) self.assertEqual(hash(a), hash(c))
def test_VT_UNKNOWN_multi(self): a = _midlSAFEARRAY(POINTER(IUnknown)) t = _midlSAFEARRAY(POINTER(IUnknown)) self.failUnless(a is t) def com_refcnt(o): "Return the COM refcount of an interface pointer" import gc gc.collect() gc.collect() o.AddRef() return o.Release() from comtypes.typeinfo import CreateTypeLib, ICreateTypeLib punk = CreateTypeLib("spam").QueryInterface( IUnknown) # will never be saved to disk # initial refcount initial = com_refcnt(punk) # This should increase the refcount by 4 sa = t.from_param((punk, ) * 4) self.failUnlessEqual(initial + 4, com_refcnt(punk)) # Unpacking the array must not change the refcount, and must # return an equal object. self.failUnlessEqual((punk, ) * 4, sa[0]) self.failUnlessEqual(initial + 4, com_refcnt(punk)) del sa self.failUnlessEqual(initial, com_refcnt(punk)) # This should increase the refcount by 2 sa = t.from_param((punk, None, punk, None)) self.failUnlessEqual(initial + 2, com_refcnt(punk)) null = POINTER(IUnknown)() self.failUnlessEqual((punk, null, punk, null), sa[0]) del sa self.failUnlessEqual(initial, com_refcnt(punk)) # repeat same test, with 2 different com pointers plib = CreateTypeLib("foo") a, b = com_refcnt(plib), com_refcnt(punk) sa = t.from_param([plib, punk, plib]) #### self.failUnlessEqual((plib, punk, plib), sa[0]) self.failUnlessEqual((a + 2, b + 1), (com_refcnt(plib), com_refcnt(punk))) del sa self.failUnlessEqual((a, b), (com_refcnt(plib), com_refcnt(punk)))
def test_VT_UNKNOWN_1(self): a = _midlSAFEARRAY(POINTER(IUnknown)) t = _midlSAFEARRAY(POINTER(IUnknown)) self.failUnless(a is t) def com_refcnt(o): "Return the COM refcount of an interface pointer" import gc gc.collect() gc.collect() o.AddRef() return o.Release() from comtypes.typeinfo import CreateTypeLib, ICreateTypeLib punk = CreateTypeLib("spam").QueryInterface( IUnknown) # will never be saved to disk # initial refcount initial = com_refcnt(punk) # This should increase the refcount by 1 sa = t.from_param([punk]) self.failUnlessEqual(initial + 1, com_refcnt(punk)) # Unpacking the array must not change the refcount, and must # return an equal object. self.failUnlessEqual((punk, ), sa[0]) self.failUnlessEqual(initial + 1, com_refcnt(punk)) del sa self.failUnlessEqual(initial, com_refcnt(punk)) sa = t.from_param([None]) self.failUnlessEqual((POINTER(IUnknown)(), ), sa[0])
def test_VT_UNKNOWN_1(self): a = _midlSAFEARRAY(POINTER(IUnknown)) t = _midlSAFEARRAY(POINTER(IUnknown)) self.assertTrue(a is t) from comtypes.typeinfo import CreateTypeLib # will never be saved to disk punk = CreateTypeLib("spam").QueryInterface(IUnknown) # initial refcount initial = com_refcnt(punk) # This should increase the refcount by 1 sa = t.from_param([punk]) self.assertEqual(initial + 1, com_refcnt(punk)) # Unpacking the array must not change the refcount, and must # return an equal object. self.assertEqual((punk, ), sa[0]) self.assertEqual(initial + 1, com_refcnt(punk)) del sa self.assertEqual(initial, com_refcnt(punk)) sa = t.from_param([None]) self.assertEqual((POINTER(IUnknown)(), ), sa[0])
def test_VT_UNKNOWN_multi_ndarray(self): np = get_numpy() if np is None: return a = _midlSAFEARRAY(POINTER(IUnknown)) t = _midlSAFEARRAY(POINTER(IUnknown)) self.failUnless(a is t) def com_refcnt(o): "Return the COM refcount of an interface pointer" import gc gc.collect() gc.collect() o.AddRef() return o.Release() from comtypes.typeinfo import CreateTypeLib, ICreateTypeLib punk = CreateTypeLib("spam").QueryInterface( IUnknown) # will never be saved to disk # initial refcount initial = com_refcnt(punk) # This should increase the refcount by 4 sa = t.from_param((punk, ) * 4) self.failUnlessEqual(initial + 4, com_refcnt(punk)) # Unpacking the array must not change the refcount, and must # return an equal object. Creating an ndarray may change the # refcount. arr = get_array(sa) self.failUnless(isinstance(arr, np.ndarray)) self.failUnlessEqual(np.dtype(object), arr.dtype) self.failUnless((arr == (punk, ) * 4).all()) self.failUnlessEqual(initial + 8, com_refcnt(punk)) del arr self.failUnlessEqual(initial + 4, com_refcnt(punk)) del sa self.failUnlessEqual(initial, com_refcnt(punk)) # This should increase the refcount by 2 sa = t.from_param((punk, None, punk, None)) self.failUnlessEqual(initial + 2, com_refcnt(punk)) null = POINTER(IUnknown)() arr = get_array(sa) self.failUnless(isinstance(arr, np.ndarray)) self.failUnlessEqual(np.dtype(object), arr.dtype) self.failUnless((arr == (punk, null, punk, null)).all()) del sa del arr self.failUnlessEqual(initial, com_refcnt(punk))
# It was a pain to get this CLSID: it's neither in the registry, nor # in any header files. I had to compile a C program, and find it out # with the debugger. Apparently it is in uuid.lib. CLSID_StdGlobalInterfaceTable = GUID("{00000323-0000-0000-C000-000000000046}") git = CoCreateInstance(CLSID_StdGlobalInterfaceTable, interface=IGlobalInterfaceTable, clsctx=CLSCTX_INPROC_SERVER) RevokeInterfaceFromGlobal = git.RevokeInterfaceFromGlobal RegisterInterfaceInGlobal = git.RegisterInterfaceInGlobal GetInterfaceFromGlobal = git.GetInterfaceFromGlobal __all__ = ["RegisterInterfaceInGlobal", "RevokeInterfaceFromGlobal", "GetInterfaceFromGlobal"] 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)