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 />

IFooPtr ipFoo;<br />

HRESULT hr = ipFoo.CreateInstance(CLSID_Foo);<br />

if (FAILED(hr))<br />

return hr; // Return object creation failure code to caller.<br />

Active Template Library smart types<br />

ATL defines various smart types, as seen in the list below. You are free to combine<br />

both the ATL and DTC smart types in your code. However, it is typical to<br />

use the DTC for smart pointers, as they are easily generated by importing type<br />

libraries. For BSTR and VARIANT types, the ATL versions for CComBSTR and<br />

CComVariant are typically used.<br />

ATL smart types include:<br />

• CComPtr—encapsulates a COM interface pointer by wrapping the AddRef and<br />

Release methods of the IUnknown interface<br />

• CComQIPtr—encapsulates a COM interface and supports all three methods of<br />

the IUnknown interface: QueryInterface, AddRef, and Release<br />

• CComBSTR—encapsulates the BSTR data type<br />

• CComVariant—encapsulates the VARIANT data type<br />

• CRegKey—provides methods for manipulating Windows registry entries<br />

• CComDispatchDriver—provides methods for getting and setting properties and<br />

calling methods through an object’s IDispatch interface<br />

• CSecurityDescriptor—provides methods for setting up and working with the<br />

Discretionary Access Control List (DACL)<br />

This section examines the first four smart types and their uses. The example code<br />

below, written with ATL smart pointers, looks like the following:<br />

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

extern "C" const GUID __declspec(selectany) CLSID_Foo = \<br />

{0x2f3b470c,0xb01f,0x11d3,{0x83,0x8e,0x00,0x00,0x00,0x00,0x00,0x00}};<br />

STDMETHODIMP SomeClass::Do ()<br />

{<br />

// Create Instance of Foo class and QI for IFoo interface.<br />

CComPtr ipFoo;<br />

HRESULT hr = CoCreateInstance(CLSID_Foo, NULL, CLSCTX_INPROC_SERVER,<br />

IID_IFoo, (void **)&ipFoo);<br />

if (FAILED(hr)) return hr;<br />

The equality operator (“==”) may have different<br />

implementations when used during smart<br />

pointer comparisons. The COM specification<br />

states object identification is performed by<br />

comparing the pointer values of IUnknown. The<br />

DTC smart pointers will perform necessary QI<br />

and comparison when using the “==” operator.<br />

However, the ATL smart pointers will not do<br />

this, so you must use the ATL IsEqualObject<br />

method.<br />

// Call method on IFoo to get IBar.<br />

CComPtr ipBar;<br />

HRESULT hr = ipFoo->get_Bar(&ipBar);<br />

if (FAILED(hr)) return hr;<br />

// IBar interface for IGak interface<br />

CComQIPtr ipGak(ipBar);<br />

// Call method on IGak.<br />

hr = ipGak->DoSomething();<br />

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