How to capture output from a process that i started with ShellExecute

How can I, in Borland C++ Builder, run any system command from my application and capture the output?
LVL 13
Who is Participating?
Xyptilon2Author Commented:
I will answer myself here for others to read in the future. popen returns an invalid handle when used in a GUI. The workaround i found was by generating 2 temporary file names. and redirecting output like this

cmd /c whatevercommand 1>stdout 2>stderr

Where stdout and stderr are the filenames, i can then read those files from my application and do with it as i place. I use WaitForSingleObject to wait for the command started by ShellExecuteEx to finish. (not ShellExecute) because i need the hProcess handle passed back (by reference) by ShellExecuteEx.
Kent OlsenData Warehouse Architect / DBACommented:
Hi X,

C++ Builder supports the old C function popen().  The function executes a command (much like the system() function does) but also opens streams to/from the process.  You can "read" the output from the process as it is generated.

Open the Builder's help index and query popen.  It has an example that should help.

Good Luck,
Xyptilon2Author Commented:
I had a look, it seems _popen returns an invalid file pointer when used under windows. Do you've any other suggestions?
Xyptilon2Author Commented:
I found this example, but cannot seem to get it to work as a BCB VCL application. Can it work?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.