Implicit linking vs. explicit linking of DLL in Delphi

Posted by Tom on Stack Overflow See other posts from Stack Overflow or by Tom
Published on 2010-04-20T08:29:05Z Indexed on 2010/04/20 8:33 UTC
Read the original article Hit count: 185

Filed under:
|

I'm having trouble getting my dll to work when using explicit linking. Using implicit linking it works fine. Would someone google me a solution? :) No, just kidding, here's my code:

This code works fine:

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

procedure TForm1.Button1Click(Sender: TObject);
begin   
  ShowMessage(IntToStr(CountChars('Hello world')));
end;

This code doesn't work (I get an access violation):

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;

This is the DLL code:

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.

© Stack Overflow or respective owner

Related posts about delphi

Related posts about dll