FireMonkey DLL doesn't stay active

I have a VCL that calls a DLL written in FireMonkey.

When I call it, it immediately returns, rather than staying active. It should stay active, as it has a lot to do, and much user interface to handle.
library LangDLL;
{$R *.dres}
  uMMain in 'uMMain.pas';
{$R *.res}
procedure ShowMobile(Language: ShortString; Directory: ShortString;
      AppDir: ShortString); export;
  MM: TfMMain;
  test: string;
  test := 'OK';
  MM := TfMMain.Create(application);
  MM.DLL := true;
  MM.AppDir := AppDir;
  MM.Language := Language;
  MM.SubDir := Directory;
  test := 'Failed';

Open in new window

Also, even though the debugger shows that the logic is passing through the Free command, the FireMonkey form appears to persist, and then the system destroys it a few seconds after the parent program has ended.

When the code in the DLL is compiled as a stand-alone Windows32 exe, it works fine.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamDLovellAuthor Commented:
Here is progress so far:

Both PChar and ShortString don't seem to work in passing parameters to FireMonkey. I changed them to string, and put ShareMem in both the DLL library and the calling program's uses clause (as instructed in the DLL). After I did this the variables passed to FireMonkey OK.

When the variables were correctly passed, the DLL didn't (invisibly) crash, and so ShowModal worked as expected. (I need more error checking in the DLL!)

After exiting ShowModal, the program passes through MM.Free, but doesn't actually release the form. It stays current even after the calling program is closed. My best guess is something is still un-freed in the DLL - but if anyone has a better idea, please let me know.
GrahamDLovellAuthor Commented:
Is there a way in the IDE to list all the resources that have not yet been "freed" at any moment in time? In the meantime, I will make a list of them all, and check each one.
GrahamDLovellAuthor Commented:
Next result - The Fire Monkey DLL does not appear to have any "un-freed" resources at the time of closing, but after going through that , it loops through procedures in FMX.Platform.Win trying to close everything down, but apparently not succeeding.

Any suggestions?
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

GrahamDLovellAuthor Commented:
Even cutting down the FMX DLL to a single TEdit does not fix the problem of the DLL not closing its Windows interface.

I have a work-around for my particular application, a simple one - don't use the DLL method, use ExecuteShell.

Here is my function, copied and cosmetically modified from
function TfMain.ExecuteShell(ProgStr, Params: string): boolean;
  SEInfo: TShellExecuteInfo;
  ExitCode: DWORD;
  FillChar(SEInfo, SizeOf(SEInfo), 0) ;
  SEInfo.cbSize := SizeOf(TShellExecuteInfo) ;
  with SEInfo do
    Wnd := Application.Handle;
    lpFile := PChar(ProgStr) ;
    { ParamString can contain the application parameters. }
    if Params <> '' then
      lpParameters := PChar(Params) ;
    { StartInString specifies the name of the working directory.
      If ommited, the current directory is used. }
    // lpDirectory := PChar(StartInString) ;
    nShow := SW_SHOWNORMAL;
  result := true;
  if ShellExecuteEx(@SEInfo) then
      GetExitCodeProcess(SEInfo.hProcess, ExitCode) ;
    until (ExitCode <> STILL_ACTIVE) or Application.Terminated;
    ShowMessage('Error starting this process') ;
    result := false;

Open in new window

If there is a fix / workaround for my FMX DLL problem, please reply.
Sinisa VukSoftware architectCommented:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GrahamDLovellAuthor Commented:
My original question was fixed by fixing a bug in my own code (as I noted above), but Sinisa's sample app is very helpful in writing the interface for FireMonkey DLLs, supplementing the demonstration code at Here the finalization routine is either wrong or out-of-date (for XE6) or there is something else going on about which I know nothing. In any event, removing the finalization code fixed the problem.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.