delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1998/10/01/13:17:13

From: boukanov AT fi DOT uib DOT no (Igor boukanov)
Subject: Right way in Win32 to redirect child process stdout?
1 Oct 1998 13:17:13 -0700 :
Message-ID: <19981001085947.19345.qmail.cygnus.gnu-win32@findmail.com>
To: gnu-win32 AT cygnus DOT com

Hello!

Probably this message is out of topic but...

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. 
A mutex will not help here because it would mean that each and every use of STD_OUTPUT_HANDLE should be under that mutex and of cause any library with printf should be modified. It is 
just impossible.

I saw several examples with "solutions" like increasing priority of the thread  with CreateProcess to maximum and creating 
child process in a suspense state to minimize the execution time between SetStdHandle and CreateProcess but it just hides the problem. 

But maybe there is a truly robust way to do that?

Regads, Igor Boukanov
boukanov AT fi DOT uib DOT no

-----
Free e-mail group hosting at http://www.eGroups.com/
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request AT cygnus DOT com" with one line of text: "help".

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019