The IUnknown interface lets clients get pointers to other interfaces on a given object through the method, and manage the existence of the object through the IUnknown::AddRef and IUnknown::Release methods. All other Component Object Model (COM) interfaces are inherited, directly or indirectly, from IUnknown. Therefore, the three methods in IUnknown are the first entries in the vtable for every interface.
This interface and its methods are fully described in the COM documentation and are only partially documented here for quick reference.
You must implement IUnknown as part of every interface. If you are using C++ multiple inheritance to implement multiple interfaces, the various interfaces can share one implementation of IUnknown. If you are using nested classes to implement multiple interfaces, you must implement IUnknown once for each interface you implement.
Note The IUnknown interface is implemented by the CUnknown base class in the Microsoft® DirectShow® class library and so is inherited by most other classes.
Use IUnknown methods to switch between interfaces on an object, add references, and release objects.
Methods in Vtable Order
IUnknown methods Description QueryInterface Retrieves pointers to supported interfaces. AddRef Increments the reference count. Release Decrements the reference count.
Increments the reference count for the calling interface on an object. It should be called for every new copy of a pointer to an interface on a given object.
Syntax
ULONG AddRef(void);
Return Value
Returns an integer from 1 to n, the value of the new reference count. This information is meant to be used for diagnostic/testing purposes only, because, in certain situations, the value might be unstable.
Retrieves a pointer to a specified interface on a component to which a client currently holds an interface pointer. This method must use IUnknown::AddRef on the pointer it returns.
Syntax
HRESULT QueryInterface(
REFIID iid,
void **ppvObject
);
Parameters
- iid
- [in] Value specifying the IID of the interface being requested.
- ppvObject
- [out] Address of a pointer to the object on return. If the object doesn't support the interface specified in iid, ppvObject is set to NULL.
Return Value
Returns S_OK if the interface is supported, S_FALSE if not.
Decrements the reference count for the calling interface on an object. If the reference count on the object falls to zero, the object is freed from memory.
Syntax
ULONG Release(void);
Return Value
Returns the resulting value of the reference count, which is used for diagnostic/testing purposes only. If you need to know that resources have been freed, use an interface with higher-level semantics.
Top of Page
© 2000 Microsoft and/or its suppliers. All rights reserved. Terms of Use.