2010. 3. 4. 13:48

Create local COM (ATL)

This static function allows you to create a new CComObject<Base> object, without the overhead of CoCreateInstance.

static HRESULT WINAPI CreateInstance(
   CComObject< Base >** pp 
);

Parameters

pp

[out] A pointer to a CComObject<Base> pointer. If CreateInstance is unsuccessful, pp is set to NULL.

Return Value

A standard HRESULT value.

Remarks

The object returned has a reference count of zero, so call AddRef immediately, then use Release to free the reference on the object pointer when you're done.

If you do not need direct access to the object, but still want to create a new object without the overhead of CoCreateInstance, use CComCoClass::CreateInstance instead.

Example

// Definition of CMyCircle.
class CMyCircle : 
   public IDispatchImpl<IMyCircle, &IID_IMyCircle, &LIBID_CIRCCOLLLib>, 
   public ISupportErrorInfo,
   public CComObjectRoot
{
public:
   CMyCircle();
BEGIN_COM_MAP(CMyCircle)
   COM_INTERFACE_ENTRY(IDispatch)
   COM_INTERFACE_ENTRY(IMyCircle)
   COM_INTERFACE_ENTRY(ISupportErrorInfo)
END_COM_MAP()
DECLARE_NOT_AGGREGATABLE(CMyCircle)

// IMyCircle
   STDMETHOD(get_XCenter)(double* pXCenter);
...
...
}

// Create a local instance of COM object CMyCircle.
double x;
CComObject<CMyCircle>* pCircle;
HRESULT hRes = CComObject<CMyCircle>::CreateInstance(&pCircle);
_ASSERTE(SUCCEEDED(hRes));

// Increment reference count immediately
pCircle->AddRef();

// Access method of COM object
hRes = pCircle->get_XCenter(&x);

// Decrement reference count when done
pCircle->Release();
pCircle = NULL;