由于参数无效,GetConsoleScreenBufferInfoEx失败

时间:2012-02-10 03:29:53

标签: winapi console realbasic realstudio

我试图从控制台应用程序调用{​​{3}}函数。如果重要,应用程序是在64位Windows 7上运行的32位应用程序。该语言是RealBasic。

我相信我已正确定义了所有结构,并且缓冲区输出句柄适用于所调用的每个其他API函数:

  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

结构:

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

我已经过了20次,对我来说没什么错。我以前多次使用COORD和SMALL_RECT结构,所以我认为我没有对它们进行任何翻译错误。然而,CONSOLE_SCREEN_BUFFER_INFOEX结构在我看来是第一次使用它,我感觉错误在于我的翻译中的某个地方。

1 个答案:

答案 0 :(得分:7)

您需要在发送之前设置CONSOLE_SCREEN_BUFFER_INFOEX的cbSize参数。GetConsoleScreenBufferInfoEx将检查它是否正确大小,这就是它返回无效参数的原因。

所以在致电GetConsoleScreenBufferInfoEx之前添加:

info.cbSize = 96

或者更好的是,Real Basic允许您访问structure的大小:

info.cbSize = GetConsoleScreenBufferInfoEx.Size

哪个应该为你处理计算。