IUnknown

All COM interfaces are derived from an interface called IUnknown. The IUnknown interface provides DirectX with control of the object's lifetime, and the ability to navigate multiple interfaces. IUnknown has only three methods:

·AddRef, which increments the reference count of the object by 1 when an interface or another application binds itself to the object.

·Release, which decrements the reference count of the object by 1. When the count reaches 0, the object is deallocated.

·QueryInterface, which queries the object about the features it supports by asking for pointers to a specific interface.

AddRef and Release maintain the reference count of a particular object. For example, if you create a DirectDrawSurface object, the reference count of the object is set to 1. Each time a function returns a pointer to an interface for that object, the function must then call AddRef through that pointer to increment the reference count. All AddRef calls must be matched with a call to Release. Before the pointer can be destroyed, you must call Release through that pointer. Once the reference count of a particular object reaches 0, the object is destroyed and all interfaces to that object are then invalid.

QueryInterface determines whether an object supports a specific interface. If the interface is supported, QueryInterface returns a pointer to that particular interface. You can then use the methods contained in that interface to communicate with the object. If QueryInterface successfully returns a pointer to an interface, it implicitly calls AddRef to increment the reference count, so your application must call Release to decrement the reference count before destroying the pointer to the interface.