【问题标题】:how to call event of delphi dll at c#?如何在c#中调用delphi dll的事件?
【发布时间】:2018-03-16 06:48:21
【问题描述】:

Delphi7 test.dll

unit DLLFunction;
interface
uses
  Sysutils, classes,  Dialogs;

type
  TEvent = procedure(index, status, percent: integer) of object; stdcall;
  IMyDll = interface
  ['{42C845F8-F45C-4BC7-8CB5-E76658262C4A}']
    procedure SetEvent(const value: TEvent); stdcall;
  end;

  TMyDllClass = class(TInterfacedObject, IMyDll)
  public
    procedure SetEvent(const value: TEvent); stdcall;
  end;

  procedure CreateDelphiClass(out intf: IMyDll); stdcall;

implementation
procedure TMyDllClass.SetEvent(const value: TEvent); stdcall;
begin
  if Assigned (value) then
  begin
    ShowMessage('Event call');
    value(1,2,3);
  end;
end;
exports
  createDelphiClass;
end.

C# 源代码

// event
  public delegate void TEvent(int index, int status, int percent);

  [ComImport, Guid("42C845F8-F45C-4BC7-8CB5-E76658262C4A"),
  InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  public interface IMyDll
  {
    // event
    [MethodImplAttribute(MethodImplOptions.PreserveSig)]
    void SetEvent([MarshalAs(UnmanagedType.FunctionPtr)]TEvent eventCallback);

  }

  class TestInterface
  {
    const string dllPath = "testDelphiDll.DLL";

    [DllImport(dllPath, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
    public static extern void CreateDelphiClass(out IMyDll dll);

    public IMyDll getDelphiInterface()
    {
      IMyDll mydll = null;
      CreateDelphiClass(out mydll);

      return mydll;
    }
  }

然后在c#中使用

  TestInterface tInterface = new TestInterface();
  delphi = tInterface.getDelphiInterface();

  delphi.SetEvent(delegate(int index, int status, int percent)
  {
    MessageBox.Show("index: " + index.ToString() + "\nstatus: " + status.ToString() + " \npercent: " + percent.ToString());
  });

结果

索引:-19238192731

状态:1

百分比:2

然后崩溃应用程序。 例外:

“System.AccessViolationException”类型的未处理异常 发生在 DLLCallTest.exe 中。

提示:您试图读取或写入受保护的内存。大多数情况下, 这表明其他内存已损坏。

我认为这不是问题,但是为什么会出现这个异常和错误的参数?

【问题讨论】:

    标签: c# delphi dll


    【解决方案1】:

    TEvent 的 Delphi 声明是 of object。这与您的 C# 不匹配。 of object 方法类型是一个包含实例引用和代码地址的双指针。您的 C# 委托是带有代码地址的单个指针。

    删除of object,您的程序就可以运行了。

    【讨论】:

    • 如果我使用'of object',那么如何在c#中使用delegate?
    • 没有支持的方式。你必须破解。
    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多