WriteConsoleW fails under WSL #117

Open
opened 2026-01-30 21:42:48 +00:00 by claunia · 0 comments
Owner

Originally created by @bitcrazed on GitHub (Feb 16, 2018).

From @mqudsi on July 14, 2017 18:0

Under Microsoft Windows [Version 10.0.15063], attempting to use the WriteConsoleW low-level API to output to the console results in no text being printed under WSL.

Simple test case:

#include <Windows.h>

void Write(const wchar_t *text, DWORD length = -1)
{
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	if (hOut == INVALID_HANDLE_VALUE || hOut == nullptr)
	{
		ExitProcess((UINT)-1);
	}
	DWORD charsWritten = -1;
	int result = WriteConsoleW(hOut, text, length != -1 ? length : lstrlen(text), &charsWritten, 0);
	if (result == 0)
	{
		ExitProcess((UINT)GetLastError());
	}
	CloseHandle(hOut);
}

int main()
{
	Write(L"Testing\n");
	return 0;
}

The above runs successfully under cmd.exe and prints Testing, but under bash.exe the WriteConsoleW call returns 0 and GetLastError() returns 1 (ERROR_INVALID_FUNCTION).

The only thing I can think of is that WSL is redirecting output to a file which is then redirected to the bash stdout, because the documentation for WriteConsole says:

WriteConsole fails if it is used with a standard handle that is redirected to a file. If an application processes multilingual output that can be redirected, determine whether the output handle is a console handle (one method is to call the GetConsoleMode function and check whether it succeeds). If the handle is a console handle, call WriteConsole. If the handle is not a console handle, the output is redirected and you should call WriteFile to perform the I/O. Be sure to prefix a Unicode plain text file with a byte order mark. For more information, see Using Byte Order Marks.

If that's the case, is there anything WSL could do to make this silently work as the intention behind WriteConsole failing when redirected to a file doesn't translate over too well when dealing with virtualized consoles.

Copied from original issue: Microsoft/WSL#2331

Originally created by @bitcrazed on GitHub (Feb 16, 2018). _From @mqudsi on July 14, 2017 18:0_ Under `Microsoft Windows [Version 10.0.15063]`, attempting to use the `WriteConsoleW` low-level API to output to the console results in no text being printed under WSL. Simple test case: ``` #include <Windows.h> void Write(const wchar_t *text, DWORD length = -1) { HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); if (hOut == INVALID_HANDLE_VALUE || hOut == nullptr) { ExitProcess((UINT)-1); } DWORD charsWritten = -1; int result = WriteConsoleW(hOut, text, length != -1 ? length : lstrlen(text), &charsWritten, 0); if (result == 0) { ExitProcess((UINT)GetLastError()); } CloseHandle(hOut); } int main() { Write(L"Testing\n"); return 0; } ``` The above runs successfully under cmd.exe and prints `Testing`, but under bash.exe the `WriteConsoleW` call returns 0 and `GetLastError()` returns `1` (`ERROR_INVALID_FUNCTION`). The only thing I can think of is that WSL is redirecting output to a file which is then redirected to the bash stdout, because the documentation for `WriteConsole` says: > WriteConsole fails if it is used with a standard handle that is redirected to a file. If an application processes multilingual output that can be redirected, determine whether the output handle is a console handle (one method is to call the GetConsoleMode function and check whether it succeeds). If the handle is a console handle, call WriteConsole. If the handle is not a console handle, the output is redirected and you should call WriteFile to perform the I/O. Be sure to prefix a Unicode plain text file with a byte order mark. For more information, see Using Byte Order Marks. If that's the case, is there anything WSL could do to make this silently work as the intention behind `WriteConsole` failing when redirected to a file doesn't translate over too well when dealing with virtualized consoles. _Copied from original issue: Microsoft/WSL#2331_
claunia added the Resolution-Fix-AvailableProduct-WSL labels 2026-01-30 21:42:48 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#117