首页 > 解决方案 > Delphi 访问冲突调用实现接口的 C++ DLL

问题描述

为了在 Delphi 中导入 DLL,我编写了一个翻译 .h 文件的单元。这个 DLL 允许我定义一个过程来监视两个值并在我的应用程序中使用它们。

原始 .h 文件是:

class MyINTERFACE
{
   public:
   virtual void MonitorProc(int value1, int value2) = 0;
};

extern MY_API MyHANDLE* Init();
extern MY_API UINT InitMonitorProc(MyHANDLE * handle, MyINTERFACE * pMyProc);

这是我的翻译:

unit myCdllUnit;

interface

const

 TMyHANDLE = DWORD;  

 MyINTERFACE = class
 public
   procedure MonitorProc(value1: longint;  value2: longint); virtual; cdecl; abstract;
 end;

function Init(): TMyHANDLE; cdecl; external 'myCdll.dll'; 
function InitMonitorProc(handle: TMyHANDLE; pMyProc: MyINTERFACE):DWORD; cdecl; external 'myCdll.dll';

implementation

end.

在我的主要形式中,我覆盖了 MonitorProc(最初仅使用“继承”来测试调用)

TMyMonitor = class(MyINTERFACE)
public
procedure MonitorProc(value1: longint; value2: longint); override;
end;

procedure TMyMonitor.MonitorProc(value1: longint;  value2: longint);
begin
  inherited;
end;   <--------------------------- here get an Access Violation

在我的 FormCreate 中,我将一个 istance 传递给 InitMonitorProc

MyMonitor := TMyMonitor.Create;
InitMonitorProc(Handle, MyMonitor);

DLL 调用正确但退出 MonitorProc 我收到访问冲突错误。我哪里错了?

标签: c++delphidllaccess-violation

解决方案


推荐阅读