从Delphi dll到C#app的回调

ber*_*ert 6 c# delphi dll callback

在ac#application中,带参数的ac#-method将从delphi dll中调用:调用C#方法,但int参数未正确传输:某些"随机"值到达.

C#-method通过寄存器方法传递给delphi dll:

    [UnmanagedFunctionPointer(CallingConvention.ThisCall)]
    public delegate void ProcDelegate(int value);

    private static ProcDelegate procDelegate;

    [DllImport("CallbackTest.dll", CallingConvention = CallingConvention.StdCall)]
    public static extern void RegisterCallback(ProcDelegate callBackHandle, int value);


    public Form1()
    {
        InitializeComponent();

        procDelegate = new ProcDelegate(CalledFromDelphi);

        RegisterCallback(procDelegate, 10); // register in delphi dll


    }

    public static void CalledFromDelphi(int value)
    {
        MessageBox.Show("Value:" + value); // expect "10", but getting random value

    }
Run Code Online (Sandbox Code Playgroud)

这是delphi代码:

type TCallback = procedure(val: integer);
var callback : TCallback;
procedure RegisterCallback(aCallback : TCallback; value: integer); stdcall;
begin
  callback:=  aCallback; 
  ShowMessage('Inside Delphi:'+ IntToStr(value)); // successful ("10")

  callback(value);  // ...and test callback
end;

exports
  RegisterCallback;
Run Code Online (Sandbox Code Playgroud)

有趣的是:回调方法被调用两次(两次都是"随机"值到达)虽然它在代码中只被调用一次.之后应用程序崩溃退出代码(0xc0000005).

任何的想法?

Too*_*the 4

尝试

type TCallback = procedure(val: integer); stdcall;
Run Code Online (Sandbox Code Playgroud)