我正在使用 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/