Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1715
  • Last Modified:

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?
  • 3
2 Solutions
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, 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?
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.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now