原文来自MSDN Library for Visual Studio 2008 SP1,翻译部分仅为个人观点,想要看更多信息请看MSDN,如有版权问题请联系QQ 643166601,邮件643166601@qq.com
COM
CoCreateInstance
See Also
Creates a single uninitialized object of the class associated with a specified CLSID.
用关联指定的CLSID创建一个单个未初始化的对象。
Call CoCreateInstance when you want to create only one object on the local system.
当你相应创建仅仅是在本地系统上时调用CoCreateInstance。
To create a single object on a remote system, call CoCreateInstanceEx, as CoCreateInstance requires that the HKCR\APPID\{appid}\RemoteServerName key be set to specify where remote activation of the object must occur.
去创建一个单个对象在远程系统,调用CoCreateInstanceEx,设置HKCR\APPID\{appid}\RemoteServerName作为CoCreateInstance请求那里的远程对象必须产生激活。
To create multiple objects based on a single CLSID, refer to the CoGetClassObject function.
去创建基于单个CLSID多个对象,参考CoGetClassObject函数。
Copy Code
STDAPI CoCreateInstance(
REFCLSID rclsid,
LPUNKNOWN pUnkOuter,
DWORD dwClsContext,
REFIID riid,
LPVOID * ppv
);
Parameters
rclsid
[in] CLSID associated with the data and code that will be used to create the object.
CLSID关联数据和代码将被使用去创建对象。
pUnkOuter
[in] If NULL, indicates that the object is not being created as part of an aggregate.
如果是NULL,指示对象没有被创建作为一个集合体的一部分。
If non-NULL, pointer to the aggregate object's IUnknown interface (the controlling IUnknown).
如果是非空,指针指向集合体对象的IUnknown接口(控制的IUnknown)。
dwClsContext
[in] Context in which the code that manages the newly created object will run.
上下文在代码的管理这个新创建的对象将要运行
The values are taken from the enumeration CLSCTX.
这个值取自CLSCTX枚举。
riid
[in] 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.
地址的指针变量它在riid中接收请求接口的指针。
Upon successful return, *ppv contains the requested interface pointer.
当返回成功时,*ppv包含被清去的几口指针。
Upon failure, *ppv contains NULL.
当失败时,*ppv包含NULL。
Return Values
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.
也可能你在CLSCTX枚举中请求的指示服务器的类型没有注册或在服务器类型的值在注册表中损坏。
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.
这个指定类没有实现接口请求或控制IUnknown没有公开这个请求接口。
Remarks
The CoCreateInstance helper 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.
这个CoCreateInstance辅助函数提供了通过连接到类对象关联这个指定的CLSID的一个方便的捷径,创建一个未初始化的实例并且释放类对象。
As such, it encapsulates the following functionality:
正因为如此,它封装了以下功能:
Copy Code
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.
当你需要去创建一个本地机器的对象的一个单实例时它是方便的去使用CoCreateInstance。
If you are creating an instance on remote machine, call CoCreateInstanceEx.
如果你创建一个远程机器的一个实例调用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.
当你创建多实例时,它是更有效的去检索一个指针都类对象的IClassFactory接口并且使用它需要的方法。
In the latter case, you should use the CoGetClassObject function.
在最后情况,你应该使用CoGetClassObject函数。
In the CLSCTX enumeration, you can specify the type of server used to manage the object.
在CLSCTX枚举中,你可以指定使用服务器的类型去管理这个对象。
The constants can be CLSCTX_INPROC_SERVER, CLSCTX_INPROC_HANDLER, CLSCTX_LOCAL_SERVER, or any combination of these values.
这个内容可以是CLSCTX_INPROC_SERVER, CLSCTX_INPROC_HANDLER, CLSCTX_LOCAL_SERVER这些值的联合。
The constant CLSCTX_ALL is defined as the combination of all three.
这个常量CLSCTX_ALL的定义作为全部的三个的联合。
For more information about the use of one or a combination of these constants, refer to CLSCTX.
更多的信息关于一个或这些常量的联合的使用,参考CLSCTX。
Requirements
For an explanation of the requirement values, see Requirements (COM).
Windows NT/2000/XP: Requires Windows NT 3.1 or later.
Windows 95/98: Requires Windows 95 or later.
Header: Declared in objbase.h.
Library: Use ole32.lib.
See Also
Reference
CoGetClassObject
IClassFactory::CreateInstance
CoCreateInstanceEx
CLSCTX
Concepts
Instance Creation Helper Functions
Send comments about this topic to Microsoft.