可以解决这个问题,如果一个COM对象能自动聚合或独立的话,
一般是要创建一个能使用参数(聚合的COM对象接口IUuknown)构照函数;
如下:
TOneCom = Class (TAutoObject,IOneInterface)
private
Fintf: IUnknown;
protected
function QueryInterface(....)//参看IUnknown的QueryInterface,
public
Constructor Create(pUnk: IUnknown);
end;
Constructor TOneCom.Create(pUnk: IUnknown);
begin
FIntf:=pUnk;
end;
//缺省的代码自己补吧
function TOneCom.QueryInterface(....)
begin
//聚合方式
if Fintf<>nil then
if 查找的是IUnknown接口 then
Return:=Fintf;
Exit;
end;
if 查找的是IOne接口 then
Return:=Self as IOne;
exit;
end;
result:=FIntf.QueryInterface(...);
exit;
end;
//无聚合方式
if 要查找IOne or IUnknown then
Return:=FOne;
exit;
end;
end;
TTwoCom=(TAutoObject, ITwo)
private
FOne: IOne;
protected
function QueryInterface(...);
public
procedure initiaze;
override;
end;
Procedure TTwoCom.initiaze;
begin
inherited initiaze;
FOne:=IOne(TOneCOm.Create(Self as IUnknown));
// FOne._AddRef;
如果程序有错的话,补充这句.记不太清了.
end;
function TTwoCom.QueryInterface(...)
begin
if 要查找IOne then
Return:=FOne;
exit;
end;
//其他按正常返回
if 要查找ITwo or IUnknown then
Return:=Self as ITwo;
end;
end;
end;
大概思路如此.