Interfaces and Objects
COM makes a clear distinction between
interfaces
and
objects
.
An interface describes certain functionality, while an object
implements that functionality (that is, implements the interface). An
interface describes how an object is to behave, while the object
itself implements the behavior. For example, COM defines an
IStream
interface, a generic interface for
reading and writing, in a manner similar to a file. Although COM
defines the IStream interface, it’s the
responsibility of objects to implement the interface; thus, you may
have an object that implements the IStream
interface writing to and from files or an object implementing the
IStream interface using sockets, and so forth.
This is a huge advantage to users of these interfaces, because you
can code to the IStream interface, and your code
works regardless of whether your data goes to a file or out over a
socket. Each COM interface has a unique 128-bit GUID known as an
interface
ID (IID).
An interface defines a series of methods: interfaces can’t have
properties. An interface is defined in terms of a C++
vtable
.
Highly experienced C++ programmers will know that a
vtable implements virtual methods in C++.
Just as with C++, COM allows one interface to derive from, or extend,
another interface; in fact, COM explicitly requires it. COM defines
an interface known as
IUnknown
, which is the root (or base) of all
COM interfaces; that is, all COM interfaces explicitly support the
IUnknown interface. IUnknown ...
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