18.04.2015 Views

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

C++ APPLICATION PROGRAMMING INTERFACE<br />

The following example illustrates how the three functions discussed above should<br />

be used within an application.<br />

In this code, both AoInitialize and IAoInitialize<br />

are used. These are not the same thing:<br />

AoInitialize is the API call discussed above, while<br />

IAoInitialize is an ArcObjects interface used in<br />

licensing.<br />

Note that IAoInitialize must be scoped so that<br />

it will be out of scope before AoUninitialize is<br />

called.<br />

int main (int argc, char* argv[])<br />

{<br />

// Initialize <strong>ArcGIS</strong> <strong>Engine</strong> and COM.<br />

::AoInitialize(NULL);<br />

// <strong>ArcGIS</strong> <strong>Engine</strong> licensing<br />

{<br />

IAoInitialize ipInit(CLSID_AoInitialize);<br />

esriLicenseStatus status;<br />

ipInit->Initialize(esriLicenseProductCode<strong>Engine</strong>, &status);<br />

// ArcObjects code here<br />

}<br />

ipInit->Shutdown();<br />

// Uninitialize <strong>ArcGIS</strong> <strong>Engine</strong> and COM.<br />

::AoUninitialize();<br />

}<br />

// Exit the application.<br />

AoExit(0);<br />

AoCreateObject<br />

• AoCreateObject—used where CoCreateInstance would be used in COM programming.<br />

extern "C" HRESULT AoCreateObject(REFCLSID rclsid,<br />

LPUNKNOWN pUnkOuter,<br />

DWORD dwClsContext,<br />

REFIID riid,<br />

LPVOID *ppv);<br />

When using smart pointers, this function will not be needed. However, you<br />

can create an instance of an object without smart pointers by using this function,<br />

as shown in the following code:<br />

// Create a Workspace Factory without using smart pointers.<br />

IWorkspaceFactory *pWorkspaceFactory;<br />

hr = ::AoCreateInstance(CLSID_ShapefileWorkspaceFactory, 0,<br />

CLSCTX_INPROC_SERVER,<br />

IID_IWorkspaceFactory,<br />

(void **)&pWorkspaceFactory);<br />

AoAllocBSTR and AoFreeBSTR<br />

• AoAllocBSTR replaces SysAllocString.<br />

extern "C" BSTR AoAllocBSTR(const OLECHAR *sz);<br />

• AoFreeBSTR replaces SysFreeString.<br />

extern "C" void AoFreeBSTR(BSTR bstr);<br />

Chapter 4 • <strong>Developer</strong> environments • 225

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

Saved successfully!

Ooh no, something went wrong!