Recent

Author Topic: Variables between a form and a DLL  (Read 815 times)

AsleyCruz

  • Jr. Member
  • **
  • Posts: 78
    • Graphic and apps designer
Variables between a form and a DLL
« 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.
AsleyCruz - Graphic and apps designer
-
http://www.asleycruz.com/

BSaidus

  • Sr. Member
  • ****
  • Posts: 329
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Variables between a form and a DLL
« Reply #1 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.  
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

Cyrax

  • Hero Member
  • *****
  • Posts: 806
Re: Variables between a form and a DLL
« Reply #2 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.
« Last Edit: February 24, 2020, 04:34:16 pm by Cyrax »

AsleyCruz

  • Jr. Member
  • **
  • Posts: 78
    • Graphic and apps designer
Re: Variables between a form and a DLL
« Reply #3 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,
AsleyCruz - Graphic and apps designer
-
http://www.asleycruz.com/

BSaidus

  • Sr. Member
  • ****
  • Posts: 329
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Variables between a form and a DLL
« Reply #4 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.  
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

Thaddy

  • Hero Member
  • *****
  • Posts: 10110
Re: Variables between a form and a DLL
« Reply #5 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.
I am more like donkey than shrek

BSaidus

  • Sr. Member
  • ****
  • Posts: 329
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Variables between a form and a DLL
« Reply #6 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.
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

AsleyCruz

  • Jr. Member
  • **
  • Posts: 78
    • Graphic and apps designer
Re: Variables between a form and a DLL
« Reply #7 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.
AsleyCruz - Graphic and apps designer
-
http://www.asleycruz.com/

Thaddy

  • Hero Member
  • *****
  • Posts: 10110
Re: Variables between a form and a DLL
« Reply #8 on: February 24, 2020, 05:27:39 pm »
Oh, well... >:D
I am more like donkey than shrek

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8385
  • FPC developer.
Re: Variables between a form and a DLL
« Reply #9 on: February 24, 2020, 05:35:55 pm »



For me It works well.

Probably because you test with a literal

BSaidus

  • Sr. Member
  • ****
  • Posts: 329
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Variables between a form and a DLL
« Reply #10 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.

 :)
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

AsleyCruz

  • Jr. Member
  • **
  • Posts: 78
    • Graphic and apps designer
Re: Variables between a form and a DLL
« Reply #11 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.
AsleyCruz - Graphic and apps designer
-
http://www.asleycruz.com/

ASerge

  • Hero Member
  • *****
  • Posts: 1561
Re: Variables between a form and a DLL
« Reply #12 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).

AsleyCruz

  • Jr. Member
  • **
  • Posts: 78
    • Graphic and apps designer
Re: Variables between a form and a DLL
« Reply #13 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?
AsleyCruz - Graphic and apps designer
-
http://www.asleycruz.com/

Handoko

  • Hero Member
  • *****
  • Posts: 3496
  • My goal: build my own game engine using Lazarus
Re: Variables between a form and a DLL
« Reply #14 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.
« Last Edit: February 25, 2020, 02:48:22 am by Handoko »

 

TinyPortal © 2005-2018