在 Delphi 中使用 ATL 组件

using ATL component in Delphi

[改写得更容易理解]

我在尝试使用 Delphi 中的 COM 对象时迷路了。该对象是使用 Visual Studio ATL 创建的。

我已经在 VBScript 中测试了该对象:

Set ourObj = Server.CreateObject( "JC_ATL_Q.JCsimple")
ourJCversion = ourObj.JCversion
Response.Write "<li>Get: JCversion = " + cstr( ourJCversion)

在Delphi,我关注了:

http://101.lv/learn/delphi/ch15.htm1

创建下面的代码,但这会产生错误

"The specified procedure could not be found"

我检查了注册表,TGUID 似乎是正确的。

我访问 IDispatch 界面的做法是否正确?有没有办法获得有关问题所在的更多信息?

IJCsimple = interface(IUnknown)
    ['{96154141-4169-4321-BADC-A08F1B2D53A4}']
    function get_JCversion: Integer; stdcall;
  end;

const
CLASS_JCsimple: TGUID = '{6724FCDA-14F6-4D40-82A1-C8FD451BED9C}';

var
  Form2: TForm2;
  Result : variant;

implementation

procedure TForm2.Button1Click(Sender: TObject);
  begin

    Result := CreateComObject(CLASS_JCsimple) as IJCsimple;

  end;

end.

解法: 正如 Remy Lebeau 指出的那样,我不应该重新发明轮子,而应该使用组件 >> 导入组件 >> 导入类型库工具

您对接口方法使用了错误的声明。它应该看起来更像这样:

type
  IJCsimple = interface(IUnknown)
    ['{DA13AEFD-C5A1-4A94-AD74-B9C355E1F19C}']
    function get_Version(out Result: Integer): HResult; stdcall;
    property Version: Integer read get_Version;
  end;

或者这样:

type
  IJCsimple = interface(IUnknown)
    ['{DA13AEFD-C5A1-4A94-AD74-B9C355E1F19C}']
    function Version: Integer; safecall;
  end;

也就是说,您不应该一开始就手动实现接口单元。正确的解决方案是让 IDE 导入 COM 对象的类型库并为您生成适当的包装代码:

Importing Type Library Information