Recent

Author Topic: how to create different class constructor and variable for it ?  (Read 2034 times)

paule32

  • Hero Member
  • *****
  • Posts: 646
  • One in all. But, not all in one.
Hello,
in my own Class System, I encounter a Problem with Constructors.
I using the Source Code as follows ...
Where is my mistake ?
I marked the Position, but I don't know how to fix it ...
So, a helping Hand is welcome ...

Code: Pascal  [Select][+][-]
  1. type
  2.   TClassForm = class of TForm;
  3.   TApplication = class
  4.   ...
  5.   end;
  6.   TForm = class
  7.   ...
  8.   end;
  9.  
  10. procedure TApplication_CreateForm(p: TApplication; InstanceClass: TFormClass; out Referenz ); stdcall;
  11. begin
  12.   TForm(Referenz) := TForm(InstanceClass).Create;   // <--- crash !!!
  13. end;
  14.  
  15. procedure TApplication.CreateForm(InstanceClass: TFormClass; out Referenz); stdcall;
  16. begin
  17.   TApplication_CreateForm(self, InstanceClass, Referenz);
  18. end;
  19.  
  20. type
  21.   TForm1 = class(TForm1)
  22.   ...
  23.   end;
  24. var
  25.   Form1: TForm1;
  26. begin
  27.   Application := TApplication.Create;
  28.   Application.CreateForm(TForm1, Form1);
  29. end;
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

ALLIGATOR

  • Sr. Member
  • ****
  • Posts: 456
  • I use FPC [main] 💪🐯💪
I may seem rude - please don't take it personally

ALLIGATOR

  • Sr. Member
  • ****
  • Posts: 456
  • I use FPC [main] 💪🐯💪
Re: how to create different class constructor and variable for it ?
« Reply #2 on: July 14, 2025, 06:22:48 am »
Try this:
Code: Diff  [Select][+][-]
  1. - TForm(Referenz) := TForm(InstanceClass).Create;
  2. + TForm(Referenz) := TForm(InstanceClass.Create);
I may seem rude - please don't take it personally

paule32

  • Hero Member
  • *****
  • Posts: 646
  • One in all. But, not all in one.
Re: how to create different class constructor and variable for it ?
« Reply #3 on: July 14, 2025, 10:49:43 am »
Thanks Alligator ...

I used a backup 5 days ago ...
I don't get it all back, but the main sources are traced back and works , now.

Don't know what the Error in my Source was.
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1598
    • Lebeau Software
Re: how to create different class constructor and variable for it ?
« Reply #4 on: July 14, 2025, 06:18:08 pm »
You don't even need to type-cast the InstanceClass at all since its a class of TForm and its Create() will return a TForm pointer:

Code: Pascal  [Select][+][-]
  1. TForm(Referenz) := InstanceClass.Create;
« Last Edit: July 14, 2025, 06:20:25 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

paule32

  • Hero Member
  • *****
  • Posts: 646
  • One in all. But, not all in one.
Re: how to create different class constructor and variable for it ?
« Reply #5 on: July 14, 2025, 09:28:15 pm »
Thanks Remy for your Reply.

Now, I get a Error on the following Code Place:

Code: Pascal  [Select][+][-]
  1. generic procedure TStream_LoadFromFile<T>   (p: specialize TStream<T>; const FileName: string); stdcall; external RTLDLL;

The Output is:

Code: Bash  [Select][+][-]
  1. T:\a\>fpc.exe -dDLLIMPORT -dLANGDEU -dDLLDEBUG -n -B -Twin64 -FE. -Fu. -O3 test.pas
  2. Stream.pas(144,115) Error: Compilation raised exception internally
  3. Fatal: Compilation aborted
  4. An unhandled exception occurred at $00000001000ECD52:
  5. EAccessViolation: Access violation
  6.   $00000001000ECD52
  7.   $00000001000EDE28
  8.   $00000001000EE5AB
  9.   $00000001000AE31F
  10.   $000000010012F0C5
  11.   $000000010012E921
  12.   $000000010012EB4C
  13.   $000000010012E00A
  14.   $000000010010B07B
  15.   $00000001000F0689
  16.   $00000001000F0771
  17.   $000000010016ED78
  18.   $0000000100171B1A
  19.   $000000010017460E
  20.   $0000000100174643
  21.   $0000000100174643
  22.   $0000000100174643
  23.  
  24. Error: C:\FPC\3.2.2\win64\fpc\bin\x86_64-win64\ppcx64.exe returned an error exitcode

MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

bytebites

  • Hero Member
  • *****
  • Posts: 793
Re: how to create different class constructor and variable for it ?
« Reply #6 on: July 14, 2025, 10:02:30 pm »
You have another topic about that issue.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1598
    • Lebeau Software
Re: how to create different class constructor and variable for it ?
« Reply #7 on: July 14, 2025, 11:00:30 pm »
Now, I get a Error on the following Code Place:

Code: Pascal  [Select][+][-]
  1. generic procedure TStream_LoadFromFile<T>   (p: specialize TStream<T>; const FileName: string); stdcall; external RTLDLL;

I'm pretty sure you can't export a Generic function from a DLL to begin with. So what are you trying to accomplish with this?
« Last Edit: July 14, 2025, 11:06:00 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

cdbc

  • Hero Member
  • *****
  • Posts: 2868
    • http://www.cdbc.dk
Re: how to create different class constructor and variable for it ?
« Reply #8 on: July 14, 2025, 11:12:26 pm »
Hi
Quote
So what are you trying to accomplish with this?
From what I understand, he's trying to put/stuff the entire RTL into a dll, in order to minimize application size amo.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

paule32

  • Hero Member
  • *****
  • Posts: 646
  • One in all. But, not all in one.
Re: how to create different class constructor and variable for it ?
« Reply #9 on: July 15, 2025, 10:04:52 am »
The Problem is not, to use Generic's in DLL or EXE ...

Like @Remy wrote: It is NOT possible to "export" Generic's.

The Problem is, that FPC DO NOT realize the Construct and end-up with FPC Kernel Faults like you can see in the Ouput Window that I posted there ...

In this case, I would expect a Error / Warning / Hint
« Last Edit: July 15, 2025, 10:06:27 am by paule32 »
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

Thaddy

  • Hero Member
  • *****
  • Posts: 19419
  • Glad to be alive.
Re: how to create different class constructor and variable for it ?
« Reply #10 on: July 15, 2025, 10:50:08 am »
In your case we expect a crash.
It is very simple if you have implemented it correctly.
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses sysutils;
  3. type
  4.  
  5.   TMyobject = class
  6.   private
  7.     FMyName:shortstring;
  8.   public
  9.     constructor Create;reintroduce;
  10.     constructor create(const aName:shortstring);
  11.     constructor create(const aName:shortstring;postscript:integer);
  12.     property MyName:ShortString read FMyName;
  13.   end;
  14.  constructor TMyObject.Create;
  15.  begin
  16.    inherited create;
  17.    FMyname := 'Name goes here';
  18.  end;
  19.  
  20.  constructor TMyObject.Create(const aName:shortstring);
  21.  begin
  22.    inherited create;
  23.    FMyname := aName;
  24.  end;
  25.  
  26.  constructor TMyObject.create(const aName:shortstring;postscript:integer);
  27.  begin
  28.    inherited create;
  29.    FMyname := aName + inttostr(postscript);
  30.  end;
  31.  
  32. var a,b,c:TMyObject;
  33. begin
  34.   a:= TMyObject.Create;
  35.   b:= TMyObject.Create('A new Name');
  36.   c:= TMyObject.Create('A numbered new name:',100);
  37.   writeln(a.MyName);writeln(b.MyName);writeln(c.MyName);
  38.   c.Free;b.Free;a.Free;
  39.   readln;
  40. end.
« Last Edit: July 15, 2025, 10:55:09 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

 

TinyPortal © 2005-2018