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

noob question about retreiving output from a createprocess...

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,

JL
0
Supraman
Asked:
Supraman
2 Solutions
 
dpearsonCommented:
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,

Doug
0
 
SalteCommented:
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.

fgets(buf,sizeof(buf),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.

Alf
0
 
tinchosCommented:
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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