SetConsoleScreenBufferInfoEx isn't working well on Window Terminal. #19085

Closed
opened 2026-01-31 06:33:19 +00:00 by claunia · 4 comments
Owner

Originally created by @BDisp on GitHub (Dec 15, 2022).

Windows Terminal version

1.15.3466.0

Windows build number

10.0.22621.963

Other Software

No response

Steps to reproduce

Please try to run this code:

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

int main(void)
{
	HANDLE hStdout;
	CONSOLE_SCREEN_BUFFER_INFOEX csbiInfoEx{ 0 };
	csbiInfoEx.cbSize = sizeof(csbiInfoEx);
	SMALL_RECT winRect, srctScrollRect, srctClipRect;
	CHAR_INFO chiFill;
	COORD coordDest;
	COORD size = COORD{ 80, 25 };
	int i;

	printf("\nPrinting 20 lines for reference. ");
	printf("Notice that line 6 is discarded during scrolling.\n");
	for (i = 0; i <= 20; i++)
		printf("%d\n", i);

	hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

	if (hStdout == INVALID_HANDLE_VALUE)
	{
		printf("GetStdHandle failed with %d\n", GetLastError());
		return 1;
	}

	// Get the screen buffer size. 

	if (!GetConsoleScreenBufferInfoEx(hStdout, &csbiInfoEx))
	{
		printf("GetConsoleScreenBufferInfoEx failed %d\n", GetLastError());
		return 1;
	}

	// Set the screen buffer size. 

	csbiInfoEx.dwSize = size;
	csbiInfoEx.srWindow = SMALL_RECT{ 0,0,size.X,size.Y };
	csbiInfoEx.dwMaximumWindowSize = size;

	if (!SetConsoleScreenBufferInfoEx(hStdout, &csbiInfoEx))
	{
		printf("SetConsoleScreenBufferInfoEx failed %d\n", GetLastError());
		return 1;
	}

	// Set the screen size. 

	winRect = { 0,0,(short)(size.X - 1),(short)(size.Y - 1) };

	if (!SetConsoleWindowInfo(hStdout, true, &winRect))
	{
		printf("SetConsoleWindowInfo failed %d\n", GetLastError());
		return 1;
	}

	// The scrolling rectangle is the bottom 15 rows of the 
	// screen buffer. 

	srctScrollRect.Top = csbiInfoEx.dwSize.Y - 16;
	srctScrollRect.Bottom = csbiInfoEx.dwSize.Y - 1;
	srctScrollRect.Left = 0;
	srctScrollRect.Right = csbiInfoEx.dwSize.X - 1;

	// The destination for the scroll rectangle is one row up. 

	coordDest.X = 0;
	coordDest.Y = csbiInfoEx.dwSize.Y - 17;

	// The clipping rectangle is the same as the scrolling rectangle. 
	// The destination row is left unchanged. 

	srctClipRect = srctScrollRect;

	// Fill the bottom row with green blanks. 

	chiFill.Attributes = BACKGROUND_GREEN | FOREGROUND_RED;
	chiFill.Char.UnicodeChar = ' ';

	// Scroll up one line. 

	if (!ScrollConsoleScreenBuffer(
		hStdout,         // screen buffer handle 
		&srctScrollRect, // scrolling rectangle 
		&srctClipRect,   // clipping rectangle 
		coordDest,       // top left destination cell 
		&chiFill))       // fill character and color
	{
		printf("ScrollConsoleScreenBuffer failed %d\n", GetLastError());
		return 1;
	}

	(void)getchar(); //Explicitly ignore return value.

	return 0;
}

Expected Behavior

With Windows Console Host I get the right size:

imagem

Actual Behavior

With Windows Terminal I don't get the right size screen as expected. See the point missing after the word scrolling.
Look at the green blanks filled at the bottom which occupies all the incorrect console size.
If I'm doing something wrong, please let me know. Thanks.

imagem

