Michael
Michael

Reputation: 83

Giving a C++ project a web front-end

I have a C++ application that currently has console output.

I want to add a simple web front-end to this application to allow me to view its output remotely.
Eventually, I would like to add some buttons to control the back end application, as well as some charting capabilities to visualize the data.

I've looked into Wt, briefly, but it seems like it's putting too much of the front end in the back end.
I'd like to be able to use the more popular web front ends, like JQuery and MooTools.
Currently I'm using my Windows desktop to prototype, but I'd like the solution to be able to eventually run on Linux, with Apache as the web server.

What is the best way to create the binding between the front and back ends?

Upvotes: 3

Views: 586

Answers (2)

Jarmund
Jarmund

Reputation: 3205

One quick-and-dirty method i sometimes use is to write a perl cgi wrapper that runs the application and captures it's output:

#!/usr/bin/perl
use warnings;
use strict;
use CGI qw(:standard);

print header();
print "<html><head><title>Example that should get you going</title></head><body>\n\n";

my @output = `/usr/bin/whatever`;

# you'd probably want to parse the output in some way at this point

print @output;

print '</body></html>';

For when you get around to controlling it, you can add a form to the website. Basically, if param() has data in it, use that data to parse a set of switches for the C++ application. If no switches are defined, display a form with a set of checkboxes and buttons that, when submitted, will parse into switches and the application will be launched with them.

Warning: If you're parsing form-input into switches, be absolutely 100% sure that it's parsed in such a way that it can't be tainted with, for example, a:

; rm -rf *

...in the parameters returned from the form.

Upvotes: 1

Nathan S.
Nathan S.

Reputation: 5388

This may not be exactly what you're looking for, but depending on the server setup, you can usually rename a C++ application to have the extension ".cgi" on a Apache server and it will run like any other cgi script. In order to make things show up properly, though, you need to add the following lines at the beginning of your program:

printf("Content-type: text/html\n\n");
printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

After that, just print the regular html for the web page, and you have a direct interface. You can also use this to provide a portion of a page using ajax.

Upvotes: 0

Related Questions