1

Capturing console output from a .NET application (C#)

I'm wanting to do a similar thing that is asked in that question except that I am using c++ and not c#. I have a small well tested java application that has some functions that are about to be rewritten in some of my c++ code and I'm wanting to write some unit tests for this using boost unit to test that the results are the same. Essentially I want to call this java command line application from c++. I'd rather not use the JNI if at all possible so executing the other command line application via the c++ code would be preferred.

How do I invoke a console application from my c++ application and capture all the output that is generated in the console? (I'd very much rather not have to write to an intermediate file but if that's the only way to do it then so be it)

It seems like this should be somewhat straightforward but I'm having a huge amount of trouble finding anything by searching the web as the keywords tend to find results about other things (c# and compiling from command line being 2 examples). I wouldn't be at all surprised if this question is a duplicate but I cannot find another question that is the same. If it does already exist I'm more than happy to close this question upon a link to the other one being found.

1 Answer 1

2

It depends on the operating system you're targeting as there's no process handling functionality in the standard C++ library. On Windows for example you would create two pipes (using the CreatePipe function) to redirect the Java app's input and output, and then run the Java app using the CreateProcess function. There's an example of this here.

But I'd really recommend using JNI instead, which is a light-years better form of interop/IPC than pipes. You'll eventually realize that if you go the pipes route.

Not the answer you're looking for? Browse other questions tagged or ask your own question.