CoInitialize CoInitializeEx 是用来初始化COM运行环境的。
OleInitialize是初始化Ole的运行环境,Ole是在Com的基础上作的扩展,是ActiveX运行的基础,
OleInitialize肯定会调用CoInitialize。
CoInitialize、CoInitializeEx都是windows的API,主要是告诉windows以什么方式为程序创建COM对象,原因是程序调用com库函数(除CoGetMalloc和内存分配函数)之前必须初始化com库。
CoInitialize指明以单线程方式创建。
CoInitializeEx可以指定COINIT_MULTITHREADED以多线程方式创建。
创建单线程方式的COM服务器时不用考虑串行化问题,多线程COM服务器就要考虑。
CoInitialize并不装载com库,这个函数只是用来初始化当前线程使用什么样的套间。当使用这个函数以后,线程就和一个套间建立了对应关系。
线程的套间模式决定了该线程如何调用com对象,是否需要列集等
套间是com中用来解决并发调用冲突的很有效的办法
Before calling any COM functions, a thread needs to call CoInitialize to load the COM infrastructure (and to enter an apartment). Once a thread calls CoInitialize, the thread is free to call COM APIs.
CoInitializeEx provides the same functionality as CoInitialize and also provides a parameter to explicitly specify the thread's concurrency model. The current implementation of CoInitialize calls CoInitializeEx and specifies the concurrency model as single-thread apartment. Applications developed today should call CoInitializeEx rather than CoInitialize.
注:新的应用程序应该调用CoInitializeEx而不是CoInitialize,否则就会有必要在之后每个调用Com的线程中调用CoInitialize来初始化出每个线程自己的套间。
AfxOleInit实际上调用了OleInitialize,虽然它在内部也调用了CoInitializeEx,但它只能处理单线程,这是AfxOleInit和CoInitialize主要区别:
OleInitialize calls CoInitializeEx internally to initialize the COM library on the current apartment. Because OLE operations are not thread-safe, OleInitialize specifies the concurrency model as single-thread apartment.
Once the concurrency model for an apartment is set, it cannot be changed. A call to OleInitialize on an apartment that was previously initialized as multithreaded will fail and return RPC_E_CHANGED_MODE.
应用:
1. CoInitialize 仅仅初始化Com,支持多线程。也就是说如果多线程调用Com接口,必须在每个线程中都调用CoInitialize。
2. OleInitialize 初始化Com(其实也是调用CoInitializeEx),支持多线程。比CoInitialize多了一下内容:
A) Clipboard
B) Drag and drop
C) Object linking and embedding (OLE)
D) In-place activation
如果不需要这些,用CoInitialize就可以。
3. AfxOleInit是MFC对OleInitialize的封装。貌似不支持多线程,也就是说只能在主进程调用该函数,如果线程需要使用Com必须调用上面的两个来实现初始化。而且MSDN明确标明AfxOleInit不能在MFC的DLL中调用,否则也会造成初始化失败。
4.
OleInitialize和OleUninitialize( )成对使用;CoInitialize和CoUninitialize成对使用;CoInitializeEx和CoUninitialize成对使用;AfxOleInit()由MFC自动释放。
VC++使用ADO访问ACCESS时,出现_RecordsetPtr Open卡或者_ConnectionPtr Excute卡或者
_CommandPtr Excute一直卡住等莫名其妙的情况时,而语法又没有错误时,请确定初始化COM函数。
MFC程序建议使用AfxOleInit()。