c# - 从 Delphi 调用 C# dll

标签 c# .net delphi managed-code

我用单一方法编写了.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());
    }
}
  1. 转到 Assembly Properties --> Assembly Information 并选中“Make Assembly COM-Visible”
  2. 使用 RegAsm.exe 注册我的 dll

这会抛出 Delphi 异常,表明它无法连接 dll。 从非托管代码启用 C# 托管 dll 的使用需要哪些步骤。

有没有人熟悉关于这个主题的好例子?

谢谢

最佳答案

使用我的 project template for unmanaged exports 跳过 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)

关于c# - 从 Delphi 调用 C# dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6174584/

相关文章:

delphi - 从Delphi控件中删除 "3D look"

c# - 如何模拟 ModuleClient?

C# 定时任务系列

.net - 有没有办法将 .Net ILookup<TKey, TElement> 展平为 List<TElement>?

c# - 无法访问已处置的对象。对象名称 : 'System.Net.HttpListener'

c# - 如何检查我是否可以对给定类型的值求和

delphi - DevExpress QuantumGrid 的 "Hello, world!"示例?

c# - 使用 PrincipalPermission 强制执行正确的 IPrincipal 和 IIdentity

c# - 从 TextBox 打印多页

C#.Net Windows 消息到 Delphi