隐式链接与Delphi中DLL的显式链接

Tom*_*Tom 2 delphi dll

使用显式链接时,我无法使用dll工作.使用隐式链接可以正常工作.有人会给我一个解决方案吗?:)不,开个玩笑,这是我的代码:

这段代码工作正常:

function CountChars(_s: Pchar): integer; StdCall; external 'sample_dll.dll';

procedure TForm1.Button1Click(Sender: TObject);
begin   
  ShowMessage(IntToStr(CountChars('Hello world')));
end;
Run Code Online (Sandbox Code Playgroud)

此代码不起作用(我得到访问冲突):

procedure TForm1.Button1Click(Sender: TObject);
var
  LibHandle: HMODULE;
  CountChars: function(_s: PChar): integer;
begin

  LibHandle := LoadLibrary('sample_dll.dll');
  ShowMessage(IntToStr(CountChars('Hello world'))); // Access violation
  FreeLibrary(LibHandle);
end;
Run Code Online (Sandbox Code Playgroud)

这是DLL代码:

library sample_dll;

uses
  FastMM4, FastMM4Messages, SysUtils, Classes;

{$R *.res}

function CountChars(_s: PChar): integer; stdcall;
begin
  Result := Length(_s);
end;

exports
  CountChars;

begin  
end.
Run Code Online (Sandbox Code Playgroud)

Ond*_*lle 7

procedure TForm1.Button1Click(Sender: TObject);
var
  LibHandle: HMODULE;
  CountChars: function(_s: PChar): integer; stdcall; // don't forget the calling convention
begin
  LibHandle := LoadLibrary('sample_dll.dll');
  if LibHandle = 0 then
    RaiseLastOSError;
  try
    CountChars := GetProcAddress(LibHandle, 'CountChars'); // get the exported function address
    if not Assigned(@CountChars) then
      RaiseLastOSError;

    ShowMessage(IntToStr(CountChars('Hello world')));
  finally
    FreeLibrary(LibHandle);
  end;
end;
Run Code Online (Sandbox Code Playgroud)