Redirecting standard streams in Windows

Adam D Ruppe destructionator at gmail.com
Tue Jul 18 22:09:40 UTC 2023


On Tuesday, 18 July 2023 at 21:31:54 UTC, Alexander Zhirov wrote:
>     HANDLE h_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
>     WriteConsoleW(h_stderr, str.ptr, cast(DWORD)str.length, 
> NULL, NULL);


If you checked the return value of this call, you'd find it fails 
since WriteConsole only works if the output is, in fact, a 
console.

You need to use WriteFile when it is redirected, which you can 
detect with GetFileType to see if it is a character device or not.


More information about the Digitalmars-d-learn mailing list