noob question about retreiving output from a createprocess...

Posted on 2003-03-28
Medium Priority
Last Modified: 2008-02-01
I'm creating a process in my code to run hostname.exe.  I need to retreive the text from the console and put it in a string.  Thanks,

Question by:Supraman
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 28

Accepted Solution

dpearson earned 100 total points
ID: 8228807
It looks like you're on Windows.

If so, the "official" method is to create a file for the output (CreateFile on Win32) and then pass that handle to CreateProcess in the STARTUPINFO structure.  The file can be a memory file if you want (i.e. doesn't exist on disk, but you read from it as if it was -- check out CreateFileForMapping).

If you want standard out, fill in the hStdOutput member (with the handle you created) and set the flags field to include STARTF_USESTDHANDLES.

Personally, I always find CreateProcess to be a royal pain to use (filling out all those structures correctly).

So the other, less "official" solution is just to call
WinExec("hostname.exe > c:\windows\temp\mytempfile.txt") ;
and then read the temp file.  (To get the name of the temp file correctly call "GetTempFileName").  I assume the redirection will work in that context.

Hope one of these helps,

LVL 12

Assisted Solution

Salte earned 100 total points
ID: 8231024
The best way to capture output from another process in a manner that works on many different platforms (windows, unix etc) is to use popen.

FILE * fp = popen("foo bar", "rt");

This will execute the command 'foo.exe' in a process and give it an argument of bar and any output from the process is availble into the FILE * pointer fp.


can be used to read lines from the process.

FILE * fp = popen("foo bar", "wt");

This will also execute the command foo.exe in a process and give it an argument of bar. The process' output will not be captured but whenever the process need to read from stdin it will read whatever you feed it.

fprintf( fp, "hello there\n" );

If you need to have a pipe both to input and another to output you have to reimplement popen so that you get both ends:

step 1. Create a pipe, it has two file descriptors (or handles if you use the Win32 function CreatePipe()).

   pipe() give you two file descriptors and CreatePipe() give you two Win32 HANDLEs.

One of those are for reading and the other is for writing, whatever you write into the write end can be read from the read end. Let's call these two ends for RA and WA.

step 2. Create one more pipe, you now have RB and WB also.

step 3. (only needed if you want a separate stderr pipe). Create one more pipe so you have RC and WC. If you don't do this the RC/WC is the same as RB/WB pipe.

step 4. create a process and give it RA as stdin and WB as stdout and WC as stderr. This is system specific, in Windows you do this by setting a struct when you create the process in Unix you do this by first doing fork and then set stdin/stdout/stderr for the forked process and then do exec on the program you want to run.

step 5. close RA, WB and WC when you know the other process has started.

step 6. use WA to write data to the process that it will read for input, use WB to read regular data from stdout and WC to read error data from stderr. If you need to be able to accept input from both keyboard and the pipes WB and WC use WaitForMultipleObjects() or select().

If you want to use something other than handles under Win32 you can probably translate the HANDLE to a file descriptor by using some obscure function in the C library, dig for it if you want to. If you can't find it it is probably because it isn't there and you have no choice but to accept using HANDLEs for this purpose.

If you want FILE * instead of file descriptors you can use fdopen() which translates a file descriptor to a FILE *.

I don't know of any iostream that can do this type of tasks, wish there was a standard way to do it but it is inherently system specific.


Expert Comment

ID: 9566428
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split points between Salte & dpearson

Please leave any comments here within the next seven days.


EE Cleanup Volunteer

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

777 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