Lazarus

Programming => General => Topic started by: AsleyCruz on February 24, 2020, 03:55:02 pm

Title: Variables between a form and a DLL
Post by: AsleyCruz on February 24, 2020, 03:55:02 pm
Hi team,

Question 1:
How can I change the text of a TEdit in my form with the new text in a DLL?

I was trying this but I got an error on click button:

Code: Pascal  [Select][+][-]
  1. //--- IN THE DLL
  2. Procedure NewTextEdit(MyTEdit: TEdit); stdcall;
  3. begin
  4.   MyTEdit.Text:= 'New text edit';
  5. end;
  6.  
  7. exports
  8.   NewTextEdit;  
  9.  
  10.  
  11. //---IN MY MAIN LAZARUS FORM
  12. procedure TForm1.Button1Click(Sender: TObject);
  13. begin
  14.   NewTextEdit(Edit1);
  15. end;

Question 2:
How can I pass variable from my Laz Form to the DLL? (viceversa, I know if it is the same)

Thanks in advance team.
Title: Re: Variables between a form and a DLL
Post by: BSaidus on February 24, 2020, 04:25:50 pm
for the form
Code: Pascal  [Select][+][-]
  1.   TForm1 = class(TForm)
  2.     Button1: TButton;
  3.     edit1: TEdit;
  4.     procedure Button1Click(Sender: TObject);
  5.   private
  6.  
  7.   public
  8.  
  9.   end;
  10.  
  11. var
  12.   Form1: TForm1;
  13.  
  14. procedure NEwEditText( var edt: TEdit ); stdcall; external 'project1.dll';
  15.  
  16.  
  17. implementation
  18.  
  19. {$R *.lfm}
  20.  
  21. { TForm1 }
  22.  
  23. procedure TForm1.Button1Click(Sender: TObject);
  24. begin
  25.   NEwEditText( edit1 );
  26. end;        
  27.  



for the dll

Code: Pascal  [Select][+][-]
  1. library project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes,
  7.   { you can add units after this }
  8.   interfaces,
  9.   Controls,
  10.   StdCtrls
  11.   ;
  12.  
  13. procedure NEwEditText( var edt: TEdit );
  14. begin
  15.   edt.Text := 'hellllllllloooooooooo !! ';
  16. end;
  17.  
  18. exports
  19.   NEwEditText;
  20.  
  21. begin
  22.  
  23. end.
  24.  
  25.  
  26.  
Title: Re: Variables between a form and a DLL
Post by: Cyrax on February 24, 2020, 04:31:28 pm
It won't work due to string type being an reference counted type. Also accessing and manipulating LCL classes between library and executable is no no due to how on different memory layouts  classes and their instances are being put during execution time.

Dynamic packages would alleviate this problem and support for it is being developed in the FPC trunk.
Title: Re: Variables between a form and a DLL
Post by: AsleyCruz on February 24, 2020, 04:43:20 pm
for the form
Code: Pascal  [Select][+][-]
  1.   TForm1 = class(TForm)
  2.     Button1: TButton;
  3.     edit1: TEdit;
  4.     procedure Button1Click(Sender: TObject);
  5.   private
  6.  
  7.   public
  8.  
  9.   end;
  10.  
  11. var
  12.   Form1: TForm1;
  13.  
  14. procedure NEwEditText( var edt: TEdit ); stdcall; external 'project1.dll';
  15.  
  16.  
  17. implementation
  18.  
  19. {$R *.lfm}
  20.  
  21. { TForm1 }
  22.  
  23. procedure TForm1.Button1Click(Sender: TObject);
  24. begin
  25.   NEwEditText( edit1 );
  26. end;        
  27.  



for the dll

Code: Pascal  [Select][+][-]
  1. library project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes,
  7.   { you can add units after this }
  8.   interfaces,
  9.   Controls,
  10.   StdCtrls
  11.   ;
  12.  
  13. procedure NEwEditText( var edt: TEdit );
  14. begin
  15.   edt.Text := 'hellllllllloooooooooo !! ';
  16. end;
  17.  
  18. exports
  19.   NEwEditText;
  20.  
  21. begin
  22.  
  23. end.
  24.  
  25.  
  26.  