Originally created by @BDisp on GitHub (Dec 15, 2022). ### Windows Terminal version 1.15.3466.0 ### Windows build number 10.0.22621.963 ### Other Software _No response_ ### Steps to reproduce Please try to run this code: ```c++ #include <windows.h> #include <stdio.h> int main(void) { HANDLE hStdout; CONSOLE_SCREEN_BUFFER_INFOEX csbiInfoEx{ 0 }; csbiInfoEx.cbSize = sizeof(csbiInfoEx); SMALL_RECT winRect, srctScrollRect, srctClipRect; CHAR_INFO chiFill; COORD coordDest; COORD size = COORD{ 80, 25 }; int i; printf("\nPrinting 20 lines for reference. "); printf("Notice that line 6 is discarded during scrolling.\n"); for (i = 0; i <= 20; i++) printf("%d\n", i); hStdout = GetStdHandle(STD_OUTPUT_HANDLE); if (hStdout == INVALID_HANDLE_VALUE) { printf("GetStdHandle failed with %d\n", GetLastError()); return 1; } // Get the screen buffer size. if (!GetConsoleScreenBufferInfoEx(hStdout, &csbiInfoEx)) { printf("GetConsoleScreenBufferInfoEx failed %d\n", GetLastError()); return 1; } // Set the screen buffer size. csbiInfoEx.dwSize = size; csbiInfoEx.srWindow = SMALL_RECT{ 0,0,size.X,size.Y }; csbiInfoEx.dwMaximumWindowSize = size; if (!SetConsoleScreenBufferInfoEx(hStdout, &csbiInfoEx)) { printf("SetConsoleScreenBufferInfoEx failed %d\n", GetLastError()); return 1; } // Set the screen size. winRect = { 0,0,(short)(size.X - 1),(short)(size.Y - 1) }; if (!SetConsoleWindowInfo(hStdout, true, &winRect)) { printf("SetConsoleWindowInfo failed %d\n", GetLastError()); return 1; } // The scrolling rectangle is the bottom 15 rows of the // screen buffer. srctScrollRect.Top = csbiInfoEx.dwSize.Y - 16; srctScrollRect.Bottom = csbiInfoEx.dwSize.Y - 1; srctScrollRect.Left = 0; srctScrollRect.Right = csbiInfoEx.dwSize.X - 1; // The destination for the scroll rectangle is one row up. coordDest.X = 0; coordDest.Y = csbiInfoEx.dwSize.Y - 17; // The clipping rectangle is the same as the scrolling rectangle. // The destination row is left unchanged. srctClipRect = srctScrollRect; // Fill the bottom row with green blanks. chiFill.Attributes = BACKGROUND_GREEN | FOREGROUND_RED; chiFill.Char.UnicodeChar = ' '; // Scroll up one line. if (!ScrollConsoleScreenBuffer( hStdout, // screen buffer handle &srctScrollRect, // scrolling rectangle &srctClipRect, // clipping rectangle coordDest, // top left destination cell &chiFill)) // fill character and color { printf("ScrollConsoleScreenBuffer failed %d\n", GetLastError()); return 1; } (void)getchar(); //Explicitly ignore return value. return 0; } ``` ### Expected Behavior With `Windows Console Host` I get the right size: ![imagem](https://user-images.githubusercontent.com/13117724/207921245-1becac6c-5b55-4096-99a1-ef4de117fd45.png) ### Actual Behavior With `Windows Terminal` I don't get the right size screen as expected. See the point missing after the word `scrolling.` Look at the green blanks filled at the bottom which occupies all the incorrect console size. If I'm doing something wrong, please let me know. Thanks. ![imagem](https://user-images.githubusercontent.com/13117724/207922403-87e6c2ab-4417-415e-8d6e-4f3ae906e68d.png)
claunia added the Issue-BugResolution-Duplicate labels 2026-01-31 06:33:19 +00:00
Author
Owner

@j4james commented on GitHub (Dec 15, 2022):

I think the only issue here is that we don't support screen size changes in Windows Terminal. If I remember correctly, the SetConsoleScreenBufferInfoEx call will change the size on the conhost side of the conpty connection, but that change isn't passed on to the conpty client. As a result, the two get out of sync and things can get weird. This problem is tracked in issue #5094.

@j4james commented on GitHub (Dec 15, 2022): I think the only issue here is that we don't support screen size changes in Windows Terminal. If I remember correctly, the `SetConsoleScreenBufferInfoEx` call will change the size on the conhost side of the conpty connection, but that change isn't passed on to the conpty client. As a result, the two get out of sync and things can get weird. This problem is tracked in issue #5094.
Author
Owner

@BDisp commented on GitHub (Dec 15, 2022):

Thanks for your response @j4james. Do you prefer I close this issue and use the tracked one?

@BDisp commented on GitHub (Dec 15, 2022): Thanks for your response @j4james. Do you prefer I close this issue and use the tracked one?
Author
Owner

@zadjii-msft commented on GitHub (Dec 15, 2022):

Yea, that's probably easiest, thanks!

/dup #5094

@zadjii-msft commented on GitHub (Dec 15, 2022): Yea, that's probably easiest, thanks! /dup #5094
Author
Owner

@ghost commented on GitHub (Dec 15, 2022):

Hi! We've identified this issue as a duplicate of another one that already exists on this Issue Tracker. This specific instance is being closed in favor of tracking the concern over on the referenced thread. Thanks for your report!

@ghost commented on GitHub (Dec 15, 2022): Hi! We've identified this issue as a duplicate of another one that already exists on this Issue Tracker. This specific instance is being closed in favor of tracking the concern over on the referenced thread. Thanks for your report!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#19085