Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetConsoleScreenBufferInfoEx fails due To an invalid parameter

I am trying to call the GetConsoleScreenBufferInfoEx function from a console application. If it matters, the application is a 32 bit application running on 64 bit Windows 7. The language is RealBasic.

I believe I have defined all the structures correctly, and the buffer output handle works for every other API function that is being called:

  Declare Function GetConsoleScreenBufferInfoEx Lib "Kernel32" (cHandle As Integer, ByRef info As CONSOLE_SCREEN_BUFFER_INFOEX) As Boolean
  Declare Function GetLastError Lib "Kernel32" () As Integer
  Declare Function GetStdHandle Lib "Kernel32" (hIOStreamType As Integer) As Integer

  Const STD_OUTPUT_HANDLE = -11
  Dim stdHandle As Integer = GetStdHandle(STD_OUTPUT_HANDLE)

  Dim err As Integer
  Dim info As CONSOLE_SCREEN_BUFFER_INFOEX

  If GetConsoleScreenBufferInfoEx(stdHandle, info) Then
    Break
  Else
    err = GetLastError  //Always 87, Invalid parameter
    Break
  End If

Structures:

Structure CONSOLE_SCREEN_BUFFER_INFOEX
  cbSize As Integer
  dwSize As COORD
  CursorPosition As COORD
  Attribute As UInt16
  srWindow As SMALL_RECT
  MaxWindowSize As COORD
  PopupAttributes As UInt16
  FullScreenSupported As Boolean
  ColorTable(15) As UInt32


Structure COORD
  X As UInt16
  Y As UInt16


Structure SMALL_RECT
  Left As UInt16
  Top As UInt16
  Right As UInt16
  Bottom As UInt16

I've gone over this 20 times and nothing looks wrong to me. I've used the COORD and SMALL_RECT structures many times before, so I don't think I made any translation errors on them. The CONSOLE_SCREEN_BUFFER_INFOEX structure, however, is seeing its first use by me here, and I sense that the error lies somewhere in my translation of it.

like image 341
Andrew Lambert Avatar asked Feb 21 '23 13:02

Andrew Lambert


1 Answers

You need to set the cbSize parameter of the CONSOLE_SCREEN_BUFFER_INFOEX before you send it in. GetConsoleScreenBufferInfoEx will check that it is the correct size and that's why it's returning an invalid parameter.

So before the call to GetConsoleScreenBufferInfoEx add:

info.cbSize = 96

Or better yet Real Basic does allow you to access the size of the structure:

info.cbSize = GetConsoleScreenBufferInfoEx.Size

Which should handle the calculation for you.

like image 129
shf301 Avatar answered Feb 25 '23 00:02

shf301