The WinApi
WriteFile()
function seems to accept the STD_xxx_HANDLE
constants directly as a first argument. I executed the following:
#include <windows.h>
main() {
DWORD bw;
WriteFile( (void *)STD_OUTPUT_HANDLE, "output", 6, &bw, NULL);
WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), "output", 6, &bw, NULL);
WriteFile( (void *)STD_ERROR_HANDLE, "error", 5, &bw, NULL);
WriteFile( GetStdHandle(STD_ERROR_HANDLE), "error", 6, &bw, NULL);
}
The above example writes "output" twice to stdout and "error" twice to stderr. I have tested on Win XP and Win 7 (I have no access to WINE or Win 10).
I can see GetStdHandle()
does convert the value (from -11 to 7 in the case of STD_OUTPUT_HANDLE
), but either value works the same as a WriteFile()
input HANDLE
.
Is WriteFile implicitly doing GetStdHandle()
when it recognizes a STD_xxx_HANDLE
constant as its HANDLE
input? I have looked but can't find this documented.
Yes I know if it's not documented, don't do it... I'm simply wondering if I missed something.
Surprisingly, this works even on Windows 95 RTM and NT 4 (and XP and 8) so it seems like it is on purpose. I can't tell you why nor how far back in NT 3.x this goes.
But it is undocumented and not really part of the ABI so I would suggest that you continue calling GetStdHandle
to avoid a nasty surprise in the future.