Recent

Author Topic: objfpc and delphi mode callbacks  (Read 3420 times)

Zaher

  • Hero Member
  • *****
  • Posts: 679
    • parmaja.org
objfpc and delphi mode callbacks
« on: April 02, 2018, 07:28:23 pm »
I got error Wrong number of parameters  when i pass callback function in objfpc, and the function declared other unit in delphi mode

Error: Wrong number of parameters specified for call to "MyCallback2"
Error: Found declaration: MyCallback2(AnsiString);

Unit2
Code: [Select]
unit Unit2;

{$mode delphi}{$H+}

interface

uses
  Classes, SysUtils;

type
  TMyCallback = procedure(S: string);

procedure CallIt(const ACallback: TMyCallback);

implementation

procedure MyCallback1(S: string);
begin
end;

procedure CallIt(const ACallback: TMyCallback);
begin
  ACallback('Hello');
end;

end.



Here where i call it

Code: [Select]
procedure MyCallback2(S: string);
begin
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  CallIt(MyCallback2);
end;

I need to use CallIt and pass mycallback2 without using @ pointer of procedure, i don't to lose checking parameters benefits , or using delphi mode in unit1.

is there any way to solve it?

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: objfpc and delphi mode callbacks
« Reply #1 on: April 02, 2018, 07:54:54 pm »
I need to use CallIt and pass mycallback2 without using @ pointer of procedure, i don't to lose checking parameters benefits , or using delphi mode in unit1.

is there any way to solve it?
Code: [Select]
{$modeswitch CLASSICPROCVARS}
?

More info.

Zaher

  • Hero Member
  • *****
  • Posts: 679
    • parmaja.org
Re: objfpc and delphi mode callbacks
« Reply #2 on: April 02, 2018, 08:16:17 pm »
Yes that help, but still need to understand why there is different?
Maybe I can change the parameters to suite with callback proc

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: objfpc and delphi mode callbacks
« Reply #3 on: April 02, 2018, 08:25:56 pm »
I need to use CallIt and pass mycallback2 without using @ pointer of procedure, i don't to lose checking parameters benefits , or using delphi mode in unit1.
I'm not sure I understand... but what's the problem with using @ in this case in unit1?

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4. //...
  5. procedure TForm1.FormCreate(Sender: TObject);
  6. begin
  7.   CallIt(@MyCallback2);
  8. end;

A.F.A.I.K. you don't "lose checking parameters benefits" when passing it with @, or am I missing something?

Zaher

  • Hero Member
  • *****
  • Posts: 679
    • parmaja.org
Re: objfpc and delphi mode callbacks
« Reply #4 on: April 02, 2018, 08:29:25 pm »
The problem if I changed the parameters of TMyCallback, I need the compiler stops me, to correct my functions
With passing pointer, compiler work, but at runing time, I got AV.

Code: [Select]
type
  TMyCallback = procedure(S: string; ID: Integer);

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: objfpc and delphi mode callbacks
« Reply #5 on: April 02, 2018, 08:31:39 pm »
Ah, ok. How about using {$typedaddress on} in unit2?

Quote
Also, if you are using the @-address-operator usage of {$typedaddress on} is advised in order to prevent programming mistakes.
http://wiki.freepascal.org/How_to_use_procedural_variables

B.T.W. I still got an "Incompatible type for arg no. 1" error even without it.

Code below gives me:
Quote
unit1.pas(39,22) Error: Incompatible type for arg no. 1: Got "<address of procedure(AnsiString);Register>", expected "<procedure variable type of procedure(AnsiString;LongInt);Register>"
Which seems fine.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     procedure FormCreate(Sender: TObject);
  17.   private
  18.  
  19.   public
  20.  
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27. uses Unit2;
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32. procedure MyCallback2(S: string);
  33. begin
  34.   Showmessage(S);
  35. end;
  36.  
  37. procedure TForm1.FormCreate(Sender: TObject);
  38. begin
  39.   CallIt(@MyCallback2);
  40. end;
  41.  
  42. end.

Code: Pascal  [Select][+][-]
  1. unit Unit2;
  2.  
  3. {$mode delphi}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils;
  9.  
  10. type
  11.   TMyCallback = procedure(S: string; ID: Integer);
  12.  
  13. procedure CallIt(const ACallback: TMyCallback);
  14.  
  15. implementation
  16.  
  17. procedure CallIt(const ACallback: TMyCallback);
  18. begin
  19.   ACallback('Hello', 2);
  20. end;
  21.  
  22. end.
« Last Edit: April 02, 2018, 08:37:47 pm by rvk »

Zaher

  • Hero Member
  • *****
  • Posts: 679
    • parmaja.org
Re: objfpc and delphi mode callbacks
« Reply #6 on: April 02, 2018, 08:52:19 pm »
Oh, my fault,

Yes when i used @ the compiler still check the params, in objfpc,

It is my fault, but i am sure I tested it before posting here O.o

 

TinyPortal © 2005-2018