Recent

Author Topic: Lazarus Example: forms inside a dll crashes for Win64... Why?  (Read 1527 times)

muperman

  • Newbie
  • Posts: 3
Hello everyone,

I am using the following Lazarus-Version:
Lazarus 2.1.0
r60422M
FPC 3.3.1
x86_64-win64-win32/win64

I want to generate a dll, which is able to open a form to interact with.
I found the following example from Lazarus:
https://wiki.freepascal.org/Form_in_DLL

This is working fine when compiling for a win32 OS, but if I compile it for Win64 the example crashes with an external SigSev-Fault.
Since I am new to dll developement I really have no Idea why this is working for Win32 and not for Win64...

Please help.
« Last Edit: May 27, 2020, 03:22:37 pm by muperman »

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Lazarus Example: forms inside a dll crashes for Win64... Why?
« Reply #1 on: May 27, 2020, 04:01:02 pm »
I assume you are compiling both the host App and the DLL for 64 bit target ?

You can't mixed them.
The only true wisdom is knowing you know nothing

muperman

  • Newbie
  • Posts: 3
Re: Lazarus Example: forms inside a dll crashes for Win64... Why?
« Reply #2 on: May 27, 2020, 05:16:54 pm »
Yes I am.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Lazarus Example: forms inside a dll crashes for Win64... Why?
« Reply #3 on: May 27, 2020, 05:55:11 pm »
The problem is that both the main program and the dll have their own copy of the VCL and RTL state.

So this is not surprising, it is afaik not really a supported solution (anybody can write wiki articles like that)

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: Lazarus Example: forms inside a dll crashes for Win64... Why?
« Reply #4 on: May 28, 2020, 09:09:00 am »
I don't know, if this example will work, but I just think, that example from Wiki page is a little bit overcomplicated. Try to do this:
1) Create library
2) Add form to in via File->Create form
3) Edit form's design and code
4) All you need - is just one function, exported from Dll: function CreateMyForm:TFormClass;begin Result := TMyForm; end;
5) Then just try to do Application.CreateFrom(<Result of CreateMyFrom from Dll>, MyForm).

P.S. I tried to do it. Everything works. CreateMyForm is called. Form is created. But when I call MyForm.Show - nothing happens.

P.P.S. TForm meta classes differ - "Instance is TForm" doesn't work.

P.P.P.S. Attached project seems to work!

If project works on Win32, but doesn't on Win64, it usually means, that it has invalid pointer casting somewhere. And many copies of RTL and VLC in every DLL - is real reason, why we need runtime packages.
« Last Edit: May 28, 2020, 10:34:45 am by Mr.Madguy »
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Lazarus Example: forms inside a dll crashes for Win64... Why?
« Reply #5 on: May 28, 2020, 02:07:01 pm »
I don't know, if this example will work, but I just think, that example from Wiki page is a little bit overcomplicated. Try to do this:
1) Create library
2) Add form to in via File->Create form
3) Edit form's design and code
4) All you need - is just one function, exported from Dll: function CreateMyForm:TFormClass;begin Result := TMyForm; end;
5) Then just try to do Application.CreateFrom(<Result of CreateMyFrom from Dll>, MyForm).
Bad idea, this is only guaranteed to work with the exact same fpc version. If you build this dll with a different fpc version than the one used to build the program, it might not work. Also this does not work with any other environment (delphi, C, C++, etc.).

And if you have to guarantee that the fpc version is identical, the only feasable way would be to build the dll locally, with the same fpc that is going to use it, in which case there is absolutely no advantage to simply putting the form into a unit

muperman

  • Newbie
  • Posts: 3
Re: Lazarus Example: forms inside a dll crashes for Win64... Why?
« Reply #6 on: May 29, 2020, 08:47:10 am »
Quote
Bad idea, this is only guaranteed to work with the exact same fpc version. If you build this dll with a different fpc version than the one used to build the program, it might not work. Also this does not work with any other environment (delphi, C, C++, etc.).

If this a bad way, what would be the correct way doing it?

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Lazarus Example: forms inside a dll crashes for Win64... Why?
« Reply #7 on: May 29, 2020, 01:46:25 pm »
Basically you must do everything internally, you can not assume to access any classes from your dll in the outside program.

This means you need to create the LCL Application instance with Application.Initialize and start the LCLs messaging loop with Application.Run.

That said, this is then blocking, meaning the function that calls Application.Run will not return until the LCL application is exited (e.g. via mainform close)

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: Lazarus Example: forms inside a dll crashes for Win64... Why?
« Reply #8 on: May 29, 2020, 02:27:56 pm »
Question is - is it needed for internal use or external? I mean, if you make plug-in for your own application, then you shouldn't overcomplicate things. Why do we need RAD, if we don't use it's power? And even if you plan plug-in interface for someone else, then you should know, that in many professional projects, like 3DS Max, same compiler version is REQUIRED for plug-ins anyway. If you plan to make universal plug-in API, then it's better to use interfaces.
« Last Edit: May 29, 2020, 02:30:19 pm by Mr.Madguy »
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

 

TinyPortal © 2005-2018