IDispatch, the Genesis of All Scriptable Interfaces

Every COM interface is derived from a base interface called IUnknown. This interface has three methods:

• QueryInterface

• AddRef

• Release

QueryInterface is used to request a different interface. Every interface has a unique ID that can be passed to QueryInterface. When you pass an interface ID to QueryInterface the requested interface, or pointer to the appropriate abstract base class, is returned. QueryInterface returns an error code that can be used to determine whether an object implements a given interface.

AddRef and Release are used to manage the life cycle of the COM object. Whenever you are given an interface—for example, by calling QueryInterface—AddRef is called. When you are finished with the interface, you must call Release before discarding it. This mechanism allows objects to use reference counting to determine when they are no longer used and can be destroyed.