从 Delphi 调用 C# dll [英] Call C# dll from Delphi
问题描述
我用单一方法编写了 .Net 3.5 dll,该方法将由 Delphi .exe 调用.不幸的是它不起作用.
I composed .Net 3.5 dll with single method, which is to be called by Delphi .exe. Unfortunately it does not work.
步骤:1. 使用以下代码创建 C# 3.5 dll:
The steps: 1. Create C# 3.5 dll with the code:
public class MyDllClass
{
public static int MyDllMethod(int i)
{
MessageBox.Show("The number is " + i.ToString());
}
}
- 转到程序集属性 --> 程序集信息并选中使程序集 COM 可见"
- 使用 RegAsm.exe 注册我的 dll
这会引发 Delphi 异常,表明它无法连接 dll.从非托管代码启用 C# 托管 dll 的使用需要哪些步骤.
This throws Delphi exception which indicates it cannot connect the dll. What are the steps required to enabled usage of C# managed dll from unmanaged code.
是否有人熟悉有关该主题的好例子?
Does any one familiar with good example about the subject?
谢谢
推荐答案
经过大量调查,我找到了解决方案:一切都与注册参数有关.标志/codebase 必须添加到 regasm 命令中.
After massive investigation I found the solution: it's all about registration parameters. The flag /codebase must be added to the regasm command.
许多帖子建议在 C# Com 公开对象上使用 Guid 和其他 COM 属性,我设法使用 ComVisible(true) 属性和 regasm/tlb/codebse 命令提供 COM 功能.
Many posts out there suggest to use Guid and other COM attributes on the C# Com exposed object, I managed to deliver COM functionality using the ComVisible(true) attribute and regasm /tlb /codebse command.
代码:
[Guid("7DEE7A79-C1C6-41E0-9989-582D97E0D9F2")]
[ComVisible(true)]
public class ServicesTester
{
public ServicesTester()
{
}
//[ComVisible(true)]
public void TestMethod()
{
MessageBox.Show("You are in TestMEthod Function");
}
}
正如我提到的,我使用 regasm.exe/tlb/codebase 来注册它
and as I mentioned I used regasm.exe /tlb /codebase to register it
这篇关于从 Delphi 调用 C# dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!