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.

C++ APPLICATION PROGRAMMING INTERFACE<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 />

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

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

// Declare Smart Pointers for IFoo, IBar, and IGak interfaces.<br />

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

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

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

HRESULT SomeClass::Do()<br />

{<br />

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

IFooPtr ipFoo;<br />

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

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

If you have used smart pointers before, you<br />

might have seen differences in the implementation<br />

of the equality (“==”) operator for smart<br />

pointer comparisons. The COM specification<br />

states object identification is performed by<br />

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

smart pointers will perform necessary QI and<br />

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

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

IBarPtr ipBar;<br />

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

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

// QI IBar interface for IGak interface.<br />

IGakPtr ipGak(ipBar);<br />

// Call method on IGak.<br />

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

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

// Explicitly call Release().<br />

ipGak = 0;<br />

ipBar = 0;<br />

// Let destructor call IFoo's Release.<br />

return S_OK;<br />

}<br />

When working with CComBSTR, use the text mapping L“” to declare constant<br />

OLECHAR strings. To display a CComBSTR at the command line, use wcerr.<br />

You will need to include iostream to use wcerr.<br />

CComBSTR bsName(L"Matt");<br />

std::wcerr

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

Saved successfully!

Ooh no, something went wrong!