Thanks BSaidus, I don't know if you tried this code but I'm getting the same error.
And the DLL is working well, just this part of passing variables is not working for now.

Best regards,
Title: Re: Variables between a form and a DLL
Post by: BSaidus on February 24, 2020, 04:49:43 pm
try to add var to your procedure in the dll

Code: Pascal  [Select][+][-]
  1.  
  2. procedure NEwEditText( var edt: TEdit );
  3. begin
  4.   edt.Text := 'hellllllllloooooooooo !! ';
  5. end;
  6.  
  7.  
Title: Re: Variables between a form and a DLL
Post by: Thaddy on February 24, 2020, 04:57:25 pm
try to add var to your procedure in the dll
Do not even try to add such code to a shared library (possibly used from other languages too) Cyrax is right.
Either use PChar or (in case you only use fpc for both library and main program, ) wait for dynamic package support.
Cyrax is very right, do not try to get around this.
Title: Re: Variables between a form and a DLL
Post by: BSaidus on February 24, 2020, 05:00:15 pm
try to add var to your procedure in the dll
Do not even try to add such code to a shared library (possibly used from other languages too) Cyrax is right.
Either use PChar or (in case you only use fpc for both library and main program, ) wait for dynamic package support.
Cyrax is very right, do not try to get around this.


For me It works well.
Title: Re: Variables between a form and a DLL
Post by: AsleyCruz on February 24, 2020, 05:04:13 pm
try to add var to your procedure in the dll
Do not even try to add such code to a shared library (possibly used from other languages too) Cyrax is right.
Either use PChar or (in case you only use fpc for both library and main program, ) wait for dynamic package support.
Cyrax is very right, do not try to get around this.


For me It works well.

Yes BSaidus, for you in CodeTyphon but not for us in Lazarus.
Title: Re: Variables between a form and a DLL
Post by: Thaddy on February 24, 2020, 05:27:39 pm
Oh, well... >:D
Title: Re: Variables between a form and a DLL
Post by: marcov on February 24, 2020, 05:35:55 pm



For me It works well.

Probably because you test with a literal
Title: Re: Variables between a form and a DLL
Post by: BSaidus on February 24, 2020, 09:38:58 pm
Note: I use

Lazarus version: 2.1.0  r 59757M
freepascal Version=3.3.1-r40491

build with fpcdeluxe for mORMot needs.

 :)
Title: Re: Variables between a form and a DLL
Post by: AsleyCruz on February 25, 2020, 12:10:16 am
Wow... it works in CT but no in Lazarus. Really?
Anybody else would like to help me, please?

Thanks again.
Title: Re: Variables between a form and a DLL
Post by: ASerge on February 25, 2020, 01:07:45 am
Anybody else would like to help me, please?
The problem is with dynamic strings. They use reference counting and are managed by the memory мanager. Unfortunately, the main program and library use their own copy of the memory мanager. The string allocated in the memory мanager by the library will be released by the main program's memory мanager.
Possible solution:
1. Types without reference counting: ShortString, PChar, PUnicodeChar, or WideString (for Windows only).
2. Use sharemem both in the library and in the program. Its main purpose is to make a single memory Manager (i.e. the one compiled in it).
Title: Re: Variables between a form and a DLL
Post by: AsleyCruz on February 25, 2020, 01:22:59 am
Anybody else would like to help me, please?
The problem is with dynamic strings. They use reference counting and are managed by the memory мanager. Unfortunately, the main program and library use their own copy of the memory мanager. The string allocated in the memory мanager by the library will be released by the main program's memory мanager.
Possible solution:
1. Types without reference counting: ShortString, PChar, PUnicodeChar, or WideString (for Windows only).
2. Use sharemem both in the library and in the program. Its main purpose is to make a single memory Manager (i.e. the one compiled in it).

