IUnknown
From
looking at the type library, you can see that
_Animal is derived from another interface called
IDispatch. What is not apparent is that
IDispatch is derived from yet another interface
called IUnknown. Actually, all interfaces are
ultimately derived from IUnknown. This means that
all COM components share a dependable commonality.
The IUnknown interface contains three methods:
QueryInterfaceAddRefRelease
QueryInterface
The purpose of
QueryInterface is to allow clients to discover
whether a component supports a given interface. It is also used to
navigate between interfaces on a given component. Before returning
the requested interface (if it exists), AddRef is
called to give the object a reference count.
AddRef and Release
AddRef
and Release are
used for reference counting. All objects in memory have an associated
reference count. Every time an object is created or copied, this
count is incremented by one. Every time an object is released, the
reference count is decremented by one. When the reference count is
zero, the object can safely unload itself. As a VB programmer, you
have seen this entire process many times in code fragments like the
following, probably without ever realizing precisely what was
happening behind the scenes:
Dim Cow1 As Animal 'QueryInterface Animal for Cow interface and call AddRef. 'Cow1 now has a reference count of one. Set Cow1 = New Cow Dim Cow2 As Cow 'AddRef is called. Reference count is two. Set Cow2 = Cow1 'Release Cow1. Reference ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access