DLL:
Code: Alles auswählen
===callblib.lpr:===
library callblib;
{$mode objfpc}{$H+}
uses
Classes, callblibunit
{ you can add units after this };
{$R *.res}
exports GetNr;
exports SetCallProc;
exports TestCall;
begin
end.
====callblibunit.pas:=====
unit callblibunit;
{$mode objfpc}{$H+}
interface
uses
Classes;//, SysUtils;
type
TCallFromDllProc = procedure(ANr: longint);//auch mit stdcall; gibts "External: SIGSEGV"
var
ProgPoc: TCallFromDllProc;
TestItNr : integer =101;
function GetNr:Integer; stdcall;
procedure SetCallProc(FromProgram: TCallFromDllProc); stdcall;
procedure TestCall; stdcall;
implementation
function GetNr:Integer; stdcall;
begin
Result:=TestItNr;
end;
procedure SetCallProc(FromProgram: TCallFromDllProc); stdcall;
begin
ProgPoc:=FromProgram;
end;
procedure TestCall; stdcall;
begin
if @ProgPoc<>nil then TCallFromDllProc(ProgPoc)(TestItNr);
//für delphi: if @ProgPoc<>nil then TCallFromDllProc(ProgPoc)(TestItNr);
end;
initialization
finalization
end.
Code: Alles auswählen
===callblibtest.lpr=========
program callblibtest;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes
{ you can add units after this },
SysUtils;
{$R *.res}
type
TCallFromDllProc = procedure(ANr: longint); // auch mit stdcall; gibts "External: SIGSEGV"
function GetNr:Integer; stdcall; external 'callblib.dll' name 'GetNr';
procedure SetCallProc(FromProgram: TCallFromDllProc); stdcall; external 'callblib.dll' name 'SetCallProc';
procedure TestCall; stdcall; external 'callblib.dll' name 'TestCall';
procedure ProgCallProg(ANr: longint);
begin
Writeln('Wurde von Dll-Aufgerufen: '+IntToStr(ANr));
end;
begin
SetCallProc(@ProgCallProg);
Writeln('Ich rufe auf: '+IntToStr(GetNr));
TestCall; //ereignis simulieren
Writeln('[ENTER] drücken zum beenden: ');
Readln;
SetCallProc(nil);
Writeln('.. und tschüsssssss. ');
end.
Fehlermeldung 'Exception-Klasse "External: SIGSEGV" ausgelöst.' erscheint nur wenn man das Testprogramm von ide startet.