Callback with c++

<< Click to Display Table of Contents >>

Navigation:  Forum >

Callback with c++

Forum link

 


 

04-23-2021, 07:54 AM:

 

Hello,

 

is there a simple example how to use callback functions whithin c++.

 

I think it’s more complex to define as in the delphi example.

 

I tried this:

 

void TfrmMain::myCallbackMethod(TStringList *slList)

 

{

 

  // some Code

 

}

 

void __fastcall TfrmMain::headerRightButtonClick(TObject *Sender)

 

{

 

  WebApplication->RegisterCallBack("MyCallback", &myCallbackMethod);

 

}

 

but it doesn’t compile.

 

Any hints?

 


 

04-23-2021, 12:19 PM:

 

Hi, try move "WebApplication->RegisterCallBack("MyCallback", &myCallbackMethod);" to OnCreate of form.

 


 

04-23-2021, 01:43 PM:

 

Thanks, but the problem is on compile time - not run time.

 

I will probably need to create a handler class.

 

This is a delphi Example but i can't figue out how to do it in c++

 

procedure TIWForm1.IWAppFormCreate(Sender: TObject);

 

begin

 

RegisterCallBack('myAjaxFunc',

 

procedure (aParams: TStrings)

 

begin

 

WebApplication.ShowMessage('this is an anonymous method used as a callback');

 

end

 

);

 

end;

 


 

04-23-2021, 03:34 PM:

 

(04-23-2021, 01:43 PM)kpenzkofer Wrote: [ -> ]This is a delphi Example but i can't figue out how to do it in c++

 

The 2nd parameter of RegisterCallback() is a TIWCallBackFunction. All online references I can find (I don't have IntraWeb myself) show TIWCallBackFunction is a "procedure(TStringList) of object", which means your original C++ should have worked fine. However, since the "working" Delphi example is using an anonymous procedure for the callback, that means TIWCallBackFunction must have been changed at some point into a "reference to procedure(TStrings)" instead.

 

See How to Handle Delphi Anonymous Methods in C++ in Delphi's documentation.

 

If you are using one of the CLang-based C++ compilers, try using a lambda for the callback, eg:

 

Code:

 

void __fastcall TfrmMain::headerRightButtonClick(TObject *Sender)

 

{

 

&nbsp;&nbsp;&nbsp;&nbsp;WebApplication->RegisterCallBack(_D("MyCallback"),

 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[](TStrings *slList){ ... }

 

&nbsp;&nbsp;&nbsp;&nbsp;);

 

}

 

 

 

Otherwise, if you are using the classic Borland compiler, try creating a custom functor instead, eg:

 

Code:

 

class TMyCallbackRef : public TCppInterfacedObject<TIWCallBackFunction>

 

{

 

public:

 

&nbsp;&nbsp;&nbsp;&nbsp;INTFOBJECT_IMPL_IUNKNOWN(TInterfacedObject);

 

 

 

&nbsp;&nbsp;&nbsp;&nbsp;void __fastcall Invoke(TStrings *slList) {

 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...

 

&nbsp;&nbsp;&nbsp;&nbsp;}

 

};

 

void __fastcall TfrmMain::headerRightButtonClick(TObject *Sender)

 

{

 

&nbsp;&nbsp;&nbsp;&nbsp;WebApplication->RegisterCallBack(_D("MyCallback"),

 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_di_TIWCallBackFunction(new TMyCallbackRef())

 

&nbsp;&nbsp;&nbsp;&nbsp;);

 

}

 

 

 


 

04-23-2021, 03:52 PM:

 

(04-23-2021, 03:34 PM)rlebeau Wrote: [ -> ] (04-23-2021, 01:43 PM)kpenzkofer Wrote: [ -> ]This is a delphi Example but i can't figue out how to do it in c++

 

That code is using an anonymous procedure, which is a bit more complex to handle in C++ than Delphi. See How to Handle Delphi Anonymous Methods in C++ in Delphi's documentation.

 

Thanks - as I thought it is a bit more complex

 

I will give it a try later.

 

I found the issue on my inital post for the non-anonymous version.

 

Forgot the __fastcall directive in the handler definition and had to remove the Reference (&) from calling the handler.

 

Now it's running very well.

 

Code:

 

void __fastcall TfrmMain::myCallbackMethod(TStringList *slList)

 

{

 

  // some Code

 

}

 

void __fastcall TfrmMain::headerRightButtonClick(TObject *Sender)

 

{

 

  WebApplication->RegisterCallBack("MyCallback", myCallbackMethod);

 

}

 

 

 

(04-23-2021, 03:34 PM)rlebeau Wrote: [ -> ] (04-23-2021, 01:43 PM)kpenzkofer Wrote: [ -> ]This is a delphi Example but i can't figue out how to do it in c++

 

The 2nd parameter of RegisterCallback() is a TIWCallBackFunction.  All online references I can find (I don't have IntraWeb myself) show TIWCallBackFunction is a "procedure(TStringList) of object", which means your original C++ should have worked fine.  However, since the "working" Delphi example is using an anonymous procedure for the callback, that means TIWCallBackFunction must have been changed at some point into a "reference to procedure(TStrings)" instead.

 

See How to Handle Delphi Anonymous Methods in C++ in Delphi's documentation.

 

If you are using one of the CLang-based C++ compilers, try using a lambda for the callback, eg:

 

Code:

 

void __fastcall TfrmMain::headerRightButtonClick(TObject *Sender)

 

{

 

    WebApplication->RegisterCallBack(_D("MyCallback"),

 

        [](TStrings *slList){ ... }

 

    );

 

}

 

 

 

Otherwise, if you are using the classic Borland compiler, try creating a custom functor instead, eg:

 

Code:

 

class TMyCallbackRef : public TCppInterfacedObject<TIWCallBackFunction>

 

{

 

public:

 

    INTFOBJECT_IMPL_IUNKNOWN(TInterfacedObject);

 

 

 

    void __fastcall Invoke(TStrings *slList) {

 

        ...

 

    }

 

};

 

void __fastcall TfrmMain::headerRightButtonClick(TObject *Sender)

 

{

 

    WebApplication->RegisterCallBack(_D("MyCallback"),

 

        _di_TIWCallBackFunction(new TMyCallbackRef())

 

    );

 

}

 

 

 

Thanks for the addtional infos - very enlightening.

 

In firemonkey apps i had similar procedures.