Subject: RE: Right way in Win32 to redirect child process stdout?
From: [email-masked] (Colin Peters)

2 Oct 1998 15:40:16 -0700
Message-ID: <[email-masked]>
Mime-Version: 1.0
Content-Type: text/plain;
	charset="iso-2022-jp"
Content-Transfer-Encoding: 7bit
To: "Igor boukanov" <[email-masked]>
Cc: "GNU-win32" <[email-masked]>

One way to do it is to set the hStdOutput handle in the STARTUPINFO
structure passed to CreateProcess instead of reassigning your own standard
output and then starting the child. You also have to set the flag
STARTF_USESTDHANDLES and set the hStdInput and hStdError (but you can set
them to your own, or not, as you wish). Also fInheritHandles must be TRUE.
If you do it this way you don't have to reset your own standard handles, and
therefore you can avoid the whole issue of having other threads have their
output accidentally redirected.

Colin.

PS. This is all from the Win32 documentation, although I have also done it
myself it was some time ago, so I hope there are no hidden gotchas.

- Colin Peters - colin at fu.is.saga-u.ac.jp
- http://www.geocities.com/Tokyo/Towers/6162/index.html
- Go not to usenet for counsel, for it will say both
- 'yes' and 'no' and 'try another newsgroup'.

-----Original Message-----
Igor boukanov <[email-masked]> wrote:

[snip]
>I need to create a process with redirected output by the mean of Win32 API.
Unfortunately, a straightforward approach:
>
>...
>HANDLE old = GetStdHandle(STD_OUTPUT_HANDLE);
>SetStdHandle(STD_OUTPUT_HANDLE, redirection_handle);
>CreateProcess(...);
>SetStdHandle(STD_OUTPUT_HANDLE, old);
>...
>
>does not work in multithreading program because there is a chance that some
thread will write to stdout between SetStdHandle and CreateProcess
>spoiling child output to redirection_handle.
[snip]

-
For help on using this list (especially unsubscribing), send a message to
"[email-masked]" with one line of text: "help".