Disable buffering on redirected stdout Pipe (Win32 API, C++)

前端 未结 3 1902
北荒
北荒 2020-12-01 16:21

I\'m spawning a process from Win32 using CreateProcess, setting the hStdOutput and hStdError properties of STARTUPINFO to

相关标签:
3条回答
  • 2020-12-01 17:00

    It seems to me you can solve the problem if you set the hStdOutput and hStdError of STARTUPINFO not to pipe handles created with CreatePipe, but instead of that you create a named pipes (with CallNamedPipe function exactly like you used if before also using SECURITY_ATTRIBUTES with bInheritHandle = TRUE, see http://msdn.microsoft.com/en-us/library/aa365782.aspx) and then open there by name with respect of CreateFile using FILE_FLAG_WRITE_THROUGH flag. Like you can read on the MSDN (http://msdn.microsoft.com/en-us/library/aa365592.aspx):

    The pipe client can use CreateFile to enable overlapped mode by specifying FILE_FLAG_OVERLAPPED or to enable write-through mode by specifying FILE_FLAG_WRITE_THROUGH.

    So just reopen the pipe with respect of CreateFile using FILE_FLAG_WRITE_THROUGH flag and set the handle/handles to hStdOutput and hStdError of STARTUPINFO.

    0 讨论(0)
  • 2020-12-01 17:04

    The buffering is probably in the C runtime (printf etc) and there is not much you can do about it (IIRC it does a isatty() check to determine a buffering strategy)

    0 讨论(0)
  • 2020-12-01 17:16

    There's SetNamedPipeHandleState, but it only controls buffering for remote pipes, not when both ends are on the same computer.

    0 讨论(0)
提交回复
热议问题