How can I read the standard output of an already-running process?

https://news.ycombinator.com/rss Hits: 5
Summary

A customer wanted to know if they could read the standard output of an already-running process. They didn鈥檛 explain why, but my guess is that their main process launches a helper process (not written by them) to begin some workflow, and eventually that helper process launches a console process, which produces the desired output. But they want their main process to read that output, presumably so they can continue automating the workflow. Unfortunately, there is no way to read the standard output of an already-running process. Standard handles, like the environment variables and current directory, are properties of the process that are not exposed to outsiders by the system.鹿 In particular, programs do not expect these properties to change asynchronously. For example, a program that might check the standard output handle and put up a progress spinner if it is a console but not if it is a pipe or file. Some programs check whether their standard output handle refers to a console (in which case they will use functions like WriteConsoleW to get Unicode support or Set颅Console颅Text颅Attribute to get colors. Furthermore, the C runtime libraries typically check their standard output handle at startup to see whether it has been redirected to a file. This alters the behavior of standard I/O functions because buffering is enabled for non-interactive standard output. If you could change the standard output handle out from under a program鈥檚 nose, it would try using Write颅ConsoleW to write to something that isn鈥檛 a console any more. The calls would fail, and the program would generate no output at all. It鈥檚 actually worse because it鈥檚 not uncommon for programs to call Get颅Std颅Handle and save the value in a variable, then use that variable to write to standard output instead of calling Get颅Std颅Handle every time. So even if you managed to change the standard output handle, you鈥檇 be too late. What you have to do is find a way to influence the standard output of the console process at ...

First seen: 2025-12-11 13:37

Last seen: 2025-12-11 17:38