SetConsoleWindowInfo + cmd/powershell yields invisible prompt #375

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

Originally created by @rprichard on GitHub (Sep 17, 2018).

  • Your Windows build number: [Version 10.0.17760.1]

What you're doing and what's happening:

Steps:

  1. Create a new console.
  2. In the new console, reduce the height of the console window using SetConsoleWindowInfo.
  3. Spawn cmd.exe or powershell.exe

The prompt appears in the console buffer, but the console scrolls down so that the cursor and the prompt are both invisible:

invisible-prompt

What's wrong / what should be happening instead:

The window should remain at the top of the console buffer, so that the cursor and prompt are still visible.

This issue explains at least some of the winpty breakage reported at https://github.com/Microsoft/vscode/issues/57803. Any time the console window moves down, winpty assumes that something has been written to the bottommost line of the window. (e.g. In this case, it sees a window full of blank lines.) The console window is blank, so the winpty terminal output is also blank.

The problem doesn't affect the Python 3.7 REPL. I haven't tested other command-line shells/interpreters.

Test case:

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

static int childProcess() {
    // Divide the console window height in half using SetConsoleWindowInfo.
    // Using SetConsoleScreenBufferInfoEx instead seems to avoid this problem,
    // but it has other problems.
    HANDLE conout = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFOEX info = { sizeof(info) };
    GetConsoleScreenBufferInfoEx(conout, &info);
    const int h = info.srWindow.Bottom - info.srWindow.Top + 1;

    // Make the console buffer smaller so the problem is more apparent.
    SetConsoleScreenBufferSize(conout, COORD {
        (SHORT)(info.srWindow.Right - info.srWindow.Left + 1),
        (SHORT)(h + 100),
    });

    // Reduce the window height by half.
    info.srWindow.Bottom = info.srWindow.Top + h / 2 - 1;
    SetConsoleWindowInfo(conout, TRUE, &info.srWindow);

    // At this point, the console window is still at the top of the buffer.

    // Run cmd.exe (or powershell.exe). The shell prompt appears at the
    // expected place in the console buffer, but the window is moved below the
    // prompt and below the cursor, making them invisible.
    _execl("C:\\Windows\\system32\\cmd.exe", "C:\\Windows\\system32\\cmd.exe", NULL);
    return 0;
}

int main(int argc, char *argv[]) {
    if (argc == 2 && !strcmp(argv[1], "child")) {
        return childProcess();
    }

    char cmdline[4096];
    snprintf(cmdline, sizeof(cmdline), "\"%s\" child", argv[0]);
    STARTUPINFOA sui = { sizeof(sui) };
    PROCESS_INFORMATION pi = {};
    CreateProcessA(argv[0], cmdline, nullptr, nullptr, TRUE,
                   CREATE_NEW_CONSOLE, nullptr, nullptr, &sui, &pi);
    return 0;
}
Originally created by @rprichard on GitHub (Sep 17, 2018). * Your Windows build number: [Version 10.0.17760.1] ## What you're doing and what's happening: Steps: 1. Create a new console. 2. In the new console, reduce the height of the console window using `SetConsoleWindowInfo`. 3. Spawn `cmd.exe` or `powershell.exe` The prompt appears in the console buffer, but the console scrolls down so that the cursor and the prompt are both invisible: ![invisible-prompt](https://user-images.githubusercontent.com/1572855/45613504-7a16d480-ba1b-11e8-8cab-839353e92ae5.png) ## What's wrong / what should be happening instead: The window should remain at the top of the console buffer, so that the cursor and prompt are still visible. This issue explains at least some of the winpty breakage reported at https://github.com/Microsoft/vscode/issues/57803. Any time the console window moves down, winpty assumes that something has been written to the bottommost line of the window. (e.g. In this case, it sees a window full of blank lines.) The console window is blank, so the winpty terminal output is also blank. The problem doesn't affect the Python 3.7 REPL. I haven't tested other command-line shells/interpreters. ## Test case: ```c++ #include <windows.h> #include <stdio.h> #include <string.h> #include <process.h> static int childProcess() { // Divide the console window height in half using SetConsoleWindowInfo. // Using SetConsoleScreenBufferInfoEx instead seems to avoid this problem, // but it has other problems. HANDLE conout = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFOEX info = { sizeof(info) }; GetConsoleScreenBufferInfoEx(conout, &info); const int h = info.srWindow.Bottom - info.srWindow.Top + 1; // Make the console buffer smaller so the problem is more apparent. SetConsoleScreenBufferSize(conout, COORD { (SHORT)(info.srWindow.Right - info.srWindow.Left + 1), (SHORT)(h + 100), }); // Reduce the window height by half. info.srWindow.Bottom = info.srWindow.Top + h / 2 - 1; SetConsoleWindowInfo(conout, TRUE, &info.srWindow); // At this point, the console window is still at the top of the buffer. // Run cmd.exe (or powershell.exe). The shell prompt appears at the // expected place in the console buffer, but the window is moved below the // prompt and below the cursor, making them invisible. _execl("C:\\Windows\\system32\\cmd.exe", "C:\\Windows\\system32\\cmd.exe", NULL); return 0; } int main(int argc, char *argv[]) { if (argc == 2 && !strcmp(argv[1], "child")) { return childProcess(); } char cmdline[4096]; snprintf(cmdline, sizeof(cmdline), "\"%s\" child", argv[0]); STARTUPINFOA sui = { sizeof(sui) }; PROCESS_INFORMATION pi = {}; CreateProcessA(argv[0], cmdline, nullptr, nullptr, TRUE, CREATE_NEW_CONSOLE, nullptr, nullptr, &sui, &pi); return 0; } ```
claunia added the Product-ConhostResolution-Duplicate labels 2026-01-30 21:50:18 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#375