c# - 非托管导出和委托(delegate)

标签 c# delphi dll delphi-7

我正在使用 Robert Giesecke Unmanaged Exports 创建一个 .NET 包装器 DLL 以在 Delphi 7 中使用 .NET DLL。到目前为止一切正常,但现在我有一个需要回调/委托(delegate)的函数。

如何做到这一点?

我可以只将函数指针提供给我的 .NET DLL 并从那里调用它吗?如果可以,它是如何完成的?

最佳答案

这很简单。您可以像使用标准 p/invoke 一样定义委托(delegate)类型。

这是我能想到的最简单的例子:

C#

using RGiesecke.DllExport;

namespace ClassLibrary1
{
    public delegate int FooDelegate();

    public class Class1
    {
        [DllExport()]
        public static int Test(FooDelegate foo)
        {
            return foo();
        }
    }
}

德尔福

program Project1;

{$APPTYPE CONSOLE}

type
  TFooDelegate = function: Integer; stdcall;

function Test(foo: TFooDelegate): Integer; stdcall; external 'ClassLibrary1.dll';

function Func: Integer; stdcall;
begin
  Result := 666;
end;

begin
  Writeln(Test(Func));
end.

输出

666

C# 端的默认调用约定是 CallingConvention.Stdcall 所以我同意了。这是显而易见的事情。

关于c# - 非托管导出和委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24433977/

相关文章:

c# - 文本区域内的有序列表(行号)

c# - 在ASP.NET MVC 4应用程序中检查SSL协议(protocol),密码和其他属性

c++ - 我可以用 Delphi 2010 编写 Windows 驱动程序吗?

delphi - 在Delphi中建立大型软件系统

qt - 如何将 Windows 上的 opencv DLL 添加到 QTCreator 项目中?

c# - Ajax 发布到 ASP.net MVC Controller - 对象属性为空

c# - 如何防止在级联运算符中创建中间对象?

delphi - 在RAD Studio中检查更新的问题

c - 有没有办法通过名称获取 Windows 中当前进程的函数?

c# - 在 C# 程序中使用 C/C++ 数据结构