PL_call_predicate on external/foreign predicate,

Hi, i ve got a simple foreign predicate (just for testing), in this sample i named it add_four. It is registered with SWI Prolog within the RegisterForeign in the following code example.

I also wrote a wrapper class for most of the functionality swi-prolog provides. It will be called correctly and runs successful. But the call to pl_put_integer or pl_unify_integer did not manipulate the value of trm+1.

For internal predicates the Mechanism TProlog.CallPredicate works fine, but for the foreign predicate, i am not able to get a corret result in the provided anonymus method for trm + 1; Its always of the same value
as it was before (99) the result should be 8. The foreign predicate will be called correctly.

function add_four (t : term_t; arity : integer; var context : pointer) : foreign_t;
var i : Integer;
begin
result := c_true;
PL_get_Integer(t,i);
// pl_put_integer(t+1,i+4);
PL_unify_integer(t+1,i+4);
end;

var registered : Boolean = False;

procedure RegisterForeign;
begin
if not registered then
try
installed_add_four := PL_Register_foreign
( ‘add_four’, 2, @add_four, PL_FA_VARARGS);
installed_listSender := PL_Register_foreign
(‘l_sender’, 2, @listSender, PL_FA_VARARGS);
installed_listRetriever := PL_Register_foreign
(‘l_retriever’, 2, @listRetriever, PL_FA_VARARGS);
finally
registered := True;
end;
end;

Function TProlog.CallPredicate(Const aName : AnsiString;
Params : Array Of Const;
cleanFunc : TPredicatedFunc = Nil ) : Boolean;
Var p : rPredicate;
trm : term_t;
Begin
result := True;
If FPredicates.TryGetValue(aName, p) Then
Begin
trm := makeTerm (p, params);
result := CHCK(PL_call_predicate(Nil, PL_Q_NORMAL, p.predicate, trm));
If Assigned(cleanFunc) Then
result := result and cleanFunc(self,p,trm);
End;
End;

And this is how it gets called:

v := 4;
x := 99;
p.CallPredicate(prd, [v,x],

// will be called after callpredicate is succesful
function (p : TProlog; pred : rPredicate; var trm : term_t) : Boolean
var l : String;
begin
 result := True;
 ttype  := pl_term_type(trm+1);
 case ttype of
    PL_INTEGER : if chck(PL_get_integer(trm+1, solution)) then
      begin
        l := IntToStr(solution);
        M.Lines.Add('Ergebnis: ' + l);
      end; // A integer
    PL_RATIONAL ... : ; //  	A rational number
  end;

end );