Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Show FMX Form inside a dll from VCL app


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 4 - Last Post: Mar 15, 2016 1:42 PM Last Post By: David Millington
Ahmed Sayed

Posts: 173
Registered: 8/9/07
Show FMX Form inside a dll from VCL app  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 14, 2016 10:10 AM
Hello everyone,

I spent the whole day trying to make it work but with no luck at all. As the subject says i am trying to create a firemonkey form inside a dll and call it from a vcl app.

Everything works fine until i call FreeLibrary function i get an access violation.

So i was kinda hopping for someone to help me with this issue because i think its a bug.

here is the code:

//Dll unit
 
#include <fmx.h>
 
#include <FMX.Dialogs.hpp>
#include <FMX.StdCtrls.hpp>
#include <FMX.Forms.hpp>
#include "Unit1.h"
 
#pragma hdrstop
#pragma argsused
 
#define DLLEXPORT __declspec(dllexport) __stdcall
 
extern "C" {
 
void DLLEXPORT ShowFmxForm()
	{
	TForm1 *Frm = new TForm1(Application);
	try
		{
		Frm->ShowModal();
		}
	__finally
		{
        Frm->Free();
        }
    }
}
 
extern "C" int _libmain(unsigned long reason)
{
	return 1;
}
//---------------------------------------------------------------
 
//VCL Host App
 
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
DllFile = ParamStr(1);
LoadDll( Dll,DllFile);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 
 
if (Dll)
	{
	typedef void (*__declspec(dllimport) __stdcall TShowFmxForm)();
 
	TShowFmxForm ShowFmxForm = NULL;
 
	ShowFmxForm = (TShowFmxForm)GetDllFunc(Dll,"ShowFmxForm");
 
	ShowFmxForm();
	}
 
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
UnLoadDll(Dll);
}
//---------------------------------------------------------------------------
 


Any ideas what could be wrong? I tried it with and without linking with run-time packages.

I don't know how to do this, any help will be appreciated.

Thanks
--
The limits of my language mean the limits of my world
David Millington

Posts: 257
Registered: 5/29/05
Re: Show FMX Form inside a dll from VCL app  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 14, 2016 1:35 PM   in response to: Ahmed Sayed in response to: Ahmed Sayed
On 2016-03-14 17:10:28 +0000, Ahmed Sayed said:

Hello everyone,

I spent the whole day trying to make it work but with no luck at all.
As the subject says i am trying to create a firemonkey form inside a
dll and call it from a vcl app.

Everything works fine until i call FreeLibrary function i get an access
violation.

So i was kinda hopping for someone to help me with this issue because i
think its a bug.

here is the code:

My guess is the FMX Application object is alive after the form is
destroyed, ie is still running, and then when it tries to do something
there's a crash because the DLL it sits in has been unloaded.

Check this by using debug DCUs, and putting breakpoints in the
FMX.Forms.pas TApplication methods, and check its state / that it is
destroyed. Not unloading the DLL, and seeing if any likely-looking
methods are executed (including the message loop) would be a good way
to check too.

In TFireMonkeyContainer, there is some REALLY HACKY code to fake
executing the FMX TApplication object, which the FMX forms rely on, and
to control its lifetime. You might find some inspiration from that,
although please treat the component as a hack, which it is. (It allows
you to embed FireMonkey forms inside VCL forms, with some caveats.)
It's https://parnassus.co/open-source/tfiremonkeycontainer/

Cheers,

David

--
Parnassus: Tools for Delphi programmers. Making the IDE better.
https://parnassus.co/delphi-tools/

Parnassus Bookmarks: replace the IDE's bookmarks with something better.
Never overwrite a bookmark, one shortcut key, dockable list, free.
Parnassus Navigator: jump anywhere in your code - the uses clause, a
property declaration, a method. Ctrl+G, type, all useful items in your
code are filtered as you type. Plus, adds a minimap!

Ahmed Sayed

Posts: 173
Registered: 8/9/07
Re: Show FMX Form inside a dll from VCL app  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 14, 2016 3:50 PM   in response to: David Millington in response to: David Millington
Thanks David,

But i am not good with delphi code where exactly should i put the break points.

Also i noticed something when i run the vcl host app the form is shown on the taskbar but when i click on the button that create he fmx form i get another button on the taskbar says "fmxdll", and after closing the main vcl form that other button remains there. Does that mean the Form is not freed properly. Because i tried the same dll but by creating a TPanel instead of TForm1 and it worked fine without freezing or exceptions.

--
The limits of my language mean the limits of my world
David Millington

Posts: 257
Registered: 5/29/05
Re: Show FMX Form inside a dll from VCL app  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 15, 2016 1:42 PM   in response to: Ahmed Sayed in response to: Ahmed Sayed
On 2016-03-14 22:50:09 +0000, Ahmed Sayed said:

Thanks David,

But i am not good with delphi code where exactly should i put the break points.

Also i noticed something when i run the vcl host app the form is shown
on the taskbar but when i click on the button that create he fmx form i
get another button on the taskbar says "fmxdll", and after closing the
main vcl form that other button remains there. Does that mean the Form
is not freed properly. Because i tried the same dll but by creating a
TPanel instead of TForm1 and it worked fine without freezing or
exceptions.

I'm not sure, sorry. I've never created/shown a FMX form standalone in
a VCL app, only embedded.

If the taskbar button is still there, it's quite possible the form is
not freed, yes. (The taskbar button reflects the window still existing.)

Re units to debug with: look in FMX.Forms.pas. You will find the source
to both TForm and TApplication there. Try putting breakpoints on
likely-looking methods - constructors and destructors, the message loop
in TApplication, TForm.Close/Hide/Show, etc etc.

You will need to turn on "Use debug DCUs" in the linker options.

Cheers,

David

--
Parnassus: Tools for Delphi programmers. Making the IDE better.
https://parnassus.co/delphi-tools/

Parnassus Bookmarks: replace the IDE's bookmarks with something better.
Never overwrite a bookmark, one shortcut key, dockable list, free.
Parnassus Navigator: jump anywhere in your code - the uses clause, a
property declaration, a method. Ctrl+G, type, all useful items in your
code are filtered as you type. Plus, adds a minimap!
Ahmed Sayed

Posts: 173
Registered: 8/9/07
Re: Show FMX Form inside a dll from VCL app  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 15, 2016 12:33 AM   in response to: Ahmed Sayed in response to: Ahmed Sayed
Can anyone help me with this or at least give me a workaround.

--
The limits of my language mean the limits of my world
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02