CoCreateInstance

HRESULT CoCreateInstance(clsid, pUnkOuter, grfContext, iid, ppvObj)

Create an uninitialized instance of the class clsid, asking for interface iid using the execution contexts given in grfContext. If the object is being used as part of an aggregation then pUnkOuter contains a pointer to the controlling unknown. These parameters behave as those of the same name in CoGetClassObject (clsid) and IClassFactory::CreateInstance (pUnkOuter, grfContext, iid, ppv),

CoCreateInstance is simply a wrapper function for CoGetClassObject and IClassFactory that is implemented (conceptually) as follows:


HRESULT CoCreateInstance(REFCLSID clsid, IUnknown * pUnkOuter,
.....DWORD grfContext, REFIID iid, void * ppvObj)
.....{
.....IClassFactory * pCF;
.....HRESULT         hr;

.....hr=CoGetClassObject(clsid, grfContext, NULL, IID_IClassFactory, (void *)pCF);

.....if (FAILED(hr))
..........return hr;

.....hr=pCF->CreateInstance(pUnkOuter, iid, (void *)ppv);
.....pCF->Release();

...../*
..... * If CreateInstance fails, ppv will be set to NULL. Otherwise
..... * ppv has the interface pointer and hr contains NOERROR.
..... */
.....return hr;
.....}

Argument

Type

Description

clsid

REFCLSID

The class of which an instance is desired

pUnkOuter

IUnknown*

The controlling unknown, if any.

grfContext

DWORD

The CLSCTX to be used.

iid

REFIID

The initialization interface desired

ppv

void**

The place at which to return the desired interface.


Return Value

Meaning

S_OK

Success.

Any error that can be returned from CoGetClassObject or IClassFactory::CreateInstance

Semantics as in those functions.

E_UNEXPECTED

An unknown error occurred.