Functions


CoCreateInstance Function

Creates a single uninitialized object of the class associated with a specified CLSID.

Call CoCreateInstance when you want to create only one object on the local system. To create a single object on a remote system, call the CoCreateInstanceEx function. To create multiple objects based on a single CLSID, call the CoGetClassObject function.

Syntax

C++
HRESULT CoCreateInstance(
  __in   REFCLSID rclsid,
  __in   LPUNKNOWN pUnkOuter,
  __in   DWORD dwClsContext,
  __in   REFIID riid,
  __out  LPVOID *ppv
);

Parameters

rclsid [in]

The CLSID associated with the data and code that will be used to create the object.

pUnkOuter [in]

If NULL, indicates that the object is not being created as part of an aggregate. If non-NULL, pointer to the aggregate object's IUnknown interface (the controlling IUnknown).

dwClsContext [in]

Context in which the code that manages the newly created object will run. The values are taken from the enumeration CLSCTX.

riid [in]

A reference to the identifier of the interface to be used to communicate with the object.

ppv [out]

Address of pointer variable that receives the interface pointer requested in riid. Upon successful return, *ppv contains the requested interface pointer. Upon failure, *ppv contains NULL.

Return Value

This function can return the following values.

Return codeDescription
S_OK

An instance of the specified object class was successfully created.

REGDB_E_CLASSNOTREG

A specified class is not registered in the registration database. Also can indicate that the type of server you requested in the CLSCTX enumeration is not registered or the values for the server types in the registry are corrupt.

CLASS_E_NOAGGREGATION

This class cannot be created as part of an aggregate.

E_NOINTERFACE

The specified class does not implement the requested interface, or the controlling IUnknown does not expose the requested interface.

E_POINTER

The ppv parameter is NULL.

 

Remarks

The CoCreateInstance function provides a convenient shortcut by connecting to the class object associated with the specified CLSID, creating an uninitialized instance, and releasing the class object. As such, it encapsulates the following functionality:

CoGetClassObject(rclsid, dwClsContext, NULL, IID_IClassFactory, &pCF); 
hresult = pCF->CreateInstance(pUnkOuter, riid, ppvObj) 
pCF->Release(); 

It is convenient to use CoCreateInstance when you need to create only a single instance of an object on the local machine. If you are creating an instance on remote computer, call CoCreateInstanceEx. When you are creating multiple instances, it is more efficient to obtain a pointer to the class object's IClassFactory interface and use its methods as needed. In the latter case, you should use the CoGetClassObject function.

In the CLSCTX enumeration, you can specify the type of server used to manage the object. The constants can be CLSCTX_INPROC_SERVER, CLSCTX_INPROC_HANDLER, CLSCTX_LOCAL_SERVER, CLSCTX_REMOTE_SERVER or any combination of these values. The constant CLSCTX_ALL is defined as the combination of all four. For more information about the use of one or a combination of these constants, see CLSCTX.

Requirements

Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderObjbase.h
LibraryOle32.lib
DLLOle32.dll

See Also

CoGetClassObject
CoCreateInstanceEx
IClassFactory::CreateInstance
Instance Creation Helper Functions

Send comments about this topic to Microsoft

Build date: 11/12/2009

Tags :


Community Content

Charming
vbs script syntax
<DllImport("ole32.dll", ExactSpelling:=True, PreserveSig:=False)> _
Public Shared Function CoCreateInstance(<[In]> ByRef clsid As Guid, <MarshalAs(UnmanagedType.Interface)> ByVal punkOuter As Object, ByVal context As Integer, <[In]> ByRef iid As Guid) As <MarshalAs(UnmanagedType.Interface)> Object End Function
Tags : vb.net

dmex
C# syntax
[return: MarshalAs(UnmanagedType.Interface)]
[DllImport("ole32.dll", ExactSpelling=true, PreserveSig=false)] public static extern object CoCreateInstance([In] ref Guid clsid, [MarshalAs(UnmanagedType.Interface)] object punkOuter, int context, [In] ref Guid iid);
Tags : c#

Page view tracker