2

I'm starting a FireMonkey application in Delphi XE7 that must call some DLL's which have themselves forms (also created in Delphi XE7).

I load the DLL and call my exported procedure (no parameters), and the form loads and works as expected (ShowModal). I close that form and the main application resumes and works perfectly, until the moment the user terminates the application and the FreeLibrary is called. An Access Violation is then raised with the following message:

"First chance exception at $045B30E6. Exception class $C0000005 with message 'access violation at 0x045b30e6: read of address 0x00000008'. Process bpltesteVCL.exe (6708)"

I ran some tests with a basic test application and found out it only happens if the DLL has a FireMonkey TForm - VCL runs fine.

These are the combinations I tried in my testings with virtually the same code: VCL App/VCL Dll = OK; VCL App/FMX Dll = Fail; FMX App/FMX Dll = Fail; FMX App/VCL Dll = OK. These were only for testing purposes, since my goal is a full FMX application.

The code I used to load the library:

procedure TForm2.Button1Click(Sender: TObject);
type
  TFExecute = procedure; StdCall;
var
  Execute: TFExecute;
  Ext: HMODULE;
begin
  Ext := LoadLibrary(PChar(ExtractFilePath(ParamStr(0)) + 'dlltest.dll'));

  if Ext <> 0 then
  begin
    try
      @Execute := GetProcAddress(Ext, 'Execute');
      if Assigned(Execute) then
         Execute
      else
        ShowMessage('Function not found !');
    finally
      FreeLibrary(Ext);
    end;
  end
  else
    ShowMessage('DLL not found');
end;

And the Dll code for 'Execute':

procedure Execute; StdCall;
begin
     fMain := TfMain.Create(nil);
     fMain.ShowModal;
     fMain.Free;
end;

Exports
       Execute;

The DLL form has no component inside. Only the plain form. Also, no units were added to it that were not needed to load the form itself.

I have been searching and found some similar cases, and the problem seems to be the GDI+ initialization and finalization, but unfortunately, the answers there don't work for me:

FMX form in a DLL (firemonkey/delphi)

This couldn't help me, because in this method the dll is loaded in the initialization section of a unit, making it in fact similar to static loading, which I cannot use. Still I tried it for the sake of testing, and it crashed with an Access Violation in the same maner.

FireMonkey Form in a dll, loaded from a VCL Application

This was in fact the closest I got to a solution to my problem, but unfortunately, the solution given here seems to work for XE2, but not for later versions of Delphi, because one of the most important methods used here is GetMeasureBitmap, which only exists (by that name at least) in XE2.

I did search for an equivalent in XE7 so I could find the solution before posting the question, but couln't get anything of the sort.

Is there any way to load FMX forms in a dll in Delphi XE7?

EDIT: Continuing the research for a solution, I tried the following:

  • All the mentioned combinations with BPL instead of DLL
  • Usage of ShareMem / FastShareMem

No change on the mentioned behaviour!

Community
  • 1
  • 1
nunopicado
  • 307
  • 4
  • 17
  • There are two FMX instances. Probably one too many. Do you need to use DLLs? If so, why? – David Heffernan Feb 24 '15 at 05:50
  • Hi David. I need dll's because this is for a plugin system, similar to the [one you and Remy helped me out a while back](http://stackoverflow.com/questions/21540276/delphi-interfaces-and-dlls?noredirect=1#comment32611036_21540276) but this time in a FMX application. I did try it with BPL's instead, but the problem is the same. And even with the main application in VCL and the dll/bpl in FMX (hence, only one instance of each), it still fails. I could do the plugins in VCL, but that would limit my options in terms of platforms supported, therefore I'm searching for a FMX solution. – nunopicado Feb 24 '15 at 10:51
  • 1
    It looks like this sort of usage is simply not supported by the framework design. – David Heffernan Feb 24 '15 at 10:58
  • Do you think maybe a bug in the framework itself? – nunopicado Feb 24 '15 at 11:11
  • 1
    If not a bug, then just a consequence of a design choice. I couldn't get as far as you did. When I closed the modal form, the main form of the host became unresponsive. – David Heffernan Feb 24 '15 at 11:15
  • That did happened to me once, during testing, when I tried using Release insted of Free inside the DLL. – nunopicado Feb 24 '15 at 11:17
  • Just an update (not a complete solution, so I won't put it as an answer): Under Delphi 10 Seattle (at least), using BPL's with runtime packages turned on, it is now possible to use external forms without crashing. No change in DLL use yet. – nunopicado Dec 17 '15 at 10:28

0 Answers0