您现在的位置:首页 >> API >> API >> 内容

Delphi使用COM对象的时候是否需要CoInitialize

时间:2011/9/3 15:43:27 点击:

  核心提示:MSDN上CoInitialize的解释:Initializes the COM library on the current apartment and identifies the concurr...

MSDN上CoInitialize的解释:
Initializes the COM library on the current apartment and identifies the concurrency model as single-thread apartment (STA). Applications must initialize the COM library before they can call COM library functions other than CoGetMalloc and memory allocation functions.

 

CoInitialize并不装载com库,这个函数只是用来初始化当前线程使用什么样的套间。当使用这个函数以后,线程就和一个套间建立了对应关系。
线程的套间模式决定了该线程如何调用com对象,是否需要列集等
你可以看一下有关列集的资料,使用不同套间之间对象接口是通过列集来完成的。关于列集的实现,很多书上都有较详细的说明。
CoInitialize()并不会干扰客户和服务之间的通信,套所作的事情只是让线程注册一个套间,而线程运行过程中就必然在此套间中,就象我们每个活着的人,都一定属于某个国家一样。
需要强调的是,套间是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.

CoInitialize(0)让当前线程进入一个STA的Apartment

CoInitialize、CoInitializeEx都是windows的API,主要是告诉windows以什么方式为程序创建COM对象。
有哪些方式呢?单线程和多线程。
CoInitialize指明以单线程方式创建。
CoInitializeEx可以指定COINIT_MULTITHREADED以多线程方式创建。
创建单线程方式的COM服务器时不用考虑串行化问题,多线程COM服务器就要考虑。
在使用中,使用CoInitialize创建可使对象直接与线程连接,得到最高的性能。创建多线程对象可以直接接收所有线程的调用,不必像单线程那样需要消息排队,但却需要COM创建线程间汇集代理,这样访问效率不高。

来看一下delphi的代码,在ComObj.pas和ComServ.pas代码中有这样的一个函数:
procedure InitComObj;
begin
  if SaveInitProc <> nil then TProcedure(SaveInitProc);
  if (CoInitFlags <> -1) and Assigned(ComObj.CoInitializeEx) then
  begin
    NeedToUninitialize := Succeeded(ComObj.CoInitializeEx(nil, CoInitFlags));
    IsMultiThread := IsMultiThread or
      ((CoInitFlags and COINIT_APARTMENTTHREADED) <> 0) or
      (CoInitFlags = COINIT_MULTITHREADED);  // this flag has value zero
  end
  else
    NeedToUninitialize := Succeeded(CoInitialize(nil));
end;
很明显他就是用来调用CoInitialize初始化com环境的。
在ComObj.pas的initialization部分:
initialization
begin
  LoadComExProcs;
  VarDispProc := @VarDispInvoke;
  DispCallByIDProc := @DispCallByID;
{$IFDEF MSWINDOWS}
  SafeCallErrorProc := @SafeCallError;
{$ENDIF}
  if not IsLibrary then
  begin
    SaveInitProc := InitProc;
    //将InitProc指针指向InitComObj函数。
    InitProc := @InitComObj;
  end;
end;

来到Forms.pas文件中,看一下TApplication.Initialize部分:
procedure TApplication.Initialize;
begin
  if InitProc <> nil then TProcedure(InitProc);
end;
所以说,只要我们在dpr文件中包含了ComObj.pas或者是ComServ.pas,delphi就会自动为我们初始化com环境,这样就不用我们手动调用了。

在delphi中调用COM控件有下面几种方法:

CreateComObject、CreateOleObject,这两个函数都包含在ComObj.pas中。

通过Import Type Library将COM控件导入,生成的pas文件都包含ComObj这个单元。

还有通过Import Activex生成的pas文件也会包含ComObj单元。

所以说,delphi使用COM控件不需要手动调用CoInitialize

作者:tingsking1 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
本类推荐
  • 没有
本类固顶
  • 没有
  • 盒子文章(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 沪ICP备05001939号