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:
QueryInterface
AddRef
Release
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 ...
Get VB Shell Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.