RS4+: v2 console: WriteConsoleA returns lpNumberOfBytesWritten==0 during selection #457

Closed
opened 2026-01-30 21:52:48 +00:00 by claunia · 3 comments
Owner

Originally created by @DHowett-MSFT on GitHub (Nov 21, 2018).

Originally assigned to: @miniksa on GitHub.

Using a modified version of @rprichard's test from #40:

#pragma comment(lib, "user32.lib") 

#include <windows.h>
#include <stdio.h>
#include <string.h>

#include <thread>

// Send "Select All", then spawn a thread to hit ESC a moment later.
static void start_selection() {
    const HWND hwnd = GetConsoleWindow();
    const int SC_CONSOLE_SELECT_ALL = 0xFFF5;
    SendMessage(hwnd, WM_SYSCOMMAND, SC_CONSOLE_SELECT_ALL, 0);
    auto press_escape = std::thread([=]() {
        Sleep(500);
        SendMessage(hwnd, WM_CHAR, 27, 0x00010001);
    });
    press_escape.detach();
}

template <typename T>
static void do_write_test(
        const char *api_name,
        T *api_ptr,
        bool use_selection) {
    if (use_selection)
        start_selection();
    char buf[] = "1234567890567890567890567890\n";
    DWORD actual = 0;
    const BOOL ret = api_ptr(
        GetStdHandle(STD_OUTPUT_HANDLE),
        buf, strlen(buf), &actual, NULL);
    const DWORD last_error = GetLastError();
    printf("%s: %s returned %d: actual=%u LastError=%u (%s)\n",
        ((ret && actual == strlen(buf)) ? "SUCCESS" : "ERROR"),
        api_name, ret, actual, last_error,
        use_selection ? "select" : "no-select");
}

int main() {
    do_write_test("WriteConsoleA", WriteConsoleA, false);
    do_write_test("WriteConsoleA", WriteConsoleA, true);
    do_write_test("WriteFile", WriteFile, true);
    return 0;
}

We get the following output:

1234567890567890567890567890
SUCCESS: WriteConsoleA returned 1: actual=29 LastError=0 (no-select)
1234567890567890567890567890
ERROR: WriteConsoleA returned 1: actual=0 LastError=0 (select)
1234567890567890567890567890
SUCCESS: WriteFile returned 1: actual=29 LastError=0 (select)
Originally created by @DHowett-MSFT on GitHub (Nov 21, 2018). Originally assigned to: @miniksa on GitHub. Using a modified version of @rprichard's test from #40: ```cpp #pragma comment(lib, "user32.lib") #include <windows.h> #include <stdio.h> #include <string.h> #include <thread> // Send "Select All", then spawn a thread to hit ESC a moment later. static void start_selection() { const HWND hwnd = GetConsoleWindow(); const int SC_CONSOLE_SELECT_ALL = 0xFFF5; SendMessage(hwnd, WM_SYSCOMMAND, SC_CONSOLE_SELECT_ALL, 0); auto press_escape = std::thread([=]() { Sleep(500); SendMessage(hwnd, WM_CHAR, 27, 0x00010001); }); press_escape.detach(); } template <typename T> static void do_write_test( const char *api_name, T *api_ptr, bool use_selection) { if (use_selection) start_selection(); char buf[] = "1234567890567890567890567890\n"; DWORD actual = 0; const BOOL ret = api_ptr( GetStdHandle(STD_OUTPUT_HANDLE), buf, strlen(buf), &actual, NULL); const DWORD last_error = GetLastError(); printf("%s: %s returned %d: actual=%u LastError=%u (%s)\n", ((ret && actual == strlen(buf)) ? "SUCCESS" : "ERROR"), api_name, ret, actual, last_error, use_selection ? "select" : "no-select"); } int main() { do_write_test("WriteConsoleA", WriteConsoleA, false); do_write_test("WriteConsoleA", WriteConsoleA, true); do_write_test("WriteFile", WriteFile, true); return 0; } ``` We get the following output: ``` 1234567890567890567890567890 SUCCESS: WriteConsoleA returned 1: actual=29 LastError=0 (no-select) 1234567890567890567890567890 ERROR: WriteConsoleA returned 1: actual=0 LastError=0 (select) 1234567890567890567890567890 SUCCESS: WriteFile returned 1: actual=29 LastError=0 (select) ```
claunia added the Product-ConhostResolution-Fix-Available labels 2026-01-30 21:52:48 +00:00
Author
Owner

@zadjii-msft commented on GitHub (Nov 26, 2018):

MSFT:19723646 internally.

@zadjii-msft commented on GitHub (Nov 26, 2018): MSFT:19723646 internally.
Author
Owner

@miniksa commented on GitHub (Dec 21, 2018):

OK checked this in today.

@miniksa commented on GitHub (Dec 21, 2018): OK checked this in today.
Author
Owner

@DHowett-MSFT commented on GitHub (Jan 16, 2019):

This went out in 18315!

@DHowett-MSFT commented on GitHub (Jan 16, 2019): This went out in 18315!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#457