【问题标题】:delphi tlb_lib file from com component has no connection between interface and object来自com组件的delphi tlb_lib文件在接口和对象之间没有连接
【发布时间】:2012-03-22 06:51:55
【问题描述】:

将com dll文件导入delphi后,delphi又生成了一个lib_tlb.pas文件。

检查它显示的文件

Iinterface1 = interface(IDispatch)
    function func: Integer; safecall;
    procedure proc(param:Iinterface1);
end;

Cointerface1 = class
  class function Create: Iinterface;
  class function CreateRemote(const MachineName: string): Iinterface1;
end;

Tinterface1 = class(TOleServer)
    function func: Integer;
    procedure proc(param:Iinterface1);
end;

现在可以清楚地看到 Tinterface1 和 Iinterface1 之间没有连接。

当使用 Tinterface1 调用 proc 时,问题就来了。这不会编译 Tinterface1 不继承 Iinterface1。

那么建议做什么?更改自动生成的库?或者当您想将 Tinterface1 传递给 proc 时,您是否有更好的想法。

这个例子是代码的简化,代码中有另一个对象需要传递给proc,但是proc只知道它的接口,这是同样的问题。

更新:com dll文件的手册似乎说proc应该是

    procedure proc(param:^Tinterface1);

界面仅在 delphi 的角度。

【问题讨论】:

  • 您需要发布真实代码。您是否在 typelib 查看器中查看过 typelib?文件应该显示什么?
  • 您推荐使用特定类型的 lib 查看器吗?它是专有代码,不是我的。
  • 即使只是使用 Delphi 附带的类型库查看器,也会向您显示结构应该是什么。 delphi ide不太可能错误地生成tlb pas

标签: delphi com interface delphi-2009


【解决方案1】:

TInterface1.Proc() 期望将预先存在的IInterface1 对象作为输入传递给它。使用Cointerface1.Create() 创建该对象,例如:

var
  intf: Iinterface1;
begin
  intf := Cointerface1.Create;
  TheOleServerInstance.proc(intf);
end;

Tinterface1TOleServer 的后代,它不直接从 Iinterface1 继承(但它确实在内部包装了 Iinterface1),因此无论何时你想将它传递给 Iinterface1预计,例如:

var
  intf: Iinterface1;
  svr: Iinterface1;
begin
  intf := Cointerface1.Create;
  if Supports(TheOleServerInstance, Iinterface1, svr) then
    intf.proc(svr);
end;

【讨论】:

  • 是的,对象是用 coIntercae1.create 创建的,它又创建了不能接受 Tinterface 的 Iinterface。这让我想知道,如果从来没有使用过,Delphi 为什么还要费心创建一个 Tinterface?
猜你喜欢
  • 1970-01-01
  • 2011-06-27
  • 2011-04-10
  • 2012-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-18
  • 1970-01-01
相关资源
最近更新 更多