Thanks bro, you seem to know a lot info. A question...
According to your experience/knowledge, Is CodeTyphon better than Lazarus and do you recommend it?
Title: Re: Variables between a form and a DLL
Post by: Handoko on February 25, 2020, 02:23:15 am
Is CodeTyphon better than Lazarus and do you recommend it?

Each of them has its own advantages/disadvantages.

CodeTyphon makes cross compiling lots easier. Lazarus/FPC can do cross compiling, the documentation is there. But learning it is not easy. I spent many weeks just to be able to cross compile from Linux to Windows.

CodeTyphon has its own IDE. It doesn't mean better, but its docker layout makes beginners easier to use. Actually Lazarus has docker layout and can enable single-windows mode but they are not enabled by default.

CodeTyphon improves/bugfixes some libraries and convert some libraries from other languages to Pascal.

CodeTyphon really done something useful for Pascal community. But at the beginning, they had copyright issues. Not sure has them solved it or not but so far as I know, they still not clearly mention what license of their work and the libraries they bundled in their default download are. I can understand, they just want to focus on working don't want to be disturbed by the licensing issue, so they just say it is free. But licensing does not work like that.

CodeTyphon installation requires huge size of disk space. I know harddisk is cheap but I do not like bloatware. I don't want the things I do not use take place on my harddisk. To properly use CodeTyphon your system need to have at least 4 GB of memory.

Lazarus has LAMW, OPM, bug tracker. And the community has a lot of experience users, they share their experience, codes and tricks. I always learn something new just simply reading the posts on the forum.

That's what I know. But I could be wrong.
Title: Re: Variables between a form and a DLL
Post by: dbannon on February 25, 2020, 04:46:08 am
CodeTyphon makes cross compiling lots easier. Lazarus/FPC can do cross compiling, the documentation is there. But learning it is not easy. I spent many weeks just to be able to cross compile from Linux to Windows.
Really ?  I am sure I posted my notes about how to setup cross compiling from Ubuntu64 to Win32, Win64 and Linux32. It was just collected from other notes in the wiki. Maybe it needs to live in the Short Tutorial Page you asked about ?  https://forum.lazarus.freepascal.org/index.php/topic,48264.msg347466.html#msg347466

Its really only building the necessary bits for FPC, Lazarus is ready out of the box.

....
Lazarus has LAMW, OPM, bug tracker. And the community has a lot of experience users, they share their experience, codes and tricks. I always learn something new just simply reading the posts on the forum.

That little list alone is conclusive !   OPM is fantastic and this community is amazing !

Davo
Title: Re: Variables between a form and a DLL
Post by: Handoko on February 25, 2020, 05:57:35 am
I am sure I posted my notes about how to setup cross compiling from Ubuntu64 to Win32, Win64 and Linux32.

Thank you for the notes. But if I remember correctly you haven't posted those notes when I was learning how to do cross compiling. I found the documentation explaining how to do cross compiling but that information wasn't clear. I searched and read a lot of info from forum and did many experiments finally I managed to do it. The most useful information was from Leledumbo.

The documentation was there. But the explanation was not beginner friendly.
Title: Re: Variables between a form and a DLL
Post by: PascalDragon on February 25, 2020, 09:26:04 am
Wow... it works in CT but no in Lazarus. Really?
Anybody else would like to help me, please?

Code like this can work by accident. It depends on the operating system used and how the operating system loads the involved binaries. This does in no way guarantee that it will work every time, neither in Lazarus nor in CT nor in pure FPC.

If you want to pass data between libraries and a main program you must currently either use manual memory management and types like PChar (just as if you'd interface with a C library or have a Pascal library used in C) or you need to at least use a shared memory manager, then you can use types like String as well. However classes and exceptions are still of limits then. Only dynamic packages will solve that problem completely.
TinyPortal © 2005-2018