Firemonkey app fail when calling FreeLibrary

  

I have a simple Firemonkey app, that loads a DLL, call a procedure within that DLL, which just calls ShowMessage(‘Dialog from DLL …’); and returns
That all OK, but the calling application fails, when FreeLibrary has been called and I try closing the main app ?

If I leave out the FreeLibrary call, the application closes without error.

Anybody seen this before, and why ?

I’m using Delphi 10.1 Berlin, but Delphi 10 Seattle also fails on Windows 7.

The application code:

unit Unit2;

interface

uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.Controls.Presentation, FMX.StdCtrls;

type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.fmx}

uses
Winapi.Windows;
type
TTest = procedure (); stdcall;

procedure TForm2.Button1Click(Sender: TObject);
var
LibHandle: NativeUInt;
Test: TTest;
begin
LibHandle:= LoadLibrary(‘.TESTDLL.DLL’);
if LibHandle > 32 then try
@Test:= GetProcAddress(LibHandle, ‘Test’);
if @Test <> nil then
Test;
finally
FreeLibrary(LibHandle);
end;
end;

And the DLL code:

library TESTDLL;

uses
System.SysUtils,
System.Classes,
FMX.Dialogs
;

{$R *.res}

procedure Test; stdcall;
begin
ShowMessage(‘Dialog from DLL …’);
end;

exports
Test name ‘Test’;

begin
end.

If the dll procedure don’t contain any calls related to display things, the app behaves as expected.
Any hints help or suggestions, greatly appreciated.

Edit: Included the complete code from main app.

Comments are closed.