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.

VISUAL C++<br />

Grimes, Richard, Reilly Stockton, Alex Stockton, and Julian Templeman. Beginning<br />

ATL 3 COM Programming. Chicago: Wrox Press Inc., 1999.<br />

King, Brad, and George Shepherd. Inside ATL. Redmond, WA: Microsoft Press,<br />

1999.<br />

Rector, Brent, Chris Sells, and Jim Springfield. ATL Internals. Reading, MA:<br />

Addison–Wesley, 1999.<br />

SMART TYPES<br />

Smart types are objects that behave as types. They are C++ class implementations<br />

that encapsulate a data type, wrapping it with operators and functions that make<br />

working with the underlying type easier and less error prone. When these smart<br />

types encapsulate an interface pointer, they are referred to as smart pointers. Smart<br />

pointers work with the IUnknown interface to ensure that resource allocation and<br />

deallocation are correctly managed. They accomplish this by various functions,<br />

construct and destruct methods, and overloaded operators. There are numerous<br />

smart types available to the C++ programmer. The two main smart types covered<br />

here are Direct-To-COM and Active Template Library.<br />

Smart types can make the task of working with COM interfaces and data types<br />

easier, since many of the API calls are moved into a class implementation; however,<br />

they must be used with caution and never without a clear understanding of<br />

how they are interacting with the encapsulated data type.<br />

Direct-To-COM smart types<br />

The smart type classes supplied with DTC are known as the Compiler COM<br />

Support Classes and consist of:<br />

• _com_error—This class represents an exception condition in one of the COM<br />

support classes. This object encapsulates the HRESULT and the IErrorInfo<br />

COM exception objects.<br />

• _com_ptr_t—This class encapsulates a COM interface pointer. See below for<br />

common uses.<br />

• _bstr_t—This class encapsulates the BSTR data type. The functions and operators<br />

on this class are not as rich as the ATL CComBSTR smart type; hence, this<br />

is not normally used.<br />

• _variant_t—This class encapsulates the VARIANT data type. The functions<br />

and operators on this class are not as rich as the ATL CComVariant smart type;<br />

hence, this is not normally used.<br />

To define a smart pointer for an interface, you can use the macro<br />

_COM_SMARTPTR_TYPEDEF like this:<br />

_COM_SMARTPTR_TYPEDEF(IFoo, __uuidof(IFoo));<br />

The compiler expands this as follows:<br />

typedef _com_ptr_t< _com_IIID > IFooPtr;<br />

Once declared, it is simply a matter of declaring a variable as the type of the<br />

interface and appending Ptr to the end of the interface. Below are some common<br />

uses of this smart pointer that you will see in the numerous C++ samples.<br />

// Get a CLSID GUID constant.<br />

110 • <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!