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

4. To create the noncreatable object inside a method, use the CComObject<br />

template to supply the implementation of CreateInstance.<br />

// Get NonCreatable object Bar (implementing IBar) from COM object Foo.<br />

STDMETHODIMP CFoo::CreateBar(IBar **pVal)<br />

{<br />

if (pVal==0) return E_POINTER;<br />

// Smart pointer to noncreatable object Bar<br />

IBarPtr ipBar = 0;<br />

// C++ Pointer to Bar, with ATL template to supply CreateInstance<br />

implementation<br />

CComObject* pBar = 0;<br />

HRESULT hr = CComObject::CreateInstance(&pBar);<br />

if (SUCCEEDED(hr))<br />

{<br />

// Increment the ref count from 0 to 1 to protect the object<br />

// from being released in any initialization code.<br />

pBar->AddRef();<br />

// Call C++ methods (not exposed to COM) to initialize the Bar object.<br />

pBar->InitialiseBar(10);<br />

// QI to IBar and hold a smart pointer reference to the object Bar.<br />

hr = pBar->QueryInterface(IID_IBar, (void**)&ipBar);<br />

}<br />

pBar->Release();<br />

// Return IBar pointer to the caller.<br />

*pVal = ipBar.Detach();<br />

}<br />

return S_OK;<br />

ATL in Visual C++ .NET<br />

Visual C++ version 6 is used for the majority of this help. However, with the<br />

release of Visual C++ .NET, there are enhancements and changes that are relevant<br />

to the <strong>ArcGIS</strong> ATL developer. Some of these are summarized below.<br />

Attribute-based programming—This is a major change introduced in VC7.<br />

Attributes are inserted in the source code enclosed in square brackets—for example,<br />

[ coclass ]. Attributes are designed to simplify COM programming and<br />

.NET framework common language runtime development. When you include<br />

attributes in your source files, the compiler works with provider DLLs to insert<br />

code or modify the code in the generated object files. There are attributes that aid<br />

in the creation of .idl files, interfaces, type libraries, and other COM elements. In<br />

the integrated development environment (IDE), attributes are supported by the<br />

wizards and by the Properties window. The ATL wizards make extensive use of<br />

attributes to inject the ATL boilerplate code into the class. Consequently, typical<br />

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