Creating Objects with Smart Pointers

Created:4/18/2001
Description:

Smarts pointers are a smart data type that encapsulate an interface pointer.

Smart pointers work by working with the IUnknown interface to ensure that resource allocation and deallocation are correctly managed. They accomplish this by various functions, construct and destruct methods, and overloaded operators.

An interface smart pointer has the same name as the interface it refers to with the Suffix 'Ptr'. When the esriCore.olb is imported the smart pointer and CLSID_*** are defined.

If you want to access an interface defined within your own project, via a smart pointer, you must define the smart pointer using the following syntax.

_COM_SMARTPTR_TYPEDEF(IFoo,__uuidof(IFoo));

The code below show how to create instances of COM objects using functionality build into the Smart Pointer.


How to use:
  1. Paste code into your Visual C++ project and change the Ptrs and CLSIDs to match your required classes and interfaces.
// Working with CoCreateInstance
		// Create Instance of Foo class and QI for IFoo interface using Constructor
		IFooPtr ipFoo(CLSID_Foo);
		if (ipFoo ==0) return E_NOMEMORY

		// No need to explicitly call ::Release() since destructor will do it for me
		// If you want to call ::Release simple set the smart pointer = 0