在 Delphi 中使用来自 COM class 的方法
Using methods from a COM class in Delphi
我在 VB.NET 中创建了一个 COM class。我正在尝试在 Delphi 5.0.
中使用它
我看过一些关于这个话题的examples/questions,即:
Call C# dll from Delphi
http://www.drbob42.com/delphi/headconv.htm
http://edn.embarcadero.com/article/32754
和其他,但这些都是处理基本功能而不是自定义对象。
首先,我已经使用 regasm
.
注册了我的 VB.NET COM DLL
我的VB.NETCOM对象定义如下:
<ComClass(Bridge.ClassId, Bridge.InterfaceId, Bridge.EventsId)> _
Public Class Bridge
' A creatable COM class must have a Public Sub New()
' with no parameters, otherwise, the class will not be
' registered in the COM registry and cannot be created
' via CreateObject.
Public Sub New()
MyBase.New()
End Sub
Public Function Quote(ByRef input As InObject) As ReturnObject
BLABLA
End Function
End Class
输入 class:
<ComClass(InObject.ClassId, InObject.InterfaceId, InObject.EventsId)> _
Public Class InObject
End Class
结果class:
<ComClass(ReturnObject.ClassId, ReturnObject.InterfaceId, ReturnObject.EventsId)> _
Public Class ReturnObject
End Class
请不要注意 class 名称和其中缺少的代码。我只想强调我是如何将它们定义为 COM classes.
我找不到任何 Delphi 代码调用 COM class 中的方法的示例,或者使用自定义对象作为输入和 returns 的示例。但是,从我上面展示的例子来看,我认为 Delphi 中声明要使用的函数的行应该是这样的:
function Bridge.Quote(i: InObject): ReturnObject; external 'Bridge.dll';
编译失败。我得到一个错误:
function needs result type
有什么明显的地方我做错了吗?
要创建 ComObject,您有 2 个选项:
CoCreateInstance
https://msdn.microsoft.com/de-de/library/windows/desktop/ms686615(v=vs.85).aspx
这需要按照您之前的描述注册 ComObject。你可以这样称呼它:
CoCreateInstance(YourClassID, nil, CLSCTX_INPROC_SERVER, YourInterfaceID, Result)
您的 DLL 导出一个创建请求实例的函数。然后您可以在加载 DLL 时导入此函数。无需注册。
function MyFunc: IMyInterface; external 'MyDll.dll';
不要忘记使用两种语言都支持的合适的调用约定(如 stdcall 或 cdecl)
感谢 Alexander B 为我指明了正确的方向 - 我必须添加更多细节,否则我会 select 他的输入作为正确答案。
因此,正如我在评论中所述,我在 delphi GUI 中导入了 tld 文件并使用 CoClassName.Create 来访问对象。尽管由于应用程序找不到 VB.NET dll,这在 运行 时给我带来了错误,但我收到了错误消息 'The system cannot find the file specified',即使我已经使用 regasm 导入了 dll。然后我发现了以下 post
Call C# dll from Delphi
这表明标志 /codebase 必须添加到 regasm 命令中。一旦我使用带有此标志的 regasm 导入了 dll,一切都运行良好。希望这对其他人有帮助。
我在 VB.NET 中创建了一个 COM class。我正在尝试在 Delphi 5.0.
中使用它我看过一些关于这个话题的examples/questions,即:
Call C# dll from Delphi
http://www.drbob42.com/delphi/headconv.htm
http://edn.embarcadero.com/article/32754
和其他,但这些都是处理基本功能而不是自定义对象。
首先,我已经使用 regasm
.
我的VB.NETCOM对象定义如下:
<ComClass(Bridge.ClassId, Bridge.InterfaceId, Bridge.EventsId)> _
Public Class Bridge
' A creatable COM class must have a Public Sub New()
' with no parameters, otherwise, the class will not be
' registered in the COM registry and cannot be created
' via CreateObject.
Public Sub New()
MyBase.New()
End Sub
Public Function Quote(ByRef input As InObject) As ReturnObject
BLABLA
End Function
End Class
输入 class:
<ComClass(InObject.ClassId, InObject.InterfaceId, InObject.EventsId)> _
Public Class InObject
End Class
结果class:
<ComClass(ReturnObject.ClassId, ReturnObject.InterfaceId, ReturnObject.EventsId)> _
Public Class ReturnObject
End Class
请不要注意 class 名称和其中缺少的代码。我只想强调我是如何将它们定义为 COM classes.
我找不到任何 Delphi 代码调用 COM class 中的方法的示例,或者使用自定义对象作为输入和 returns 的示例。但是,从我上面展示的例子来看,我认为 Delphi 中声明要使用的函数的行应该是这样的:
function Bridge.Quote(i: InObject): ReturnObject; external 'Bridge.dll';
编译失败。我得到一个错误:
function needs result type
有什么明显的地方我做错了吗?
要创建 ComObject,您有 2 个选项:
CoCreateInstance
https://msdn.microsoft.com/de-de/library/windows/desktop/ms686615(v=vs.85).aspx
这需要按照您之前的描述注册 ComObject。你可以这样称呼它:CoCreateInstance(YourClassID, nil, CLSCTX_INPROC_SERVER, YourInterfaceID, Result)
您的 DLL 导出一个创建请求实例的函数。然后您可以在加载 DLL 时导入此函数。无需注册。
function MyFunc: IMyInterface; external 'MyDll.dll';
不要忘记使用两种语言都支持的合适的调用约定(如 stdcall 或 cdecl)
感谢 Alexander B 为我指明了正确的方向 - 我必须添加更多细节,否则我会 select 他的输入作为正确答案。
因此,正如我在评论中所述,我在 delphi GUI 中导入了 tld 文件并使用 CoClassName.Create 来访问对象。尽管由于应用程序找不到 VB.NET dll,这在 运行 时给我带来了错误,但我收到了错误消息 'The system cannot find the file specified',即使我已经使用 regasm 导入了 dll。然后我发现了以下 post
Call C# dll from Delphi
这表明标志 /codebase 必须添加到 regasm 命令中。一旦我使用带有此标志的 regasm 导入了 dll,一切都运行良好。希望这对其他人有帮助。