WMI--Windows API--CoInitializeEx

150 篇文章 0 订阅
18 篇文章 0 订阅

原文来自MSDN Library for Visual Studio 2008 SP1,翻译部分仅为个人观点,想要看更多信息请看MSDN,如有版权问题请联系QQ 643166601,邮件643166601@qq.com

 

COM
CoInitializeEx
See Also  


Initializes the COM library for use by the calling thread, sets the thread's concurrency model, and creates a new apartment for the thread if one is required.
在你调用的线程中初始化这个COM库,设置线程的并发性模型,如果这个线程需要创建一个新的单元。
  Copy Code
HRESULT CoInitializeEx(
  void * pvReserved,
  DWORD dwCoInit
);
 

Parameters
pvReserved

[in] Reserved; must be NULL.
[in] 被保留的,必须是NULL。
dwCoInit

[in] Flags specifying the concurrency model and initialization options for the thread.
[in] Flags指定并发性模型和线程初始化选项。
Values for this parameter are taken from the COINIT enumeration.
这个参数的值是来自COINIT枚举。
Any combination of values from the COINIT enumeration can be used, except that the COINIT_APARTMENTTHREADED and COINIT_MULTITHREADED flags cannot both be set.
可以使用来自COINIT枚举值的任意组合,除了COINIT_APARTMENTTHREADED 和 COINIT_MULTITHREADED flag两个不能同时设置。
Return Values
This function supports the standard return values E_INVALIDARG, E_OUTOFMEMORY, and E_UNEXPECTED, as well as the following:
除了以下的,这个函数支持标准的返回值是E_INVALIDARG, E_OUTOFMEMORY, 和 E_UNEXPECTED。
S_OK

The COM library was initialized successfully on the calling thread.
这个COM库在调用线程中成功的初始化了。
S_FALSE

The COM library is already initialized on the calling thread.
这个COM库调用线程中已经初始化过了。
RPC_E_CHANGED_MODE

A previous call to CoInitializeEx specified a different concurrency model for the calling thread, or the thread that called CoInitializeEx currently belongs to the neutral threaded apartment.
一个之前线程中调用CoInitializeEx指定了不同的并发性模型,或者说CoInitializeEx当前属于中立的线程单元。
Remarks
CoInitializeEx must be called at least once, and is usually called only once, for each thread that uses the COM library.
CoInitializeEx必须被调用至少一次,而且通常只被调用一次,对于使用COM库的每个线程。
Multiple calls to CoInitializeEx by the same thread are allowed as long as they pass the same concurrency flag, but subsequent valid calls return S_FALSE.
在相同的线程中多次调用CoInitializeEx只要它们通过相同的并发性模型是被允许的,但是随后有效的调用返回的是S_FALSE.
To close the COM library gracefully on a thread, each successful call to CoInitialize or CoInitializeEx, including any call that returns S_FALSE, must be balanced by a corresponding call to CoUninitialize.
在一个线程中优雅地关闭COM库,每次成功调用CoInitialize 或 CoInitializeEx包括返回值是S_FALSE,必须相应的调用CoUninitialize来平衡。
Note: 
You must include the #define _WIN32_DCOM preprocessor directive at the beginning of your code to be able to use CoInitializeEx.
你必须包含#define _WIN32_DCOM 预处理器指令在你代码的开头能够使用CoInitializeEx。


