2

i'm trying to call a procedure within a class using super object, but it won't work, what am i doing wrong here ?

Code sample:

program test_rpc;

{$IFDEF FPC}
  {$MODE OBJFPC}{$H+}
{$ELSE}
  {$APPTYPE CONSOLE}
{$ENDIF}

uses
  SysUtils, superobject;

type
  TCC = class(TObject)
  published
    procedure controler_method1(const This, Params: ISuperObject; var Result: ISuperObject);
  end;

procedure TCC.controler_method1(const This, Params: ISuperObject; var Result: ISuperObject);
var
  i: Integer;
begin
  write('action called with params ');
  writeln(Params.AsString);
end;

var
  s: ISuperObject;
  CC: TCC;
begin
  CC := TCC.Create;
  s := TSuperObject.Create;
  s.M['controler.action1'] := CC.MethodAddress('controler_method1');
  try
    s['controler.action1("HHAHAH")'];
  finally
    s := nil;
    writeln('Press enter ...');
    readln;
  end;
end.

that will crash, what am i doing wrong here ?

it actually gets to "action called with Params" but fails to show the param...

Jan Doggen
  • 8,799
  • 13
  • 70
  • 144

1 Answers1

2

The super method has signature as follows:

TSuperMethod = procedure(const This, Params: ISuperObject;
    var Result: ISuperObject);

This means that you cannot use an instance method since an instance method has an incompatible signature. Your method must look like this:

procedure sm(const This, Params: ISuperObject; var Result: ISuperObject);
begin
  ....
end;

The reason you get a runtime error rather than a compile time error is that you abandoned the type system by using the @ operator. Remove the @ and your program will fail at compile time with an error message that is a terser version of what I said above.

It's one of the great fallacies of Delphi programming that one must use the @ operator to obtain a function pointer. It's a bad habit that you would do well to unlearn.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Funny thing, I found this and got it to work, 5min before your response lol.. http://stackoverflow.com/questions/10460171/get-pointer-of-member-function-delphi/10460491#10460491 –  Mar 03 '13 at 16:04
  • look at the updated code (i edited the post), that works, can you explain to me why i needed to add class/static ? –  Mar 03 '13 at 16:12
  • That runs because a static class method has no implicit this pointer. It's a standard single pointer function. Unlike an instance method which is a two pointer function. – David Heffernan Mar 03 '13 at 16:16