We help IT Professionals succeed at work.

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

Xyptilon2
Xyptilon2 asked
on
Medium Priority
2,149 Views
Last Modified: 2013-11-17
How can I, in Borland C++ Builder, run any system command from my application and capture the output?
Comment
Watch Question

Kent OlsenData Warehouse / Database Architect
CERTIFIED EXPERT
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Top Expert 2008

Author

Commented:
I had a look, it seems _popen returns an invalid file pointer when used under windows. Do you've any other suggestions?
Top Expert 2008

Author

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?
Top Expert 2008
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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.