Das mit der Speicherzugriffsverletung ist mir schon klar. Nur weiß ich nicht wo die passiert. Dachte, dass das evtl. ein generelles Problem bei der Programmierung mit FPC ist. Deswegen auch die Frage nach dem Beispiel.
Aber um diesen konkreten Fall mit mehr Hintergrund zu füllen:
Das Programm:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
dynlibs;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
private
{ private declarations }
dll: THandle;
hello: function (name: string): string; cdecl;
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
Label1.Caption := hello('Du');;
end;
procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
FreeLibrary(dll);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
dll := LoadLibrary('project1.' + SharedSuffix);
Pointer(hello) := GetProcedureAddress(dll, 'hello');
end;
end.
Und die dll:
library testlib;
{$mode objfpc}{$H+}
uses
Classes
{ you can add units after this };
{$R *.res}
function hello (name: string): string; cdecl;
begin
hello := 'Hallo ' + name;
end;
exports
hello;
end.