教高手们,在COM技术中CoClass是什么???COM object 与 CoClass间的关系是什么样的?(50分)

  • 主题发起人 主题发起人 xport
  • 开始时间 开始时间
X

xport

Unregistered / Unconfirmed
GUEST, unregistred user!
昨天看了Delphi帮助文档中说的CoClass的资料,有点费解,请问CoClass到底是COM对象的类工厂呢?还是对COM所定义接口的实现类.

COM object 与 CoClass间的关系是什么样的?

谢谢.
_________________________________________________________________________

A COM object is an instance of a CoClass, which is a class that implements one or more COM interfaces. The COM object provides the services as defined by its interfaces.

CoClasses are instantiated by a special type of object called a class factory. Whenever an object's services are requested by a client, a class factory creates an object instance for that particular client. Typically, if another client requests the object's services, the class factory creates another object instance to service the second client. (Clients can also bind to running COM objects that register themselves to support it.)

A CoClass must have a class factory and a class identifier (CLSID) so that it can be instantiated externally, that is, from another module. Using these unique identifiers for CoClasses means that they can be updated whenever new interfaces are implemented in their class. A new interface can modify or add methods without affecting older versions, which is a common problem when using DLLs.

Delphi wizards take care of assigning class identifiers and of implementing and instantiating class factories.

还有,我看了在delphi中对COM的定义,有2个.pas文件,一个是TLB Wrapper,里面只有对CoClass,interface,CLSID等的定义,另一个是COM 中,interface的具体实现。

比如:对于Sample02.tlb,有一个wrapper文件:Sample02_TLB.pas,内容有TClass01的CoClass的实现:
CoTClass01 = class
class function Create: ITClass01;
class function CreateRemote(const MachineName: string): ITClass01;
end;

class function CoTClass01.Create: ITClass01;
begin
Result := CreateComObject(CLASS_TClass01) as ITClass01;
end;

class function CoTClass01.CreateRemote(const MachineName: string): ITClass01;
begin
Result := CreateRemoteComObject(MachineName, CLASS_TClass01) as ITClass01;
end;

还有一个文件:TClass01Unit.pas是对TClass01的interface的实现:
TTClass01 = class(TAutoObject, ITClass01)
protected
function Method1: OleVariant; safecall;
end;

function TTClass01.Method1: OleVariant;
begin
Result := 'Hello Word';
end;
 
CoClass是对COM所定义接口的实现类.

COM object 是 CoClass 的实例
 
這個回答,太淺顯了,請再深入一些.請看看我給的Source,CoClass並沒有對COM所定義的interface實現,而是聲明了COM所有的interface和繼承關係啊...

COM Object 是CoClass的實例,應該是通過Class Factory的Create方法產生的,這個我沒異議.
 
CoClass只不过包装了一下罢了,你去看所有的CoClass都是这么简单。
具体借口的实现是用你自定义的VCL类实现的。
 
這個我沒有異議,只是覺得:"A COM object is an instance of a CoClass, which is a class that implements one or more COM interfaces",這句中說CoClass實現了COM的interface有些費解,不是我自己定義的TClass01這個Class去實現的COM的interface嗎?
 
我昨天剛好看到李維的ADO的第五張,主要講這個,看不懂
 
正在看ye802808說的那篇文章,似乎我的理解有偏差...這裡的COM interfaces的實現,應該是指在CoClass中聲明,在自己寫的VCL Class中定義,只有這樣合起來看,我才能舒服點.

還是期待更多高手的指點...m_(_ _)_m
 
你看TLB.PAs是看不出来的,得 去看IDL,这里的实现和Object Pascal里的实现
不是一个意思。
 
coclass TClass01
{
[default] interface ITClass01;
};

沒想到,在IDL中,指定的coclass居然是TClass01,那針對Sample02.tlb所產生的那個wrapper中,有一個CoTClass01,长得蠻像Class Factory的,為什麼這個傢伙的命名規則是Co+ClassName呢?? 這個難道就是COM Object的Class Factory嗎??
 
后退
顶部