I am trying to show a modal (FMX) form from a DLL. Eventually this will be ported to OSX so I'm trying to keep the 'Windows' code to a minimum. Based on what's in the forum I have tried to keep the code as simple possible.
I know that the first time showmodal is called, the code works as expected, but if I subsequently call the DLL function, I get an AV trying to create the form. I then tried to (without exiting the function) create and free the form several times, this works without issue. And it works if I call the function, create and free the form and DO NOT showmodal. In all my tests, if I showmodal I get the AV trying to create the form the second time and the application hangs.
Of course, if I just show the form I do not get the issue, ie. I can create the form, show it and free it in a loop and it works fine. The issue is of course that the code just continues through destroying the form and I need the form to be modal.
THE DLL EMPSECURE_UI.DLL
function dAbout_UI:boolean; {$IFDEF MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF} export;
var
dAbout: TfAbout;
begin
try
try
Result := True;
dAbout := TfAbout.Create(nil);
dAbout.Showmodal;
except
Result := False;
end;
finally
FreeandNil(dAbout);
end;
The UNIT in the ABOUT Form
procedure TfAbout.bOKClick(Sender: TObject);
begin
ModalResult := mrOK;
Close;
end;
procedure TfAbout.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := System.UITypes.TCloseAction.caFree;
end;
The Test program that calls the function in the DLL uTESTEMPSECURE_UI.pas, ie. the main program, the simple call to the function that displays the modal form:
const
{$IFDEF MSWINDOWS}
EMPSecureUI = 'empsecure_ui.dll';
{$ENDIF MSWINDOWS}
{$IFDEF MACOS}
EMPSecureUI = 'libempsecure_ui.dylib';
{$ENDIF MACOS}
// EMPSecure_UI Functions
function dAbout_UI: boolean; {$IFDEF MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF}; external EMPSecureUI;
procedure TForm1.Button1Click(Sender: TObject);
begin
dAbout_UI;
end;
So the first time I run this code and call the function which shows the modal form it works as expected, a modal form is shown. When I click OK on the form, the form is freed and control is returned to the main test program. This is exactly as expected.
If I then execute a second call to the DLL function an AV occurs when trying to create the form the second time. IF, OTOH, the form is simply shown (not modal), no AV occurs when I try to create the form a second or subsequent time.