Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling a C++ Builder function and passing parameters from a Javascript function
#1
Hi people,

I need to call a c++ builder function from a HTML button. 

My HTML is something like this:

Code:
<button id="WHATEVER" onClick="add(2,2);">ADD 2 + 2</button>

I know that i can call some functions using the button events (async or not) but i need to call a function with parameters like that:

Javascript Code:

Code:
function add(a,b)
{
       var result = a + b;
       DisplayNumber(result);
}

C++ Builder Code:

Code:
void __fastcall TForm1::DisplayNumber(int number)
{
       ShowMessage(number);
}

Is this possible?

Thanks to all
Reply
#2
(03-27-2019, 07:29 PM)amine1980 Wrote: I need to call a c++ builder function from a HTML button. 

That requires either:
  • a web browser to be embedded in your C++ app, and that the app either:
    • uses the browser's DOM to hook the HTML button itself and assign a handler to its onClick event.
    • registers a COM object with the browser such that scripts within the HTML can access the object via `window.external` (which is deprecated in modern browsers).
  • you to create an COM object that the Javascript can instantiate when needed (typically only works in IE browsers).

(03-27-2019, 07:29 PM)amine1980 Wrote: I know that i can call some functions using the button events (async or not) but i need to call a function with parameters like that:

Javascript Code:

Code:
function add(a,b)
{
       var result = a + b;
       DisplayNumber(result);
}

C++ Builder Code:

Code:
void __fastcall TForm1::DisplayNumber(int number)
{
       ShowMessage(number);
}

Is this possible?

Not exactly the way you have presented it, but yes, in general it is possible to do something like that, with some extra work on the app's part to setup a bridge between the Javascript and C++ codes. But HOW you do it depends on WHICH BROWSER you are using.

But, since you are posting this in an IntraWeb forum, and IntraWeb is server-side, then no, you cannot directly call a server-side C++ function in client-side Javascript. The Javascript would have to invoke its own code to connect to the server and send a request, such as with AJAX, and then the server-side code will have to respond to that request by executing the desired C++ function.

Reply
#3
Hi Gambit, thank you for the answer. I find a way to make it work, but not exactly as i wanted.

The way is follow:

First i need to create a function (callback) in my C++ code, like that:

void __fastcall TIWForm1::function(TStringList *EventParams)
{
        ShowMessage("Hello Cruel World!");
}



Then i need to register this function as a callback function. In the OnCreate Event of my form, i just add this code:

WebApplication->RegisterCallBack("function",function);

After that, i need to send to client side what Ajax event i will use to trigger my server side C++ function:

AddToInitProc("function myAjaxFunction(){ executeAjaxEvent('&value=1234', null,'function',false, null, false);}") ;

So i create some div dynamically in OnCreate event:

AddToInitProc("var myDiv = document.createElement('div');") ;
AddToInitProc("myDiv.id = 'MyDivID'") ;
AddToInitProc("myDiv.className = 'MyDivClass'") ;
AddToInitProc("document.getElementById('OtherDiv').appendChild(myDiv)) ;
AddToInitProc("myDiv.onclick = myAjaxFunction") ;


When i click on myDiv i can call the "function" in the server side but ONLY IF MY INTRAWEB FORM HAVE THIS CODE ON IT: 

void setAsMainForm() {

  TWebMainForm::SetAsMainForm();
 }
#pragma startup setAsMainForm

The problem is that i need to handle Ajax events in all my forms.

How to solve this?

Thank you!
Reply
#4
Im having trouble understanding why you are doing things this way.

Are you loading HTML and then trying to hook it to an IW backend without loading the HTML from IntraWeb itself? How are you starting the session?

Are you able to reduce it down into a ready to run minimalized project in a zip file?
Reply
#5
Hi Kudzu, i will try to explain:

i have a webform with several dynamically created divs (up 255). I need to know wich div was clicked and then call a function like a database read/write or show some message.

I am using the intraweb inside my desktop vcl application. I just add the IWStandAloneServer component to my main VCL app form.

Do you know an easy way to do this?

Thank you!
Reply
#6
(03-27-2019, 07:29 PM)amine1980 Wrote: I need to call a c++ builder function from a HTML button. 

Are we trying to over-complicate this?  You should not need to connect an HTML button with your C++Builder code.

With Intraweb, you would use an IWButton and simply put your code in the Click event on the server.  Intraweb handles all the plumbing to connect the button and server code.

However, a server function can't directly output to a browser display (can't render a showmessage onto the html form).   If I understand some of your earlier comments, it sounds like you want to run the browser and server on the same machine.  The code you provided would attempt to show a VCL form alongside/over/under the HTML form.  While you can run server and client on the same machine, it's a very bad idea to mix VCL forms with browser forms.  Go one way or the other.  Even if you get it to work, you might find one covering the other (vcl over browser or vice versa).

If you're going to use an HTML client then stick to Intraweb for all of it.  In Intraweb, place an IWButton on the form and call your C++Builder function from the OnClick or OnAsyncClick event in your application.  That's all.

To return a value (your DisplayNumber function), place the value in an existing container (maybe TIWLabel) on the form.  Also look into the TIWModalWindow sample if you want a "message box" type popup.

Sorry if I misunderstood what you are trying to do.

Dan
Reply
#7
(03-28-2019, 01:48 PM)amine1980 Wrote: A: i have a webform with several dynamically created divs (up 255). I need to know wich div was clicked and then call a function like a database read/write or show some message.

B: I am using the intraweb inside my desktop vcl application. I just add the IWStandAloneServer component to my main VCL app form.

A: Better to use a grid or create the components server side.

B: See the demos, there is a custom stand alone that shows this if you have issue regarding this.
Reply
#8
If you cant generate server side, best to use an async call back and then have your divs hook to that.
Reply
#9
First you must decide if you are going to call this using AJAX or not. The approach is different.

If you are not using ajax, you just need to create 2 hidden input fields in your form (or even 1 and parse it at server side) which will receive the parameters.

Then your JS function add() should actually set the value of those hidden inputs and submit the form. IW server will receive all declared hidden input values as parameters of the request. 

On the other hand, AJAX (Async) is much easier. You just need to call one of the JS Ajax functions and pass the parameters as strings.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)