0

I have very strange problem and I hope somebody could explain it to me.

I created VCL app and FMX dll (just a dll with FMX form). I need to load DLL dynamically using LoadLibrary and FreeLibrary. There are two cases.

In first case my code is:

procedure TForm2.FormCreate(Sender: TObject);
begin
    Path := ExtractFilePath(Application.ExeName)+ 'plugins\';
  FLibHandle := LoadLibrary(PWideChar(Path + 'Plugin.dll'));
  if FLibHandle > 32 then
  begin
    @FOpenForm := GetProcAddress(FLibHandle, 'TestOpenGUI') ;
    @FCloseForm := GetProcAddress(FLibHandle, 'TestCloseGUI') ;  
  end;
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
  FreeLibrary(FLibHandle);
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
 FOpenForm;
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
  FCloseForm;
end;

The problem here is if I click button1 and then button2 (Just open the dll form and then close it) and close the app it will hang on FreeLibrary. I have to termiante the app (CTRL+F2).

In second case my code is:

procedure TForm2.Button1Click(Sender: TObject);
begin
 FOpenForm;
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
  FCloseForm;
end;

initialization
  Path := ExtractFilePath(Application.ExeName)+ 'plugins\';
  FLibHandle := LoadLibrary(PWideChar(Path + 'Plugin.dll'));
  if FLibHandle > 32 then
  begin
     @FOpenForm := GetProcAddress(FLibHandle, 'TestOpenGUI') ;
     @FCloseForm := GetProcAddress(FLibHandle, 'TestCloseGUI') ;  
  end;

finalization
  FreeLibrary(FLibHandle);

Library will be loaded and then if I click button1 and then button2 and close the app, library will be released without any problem. In this case everything works just fine.

I am a little bit confused and I don't know what am I doing wrong.

Thanks for your help.

Nix
  • 581
  • 5
  • 20
  • I don't think this is expected to work. FMX form in a DLL hosted. – David Heffernan Apr 29 '15 at 23:03
  • Here is a white paper written by Stephen Ball http://www.danysoft.com/free/firemonkey-vcl.pdf It should be easy. I just don't know if this paper is still valid while it's dated in 2012. Another thing is that FreeLibrary is working fine when used in Finalization section but that is not an option for me. – Nix Apr 30 '15 at 04:03
  • 1
    I'm sure we've had a question on this topic which concluded that what you are trying is not supported. – David Heffernan Apr 30 '15 at 08:21
  • Interesting. I totally believe you. But my guess is that there has been introduced some changes in Firemonkey since Delphi XE5. Great example is this one: http://www.devpage.de/blog/firemonkey-Teil7.htm It works with Delphi XE5. Now when I try this example with Delphi XE8 I got same problem with FreeLibrary call. It just hangs there. – Nix May 07 '15 at 15:06

0 Answers0