views:

557

answers:

5

Hi,

I have two independent console applications developed in C++. I was asked to develop a GUI for both of them. In this GUI I collect some parameters that are needed to launch any of these applications. Anyone can give some orientation on integrating the console applications with this GUI? Could it be possible to launch the applications from the GUI and not seeing the console? Can the things that the applications write in the console be redirected to a log file?

Thanks in advance.

A: 

Take a look at Expect. Different implementations are available and will link to GUI libraries.

Expect is designed to interface to CLIs and interact with them (feeding inoput, reading output). Depending on the implementation chosen (there are Perl/Java/Python along with the original Tcl) you can then use the corresponding GUI toolkit (e.g. Swing to Java).

Brian Agnew
I forgot telling that I'm working on Windows XP.
deb
Shouldn't be a problem. Although that page refers to Unix/X11, different implementations are available for different platforms. e.g. the Java variant will obviously be platform independent
Brian Agnew
I'm going to try calling the apps from the gui I've developed but I'll also see how Expect works just in case the other solutions doesn't work properly. Thanks again!
deb
+1  A: 

Starting from GUI:

1) Try to extract the parts of the console application which are pure logic, and have nothing to do with displaying things on the screen. Throw out the parts that display data inside of the console.

You can use a GUI Toolkit, for example Qt, which is extremely easy to setup and use.


Then

2) If you want the application to write things to a log file, then there are two things you can do:

a) Launch the program with an argument that will redirect STDOUT to a file, for example:

./MyProgram > logfile.txt

b) Simply open a file and write to it from within your application by modifying the printing code.

I hope this helps.

Jovan
Very useful... I haven't thought about the ">" thingy. One of the applications is written tottally by me, it could be easy to extract the logic parts, but the other one is a very big and modifyed app... this could be tricky... Thanks!
deb
Stream redirections are very handy, there are three standard ones:< is in, you can feed console input from a file> is out, of courseand 2> is for errors, whatever is printed to STDERR.
Jovan
+1  A: 

I'd say you have two clear options:

  1. Extend your applications and link them against a GUI, making the function calls from the GUI.
  2. Create a GUI that is an independent application (compiled by its own without any code dependencies from the console apps), that calls (execs) the console apps at runtime, passing the parameters collected from the forms or whatever interaction mechanism it provides.

The second option is generally easier and cleaner, but may go "out of sync" if the console apps arguments syntax changes.

fortran
I'll try the execs and see what happens. Thanks a lot!
deb
+1  A: 

To decouple the GUI from the command line tools you have your GUI call the existing programs supplying the proper parameters.

You can use the CreateProcess() API function redirecting its output to a filehandle. That call receives a STARTUPINFO structure where you can specify if the window for the command is shown or not.

Check the links for examples, a complete create process sample can be found here.

That's the pure Windows API way of doing things.

If you are doing C++/.NET development things are way easier via System.Diagnostics.Process.

If you can choose any language to do it I'd certainly go the .NET way (and even change the language to C#), it's almost trivial (as it manages all the API calls itself, and you have a sane interface to it)

You can also redirect output via the shell, but that's fairly inflexible and error prone, IMO.

Vinko Vrsalovic
Thanks a lot, I think this is the solution I was looking for.
deb
I composed the GUI using WinApi32 but maybe I should go with .NET.
deb
A: 

Take a look at Tcl/Tk. It was designed specifically for retrofitting scripting to command line C programs and has a C API that makes it quite straightforward to do this. As a bonus, you also get a scripting capability for your programs. There are several ways it supports this type of integration:

  • Link the Tcl interpreter into the program. There is a straightforward API to convert from Tcl data structures into argv type char ** constructs. This lets you invoke the program and use the command line parameters pretty much as is.

  • Spawn a pipe to the C/C++ program and interact with it over the pipe.

  • Spawn the program with its command parameters from the Tcl script.

  • Other, more elaborate RPC constructs (but you probably don't need these).

Tcl is very portable and will run on many platforms including Windows. Tcl also comes with an easy to program GUI toolkit called Tk. Modern versions support theming engines to give them a native look-and-feel, so you can make the Tk apps look pretty close to native windows ones.

This posting discusses using Tcl/Tk for doing the sort of thing you are asking about in some depth.

ConcernedOfTunbridgeWells