I am running an executable file (written in C#) from a C++ program using the system command and passing a string to it. The executable file returns some string. Is it possible to access the string returned by the executable file from the C++ program? Is there an easy way to do it using the systems command? I am using visual studio.
Asked
Active
Viewed 1,550 times
0
-
You can use standard output to capture the return values. the input to the c# can either be in the parameter list or through standard input. – jdweng Oct 17 '16 at 10:38
-
I'm passing the input to the exe file as : system((std::string("C:\\file.exe ") + output).c_str()); Where the file.exe is a C# executable. How will I access the output from the c++ program? – Programmer1 Oct 17 '16 at 10:45
-
You are using a file to pass data not as I suggested using a stream (standard input and standard output). In c++ use stdout and stdin as file pointers to capture the input and output (the c++ standard I/O library) from the c# program. – jdweng Oct 17 '16 at 10:53
-
Possible duplicate of [How to spawn a process and capture its STDOUT in .NET?](http://stackoverflow.com/questions/285760/how-to-spawn-a-process-and-capture-its-stdout-in-net) – phuclv Oct 17 '16 at 10:55
-
[Capturing console output from a .NET application](http://stackoverflow.com/q/186822/995714) – phuclv Oct 17 '16 at 10:55
-
@LưuVĩnhPhúc This question is about C++ code calling a .NET executable, not the other way around. – poke Oct 17 '16 at 10:57
-
Since popen() is not available on Win32 (well, _popen() would be), I'll delete my answer. Maybe this helps too: http://stackoverflow.com/questions/450865/what-is-the-equivalent-to-posix-popen-in-the-win32-api – Rene Oct 17 '16 at 11:44
1 Answers
1
If you mean the string is written to the command line of the c# program and you want to capture it then you could use named pipes to intercept the output generated from the c# program.
see https://msdn.microsoft.com/en-us/library/ms682499.aspx
from your c++ program
Or, please clarify what you mean by "running an executable file" and "using the system command" - are you writing code or running at a command prompt ?

Sample Player
- 36
- 4
-
i assumed you were using WIndows as you are running a c# program. If for Linux environment: http://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-output-of-command-within-c-using-posix – Sample Player Oct 17 '16 at 10:54
-
I have a program written in C++ from which I am calling an exe.file written in C# using this code : system((std::string("C:\\file.exe ") + output).c_str()); from visual studio. Now I would like to read the output returned by the executable from the C++ code. – Programmer1 Oct 17 '16 at 10:55
-
you must be running on Windows so popen is not an option for you - read the msdn link I sent you, it shows you how to do the equivalent in c++ on Windows - popen and CreatePipe are APIs that are OS specific. You will need to manage the creation of the c# process manually so you have a handle to the process that you can pass to the windows API to monitor the standard output. See example here http://stackoverflow.com/questions/28222285/make-c-program-to-pass-input-output-to-windows-command-prommpt-interactively – Sample Player Oct 17 '16 at 11:38