Recent

Author Topic: TForms in Lazarus DLLs?  (Read 732 times)

ozznixon

  • Jr. Member
  • **
  • Posts: 88
    • http://www.modernpascal.com/
TForms in Lazarus DLLs?
« on: July 30, 2020, 12:04:07 pm »
I am porting a plugin design I made in D7 that works perfectly. The EXE contains the splash screen and primary TApplication instance. It loads a DLL and passes the instance of TApplication to the DLL - which saves it's TApplication in OldApplication, and overrides Application with the instance from the EXE ... then should show the main form (MDI form).

Again, all of this works fine on D7, and now I am porting this to MacOS w/ Lazarus - w/o success.

if launched from the terminal.app, I see Error:
[FORMS.PP] ExceptionOccurred
  Sender=EAccessViolation
  Exception=Access violation
  Stack trace:
  $0000000000000000

if launched from Lazarus with Dwarf 3 debugging:
Project (name) raised exception class 'Debugger stopped with reason: EXC_BAD_ACCESS (code=1, address=0x0)'.

** Is there a way to trace into the DLL (dylib) to see where the error is. (may have missed a line with I copied from Delphi VM to MacOS Lazarus).

Thanks!
Ozz
---
Want to kick the tires to a Free Pascal like script engine? http://www.ModernPascal.com/

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8616
  • FPC developer.
Re: TForms in Lazarus DLLs?
« Reply #1 on: July 30, 2020, 12:10:50 pm »
For a plain dll this is an extremely complex case, unless you use packages in D7.

If it is just a dll, and it works in D7, that is just happenstance (*), and that is not portable or supportable.
See e.g. https://wiki.freepascal.org/packages

Packages are slowly being worked on, but are not working in Lazarus.

(*) basiclly it is. trial and error depending on exact VCL/LCL implementation till it doesn't crash anymore.  For now, since it is meta stable, never really stable.
« Last Edit: July 30, 2020, 12:17:57 pm by marcov »

ozznixon

  • Jr. Member
  • **
  • Posts: 88
    • http://www.modernpascal.com/
Re: TForms in Lazarus DLLs?
« Reply #2 on: July 30, 2020, 01:30:33 pm »
Thank you - it is DLL not BPL. Been using this plugin design for almost 2 decades - works flawlessly. Now, I am trying to port my code from D7 to Lazarus, mainly for MacOS users.  ::)

PS. I am not trying to cross Delphi DLL with Lazarus... not that crazy ;-)
---
Want to kick the tires to a Free Pascal like script engine? http://www.ModernPascal.com/

af0815

  • Hero Member
  • *****
  • Posts: 560
Re: TForms in Lazarus DLLs?
« Reply #3 on: July 30, 2020, 01:48:44 pm »
https://wiki.freepascal.org/Form_in_DLL the sample is working, but to use this in a real software, no.

Delphi and FPC/Lazarus have internal a differnt design, so things will work internally differnt and the application Object is such a different thing. A lot of the issue are descrbed here in the forum in the past.
« Last Edit: July 30, 2020, 01:51:58 pm by af0815 »
regards
Andreas

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8616
  • FPC developer.
Re: TForms in Lazarus DLLs?
« Reply #4 on: July 30, 2020, 04:21:20 pm »
Thank you - it is DLL not BPL. Been using this plugin design for almost 2 decades - works flawlessly.

But seems to work (even flawlessly) is not necessarily correct. Just like abuse certain low level aspects of string will bring you in problems with D2009+.

The problem with this dll problem compared to strings however, is that the factors are immensely complicated. So: hopeless or at least unsupportable for anything non trivial.

ozznixon

  • Jr. Member
  • **
  • Posts: 88
    • http://www.modernpascal.com/
Re: TForms in Lazarus DLLs?
« Reply #5 on: July 30, 2020, 04:53:27 pm »
Understood by I migrated to C style strings back in TP7 days - so I do not worry as much about AnsiString vs others. And I have ran every profiles, and QA tool against my suite of applications, no leaks, no dead code, etc. I have most of this working under Lazarus already - just not sure how to debug DLL code from Lazarus like we can do with Delphi 7. 8)
---
Want to kick the tires to a Free Pascal like script engine? http://www.ModernPascal.com/

PascalDragon

  • Hero Member
  • *****
  • Posts: 1970
  • Compiler Developer
Re: TForms in Lazarus DLLs?
« Reply #6 on: July 30, 2020, 05:39:19 pm »
You may not have have AnsiStrings, but you're mixing RTTI and VMT data. The TApplication VMT from the executable is a different one from the one in the library. While mixing this might work, it is no way guaranteed and might also lead to subtle and hard to debug problems (for example exceptions not being caught correctly).

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8616
  • FPC developer.
Re: TForms in Lazarus DLLs?
« Reply #7 on: July 31, 2020, 02:16:56 pm »
If lazarus does some IS or AS that VCL doesn't because e.g. the interface factorization, then it could already be the problem.

Anything that acccess the classtype, and specially if it does equations.

Also, it is much easier to make mistakes with FPC to use slightly different units (FPC+  lazarus) for both dll and exe. If those don't match, that is another complication.

Tools don't really help other than basic memory safety, they don't model this.

jamie

  • Hero Member
  • *****
  • Posts: 3277
Re: TForms in Lazarus DLLs?
« Reply #8 on: August 01, 2020, 02:09:26 am »
This is a MacOS he it doing this DLL for, does MAC support DLL's in the same process space for data and CODE global ?


The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 1970
  • Compiler Developer
Re: TForms in Lazarus DLLs?
« Reply #9 on: August 01, 2020, 05:45:17 pm »
Yes, it does, but that is not the point. The thing that both a library and an executable compiled with FPC will have its own "instance" of the RTL, including RTTI and VMT data. If one simply passes an object instance from the executable to the library (or the other way round) then things will break if operations like is are used and the LCL internally does that. So moving forms to a library is a very brittle thing that can blow apart very easily on any of the supported platforms.

jamie

  • Hero Member
  • *****
  • Posts: 3277
Re: TForms in Lazarus DLLs?
« Reply #10 on: August 01, 2020, 09:42:12 pm »
it would be interesting if a DLL could be compiled using the same version of compiler for both the EXE and the DLL so that the RTL (SYSTEM unit) could be accessed directly from the DLL, the one in the EXE instead of having its own copy..

 This would mean the linker would need to know how to link what would be a virtual address space I guess at the time and the DLL would not be able operate from any other app other than one compiled using the same exact compiler for both EXE and DLL.
 Now that would make life wondering for Dynamic Packaging of components etc..  :D
The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 1970
  • Compiler Developer
Re: TForms in Lazarus DLLs?
« Reply #11 on: August 02, 2020, 04:04:22 pm »
That is what dynamic packages are doing, though these have the RTL in a third library that is accessed from both the library and the executable and thus they share the whole internal state and meta data.

For selected platforms that support for compile time packages can already be enabled in FPC trunk (functionality like LoadPackage is not yet supported however), though for now one would have to manually define the packages as the fpmake build system can't generate them yet.

 

TinyPortal © 2005-2018