我用单一方法编写了.Net 3.5 dll,由Delphi .exe调用。 不幸的是它不起作用。
步骤: 1. 使用代码创建 C# 3.5 dll:
public class MyDllClass
{
public static int MyDllMethod(int i)
{
MessageBox.Show("The number is " + i.ToString());
}
}
这会抛出 Delphi 异常,表明它无法连接 dll。 从非托管代码启用 C# 托管 dll 的使用需要执行哪些步骤。
有人熟悉有关该主题的好例子吗?
谢谢你
通过使用我的用于非托管导出的项目模板,您可能会更幸运地跳过 COM 部分
class MyDllClass
{
[DllExport]
static int MyDllMethod(int i)
{
MessageBox.Show("The number is " + i.ToString());
return i + 2;
}
}
在 Delphi 中,您可以像这样导入它:
function MyDllMethod(i : Integer) : Integer; stdcall; extern 'YourAssembly.dll';
不过,我不得不否决你的问题。甚至不关心提供可以编译的代码。 (您的 C# 方法不返回值,但它期望为 int)
经过大量调查,我找到了解决方案:这都是关于注册参数的。 必须将标志
/codebase
添加到 regasm
命令中。
许多帖子建议在 C# Com 公开对象上使用
Guid
和其他 COM 属性,我设法使用 ComVisible(true)
属性和 regasm /tlb /codebse
命令提供 COM 功能。
代码:
[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
来注册它
如果您将程序集制作为 x64 或 AnyCPU,则可能会出现问题。由于 Delphi 是 32 位 (x86),因此您需要将程序集设为 x86 或确保 regasm.exe 也在 32 位注册表中注册它。您可以使用 x86 版本的 regasm.exe 来完成此操作。