A thread must call CoInitializeEx or CoInitialize before calling any other COM library function except the CoGetMalloc function and other memory allocation calls (CoTaskMemAlloc, CoTaskMemFree, CoTaskMemReAlloc, and the IMalloc methods on the task allocation supplied by CoGetMalloc).
一个线程在调用任何COM库函数之前必须调用CoInitializeEx 或 CoInitialize,除了CoGetMalloc函数和其他内存分配调用(CoTaskMemAlloc, CoTaskMemFree, CoTaskMemReAlloch和在任务分配中通过CoGetMalloc提供IMalloc方法)。
Otherwise, the COM library function will return CO_E_NOTINITIALIZED.
否则,COM库函数将返回CO_E_NOTINITIALIZED。
Once the concurrency model for a thread is set, it cannot be changed.
一旦一个线程设置了并发模型,它不能被改变。
A call to CoInitializeEx on a thread that was previously initialized with a different concurrency model will fail and return RPC_E_CHANGED_MODE.
在一个线程中一次调用CoInitializeEx,之前初始化的不同模型将失败返回RPC_E_CHANGED_MODE。
If neither concurrency model is specified by the dwCoInit parameter, the default is COINIT_MULTITHREADED.
如果dwCoInit参数没有指定并发性模型,这个默认的是COINIT_MULTITHREADED。
Objects created in a single-threaded apartment (STA) receive method calls only from their apartment's thread, so calls are serialized and arrive only at message-queue boundaries (when the Win32 function PeekMessage or SendMessage is called).
在单线程单元(STA)中接收方法创建对象只从它们的单元线程中调用,因此调用是序列化的并且只在消息队列界限(当Win32函数PeekMessage 或 SendMessage被调用)。
Objects created on a COM thread in a multithread apartment (MTA) must be able to receive method calls from other threads at any time.
在多线程单元(MTA)中必须可以接收方法可以从在任何时间其他线程调用。
You would typically implement some form of concurrency control in a multithreaded object's code using Win32 synchronization primitives such as critical sections, semaphores, or mutexes to help protect the object's data.
你以某种形式的并发控制将典型地实现在多线程对象的代码中使用Win32同步原语像临界区,信号量,或互斥量去帮助保护对象的数据。
When an object that is configured to run in the neutral threaded apartment (NTA) is called by a thread that is in either an STA or the MTA, that thread transfers to the NTA.
当配置一个对象在中立线程单元(NTA)中去运行被叫做一个线程不是STA也不是MTA,这个线程转移到NTA。
If this thread subsequently calls CoInitializeEx, the call fails and returns RPC_E_CHANGED_MODE.
如果该线程随后调用CoInitializeEx的,则调用失败并返回RPC_E_CHANGED_MODE。

CoInitializeEx provides the same functionality as CoInitialize and also provides a parameter to explicitly specify the thread's concurrency model.
CoInitializeEx提供相同痛能功能的CoInitialize也提供一个参数去明确的指定线程的并发性模型。 //--2013/12/31--寇香龙--修正原因:打字错误
The current implementation of CoInitialize calls CoInitializeEx and specifies the concurrency model as single-thread apartment.
当前的实现CoInitialize调用CoInitializeEx并且指定并发模型是单线程单元。
Applications developed today should call CoInitializeEx rather than CoInitialize.
应用程序开发到了今天将调用CoInitializeEx而不是CoInitialize。
Because OLE technologies are not thread-safe, the OleInitialize function calls CoInitializeEx with the COINIT_APARTMENTTHREADED flag.
因为OLE技术不是线程安全的,这个OleInitialize函数调用CoInitializeEx使用的是COINIT_APARTMENTTHREADED flag。
As a result, an apartment that is initialized for multithreaded object concurrency cannot use the features enabled by OleInitialize.
作为一个结果,多线程对象并发特征是初始化一个单元不能使用OleInitialize。
Because there is no way to control the order in which in-process servers are loaded or unloaded, do not call CoInitialize, CoInitializeEx, or CoUninitialize from the DllMain function.
因为没有办法去控制顺序在服务器进程内是加载或卸载,不要调用CoInitialize,CoInitializeEx或CoUninitialize在DllMain 函数。
Requirements
For an explanation of the requirement values, see Requirements (COM).

Windows NT/2000/XP: Requires Windows NT 4.0 or later.

Windows 95/98: Requires Windows 98 (or Windows 95 with DCOM).

Header: Declared in objbase.h.

Library: Use ole32.lib.

See Also
Reference
COINIT
CoInitialize

Concepts
Processes, Threads, and Apartments

Send comments about this topic to Microsoft.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值