This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: Right way in Win32 to redirect child process stdout?


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 <boukanov@fi.uib.no> 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
"gnu-win32-request@cygnus.com" with one line of text: "help".

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]