18.04.2015 Views

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

THE MICROSOFT COMPONENT OBJECT MODEL<br />

An interface’s permanence is not restricted to<br />

simply its method signatures, but extends to its<br />

semantic behavior as well. For example, an<br />

interface defines two methods, A and B, with no<br />

restrictions placed on their use. It breaks the<br />

COM contract if, at a subsequent release,<br />

Method A requires that Method B be executed<br />

first. A change like this would force possible<br />

recompilations of clients.<br />

The name IUnknown came from a 1988<br />

internal Microsoft paper called Object Architecture:<br />

Dealing with the Unknown – or –<br />

Type Safety in a Dynamically Extensible Class<br />

Library.<br />

• Interfaces are strongly typed. Every interface has its own interface identifier,<br />

thereby eliminating the possibility of a collision between interfaces of the<br />

same human-readable name.<br />

• Interfaces are immutable. Interfaces are never versioned. Once defined and<br />

published, an interface cannot be changed.<br />

Once an interface has been published, it is not possible to change the external<br />

signature of that interface. It is possible at any time to change the implementation<br />

details of an object that exposes an interface. This change may be a minor<br />

bug fix or a complete reworking of the underlying algorithm; the clients of the<br />

interface do not care since the interface appears the same to them. This means<br />

that when upgrades to the servers are deployed in the form of new DLLs and<br />

EXEs, existing clients need not be recompiled to make use of the new functionality.<br />

If the external signature of the interface is no longer sufficient, a new<br />

interface is created to expose the new functions. Old or deprecated interfaces are<br />

not removed from a class to ensure all existing client applications can continue to<br />

communicate with the newly upgraded server. Newer clients will have the choice<br />

of using the old or new interfaces.<br />

THE IUNKNOWN INTERFACE<br />

All COM interfaces derive from the IUnknown interface, and all COM objects<br />

must implement this interface. The IUnknown interface performs two tasks: it<br />

controls object lifetime and provides runtime type support. It is through the<br />

IUnknown interface that clients maintain a reference on an object while it is in<br />

use—leaving the actual lifetime management to the object itself.<br />

Object lifetime is controlled with two methods, AddRef and Release, and an<br />

internal reference counter. Every object must have an implementation of<br />

IUnknown to control its own lifetime. Anytime an interface pointer is created or<br />

duplicated, the AddRef method is called, and when the client no longer requires<br />

this pointer, the corresponding Release method is called. When the reference<br />

count reaches zero, the object destroys itself.<br />

Clients also use IUnknown to acquire other interfaces on an object. QueryInterface is<br />

the method that a client calls when another interface on the object is required.<br />

When a client calls QueryInterface, the object provides an interface and calls<br />

AddRef. In fact, it is the responsibility of any COM method that returns an<br />

interface to increment the reference count for the object on behalf of the caller.<br />

The client must call the Release method when the interface is no longer needed.<br />

The client calls AddRef explicitly only when an interface is duplicated.<br />

When developing a COM object, the developer must obey the rules of<br />

QueryInterface. These rules dictate that interfaces for an object are symmetrical,<br />

transitive, and reflexive and are always available for the lifetime of an object. For<br />

the client this means that, given a valid interface to an object, it is always valid to<br />

ask the object, via a call to QueryInterface, for any other interface on that object<br />

including itself. It is not possible to support an interface and later deny access to<br />

that interface, perhaps because of time or security constraints. Other mechanisms<br />

62 • <strong>ArcGIS</strong> <strong>Engine</strong> <strong>Developer</strong> <strong>Guide</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!