WMI--Windows API--CoInitializeEx

来源:互联网 发布:最讨厌的网络用语 编辑:程序博客网 时间:2024/06/08 06:54

原文来自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.

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 中专没考上大专怎么办 考研两次没考上怎么办 2018造价员取消后怎么办 造价员取消了投标怎么办 造价员考试取消后怎么办 只拿到结业证怎么办 本科只有结业证怎么办 健身后肌肉水肿怎么办 大腿面肌肉疼怎么办 跑步后大腿酸疼怎么办? 运动后大腿胀痛怎么办 尔雅课程没看完怎么办 国家课程校本化怎么办 上海买房限购怎么办 投资公司要求回购股份怎么办 超过应届生落户年龄怎么办 造价员章到期了怎么办 造价员继续教育过期了怎么办 sd卡存储不够怎么办? 进项税大于销项税怎么办 公司扣员工奖金怎么办 墙面大面积有霉怎么办 电脑总是断网怎么办 造价师资格证书丢了怎么办 行测做题速度太慢怎么办 普通话准考证丢了怎么办 信用卡还款逾期了怎么办 淮北建洗煤厂需要怎么办 建设工程不结算怎么办 考监理没有职称怎么办 公开招标两次招标失败怎么办 山东省植保员证怎么办 工伤没开病假单怎么办 邮储银行怎么办金卡 农机被交警查怎么办 广联达识别板筋负筋重叠怎么办 掌上品的钱怎么办 财务纠纷被起诉怎么办 牙齿补后疼痛怎么办 牙齿表面蛀了怎么办 虫牙全掉了怎么办