unit uDYLIBTestAPP;
{$mode objfpc}{$H+}
interface
uses
Classes,
SysUtils,
Buttons,
Controls,
Stdctrls,
Dialogs,
Forms,
Menus;
type
{ TfDylibTestApp }
TfDylibTestApp = class(TForm)
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
MainMenu1: TMainMenu;
Memo1: TMemo;
Preferences: TMenuItem;
MenuItem2: TMenuItem;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
public
end;
(* {$ifdef mswindows}{$ifdef wince}{$calling cdecl}{$else}{$calling stdcall}{$endif}{$elsif darwin}{$calling cdecl}{$else}{$calling default}{$endif}*)
const
{$IFDEF MSWINDOWS}
TestDLL = 'pdylibtest.dll';
preName = '';
{$ENDIF MSWINDOWS}
{$IFDEF DARWIN}
TestDLL = 'libpdylibtest.dylib';
{$linklib libpdylibtest.dylib}
preName = '_';
{$ENDIF DARWIN}
// External function (DYLIB)
{$IFDEF MSWINDOWS}
function sayHello(Hello: string): boolean; stdcall; external TestDLL;
{$ENDIF MSWINDOWS}
{$IFDEF DARWIN}
function sayHello(Hello: string): boolean; cdecl; external TestDLL;
{$ENDIF DARWIN}
// Internal function
function localFunction(localIn: string; out localOut: string): boolean;
var
fDylibTestApp: TfDylibTestApp;
implementation
function localFunction(localIn: string; out localOut: string): boolean;
begin
LocalOut := 'This was passed to the function: ' + LocalIn;
Result := True;
end;
{$R *.lfm}
{ TfDylibTestApp }
procedure TfDylibTestApp.BitBtn3Click(Sender: TObject);
begin
// Quit the test application
showmessage('[DYLIBTestApp] Closing application');
fDylibTestApp.close;
Application.Terminate;
end;
procedure TfDylibTestApp.FormCreate(Sender: TObject);
begin
showmessage('[DYLIBTestApp] FormCreate');
end;
procedure TfDylibTestApp.FormDestroy(Sender: TObject);
begin
showmessage('[DYLIBTestApp] FormDestroy');
end;
procedure TfDylibTestApp.BitBtn2Click(Sender: TObject);
var
sOut: string;
begin
// This is a local function
ShowMessage('[DYLIBTestApp] Local Function Call');
if localFunction('Local in', sOut) then
showmessage('[DYLIBTestApp] Local Function: TRUE (' + sOut + ')')
else
showmessage('[DYLIBTestApp] Local Function: FALSE');
end;
procedure TfDylibTestApp.BitBtn1Click(Sender: TObject);
var
b:boolean;
sType: string;
sDLLString: string;
begin
b := False;
// Call the DLL Function
{$IFDEF MSWINDOWS}
sType := 'Windows DLL';
sDLLString := 'The string passed to the Windows DLL';
{$ENDIF MSWINDOWS}
{$IFDEF DARWIN}
sType := 'macOS DYLIB';
sDLLString := 'The string passed to the macOS DYLIB';
{$ENDIF DARWIN}
b := sayHello(sDLLString);
if b then
showmessage('[DYLIBTestApp] Returned From: ' + sType + ': TRUE')
else
showmessage('[DYLIBTestApp] Returned From: ' + sType + ': FALSE');
end;
end.