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

Posted on 2009-02-17
Last Modified: 2013-11-17
How can I, in Borland C++ Builder, run any system command from my application and capture the output?
Question by:Xyptilon2
    LVL 45

    Assisted Solution

    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,
    LVL 13

    Author Comment

    I had a look, it seems _popen returns an invalid file pointer when used under windows. Do you've any other suggestions?
    LVL 13

    Author Comment

    I found this example, but cannot seem to get it to work as a BCB VCL application. Can it work?
    LVL 13

    Accepted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
    Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project ( automates most of the tasks discussed in this article. You can even fin…
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
    The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now