-2

I've just wanted to get the window Width and the window Height using the returned struct of the function GetConsoleScreenBufferInfo(); from header ConsoleApi2.h. I referenced this question. But then I discovered that this function needs a type _Out_ PCONSOLE_SCREEN_BUFFER_INFO parameter called lpConsoleScreenBufferInfo. Which values do I need to pass as that parameter?

Here is the function head:

WINBASEAPI
BOOL
WINAPI
GetConsoleScreenBufferInfo(
    _In_ HANDLE hConsoleOutput,
    _Out_ PCONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo
    );

Or if there is any other way to get the window Width and Height?

THANKS.

jCoder
  • 66
  • 6
  • 1
    As [the documentation](https://learn.microsoft.com/en-us/windows/console/getconsolescreenbufferinfo) says, you need to pass "a pointer to a `CONSOLE_SCREEN_BUFFER_INFO` structure that receives the console screen buffer information" – Igor Tandetnik May 30 '20 at 20:31
  • Hi jCoder, does the answer's code work for you? – Rita Han Jun 02 '20 at 01:45

1 Answers1

1

You need to give the function a pointer to a CONSOLE_SCREEN_BUFFER_INFO variable for it to fill in, eg:

HANDLE hConsole = ...;
CONSOLE_SCREEN_BUFFER_INFO info = {};
if (GetConsoleScreenBufferInfo(hConsole, &info)) {
    // use info as needed...
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks for the answer. I used it for my GDI window but it always returns `0`. – jCoder Jun 04 '20 at 11:08
  • @jCoder if `GetConsoleScreenBufferInfo()` returns FALSE, you can call `GetLastError()` to find out why. However, you can't pass a handle to a GDI window to `GetConsoleScreenBufferInfo()`, you must pass a handle to an active console screen buffer, which you can get from `GetStdHandle()`, `CreateFile()`, or `CreateConsoleScreenBuffer()` (See [Console Handles](https://learn.microsoft.com/en-us/windows/console/console-handles)) – Remy Lebeau Jun 04 '20 at 16:55