来自DLL的Delphi接口

Dan*_*ker 2 delphi dll interface loadlibrary

使用Delphi XE.

当尝试从DLL访问Delphi接口对象时,如果我尝试动态地而不是静态地执行它,则会失败.

dll中的接口单元实现了一个返回接口实例的函数.静态链接时,进入函数时结果为零,一切正常.当动态加载时,结果是非零的,所以当完成对结果的赋值时,IntFCopy代码将其视为非零,因此在赋值之前尝试释放它,这会引发异常.

任何见解将不胜感激.

DLL包括testinterfaceload_u并导出testInt:

library testinterfaceload;

uses
  SimpleShareMem,
  SysUtils,
  Classes,
  testinterfaceload_u in 'testinterfaceload_u.pas';

{$R *.res}
exports testInt;

begin
end.
Run Code Online (Sandbox Code Playgroud)

testinterfaceload_u是定义接口和简单类实现的单元:

unit testinterfaceload_u;

interface

type ITestInt = interface
 procedure Test;
end;

{this function returns an instance of the interface}
function testInt : ITestInt; stdcall; export;

type

TTestInt = class(TInterfacedObject,ITestInt)
 procedure Test;
end;



implementation

function testInt : ITestInt;
begin
//debugger shows result as non-nil ITestInt even before this assignment, when dynamic
  result := TTestInt.Create;  
end;

procedure TTestInt.Test;
var
  i : integer;
begin
  i := 0;
end;



end.
Run Code Online (Sandbox Code Playgroud)

这是一个控制台应用程序,它加载dll并调用testInt函数来返回接口:

program testload_console;

{$APPTYPE CONSOLE}

uses

SysUtils,
  Windows,
  testinterfaceload_u in 'testinterfaceload_u.pas';

type
  TTestInt = function() : ITestInt;

var
   TestInt: TTestInt;
   NewTestInt : ITestInt;
   DLLHandle: THandle;
begin
  DLLHandle := LoadLibrary('testinterfaceload.dll');
  if (DLLHandle < HINSTANCE_ERROR) then
       raise Exception.Create('testinterfaceload.dll can not be loaded or not found. ' +     SysErrorMessage(GetLastError));
  @TestInt := GetProcAddress(DLLHandle, 'testInt');
  try
    if Assigned(TestInt) then
      NewTestInt := TestInt;
  except on e:Exception do
    WriteLn(Output,e.Message);
  end;
end.
Run Code Online (Sandbox Code Playgroud)

Dav*_*nan 5

TTestInt需要stdcall在导入DLL的代码中声明.


小智 5

您的接口定义应包含GUID,每个函数都需要"stdcall"声明.没有它你可能会遇到问题..

type ITestInt = interface
  ['{AA286610-E3E1-4E6F-B631-F54BC6B31150}']
  procedure Test; stdcall
end;
Run Code Online (Sandbox Code Playgroud)