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
Xyptilon2Asked:
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.
0
 
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,
Kent
0
 
Xyptilon2Author Commented:
I had a look, it seems _popen returns an invalid file pointer when used under windows. Do you've any other suggestions?
0
 
Xyptilon2Author Commented:
I found this example, http://msdn.microsoft.com/en-us/library/ms682499.aspx but cannot seem to get it to work as a BCB VCL application. Can it work?
0
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.