【问题标题】:GetConsoleScreenBufferInfoEx fails due To an invalid parameter由于参数无效,GetConsoleScreenBufferInfoEx 失败
【发布时间】:2012-02-10 03:29:53
【问题描述】:

我正在尝试从控制台应用程序调用GetConsoleScreenBufferInfoEx 函数。如果重要的话,该应用程序是在 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 结构在这里首次被我使用,我感觉错误出在我的翻译中。

【问题讨论】:

    标签: winapi console realbasic realstudio


    【解决方案1】:

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

    所以在调用GetConsoleScreenBufferInfoEx之前添加:

    info.cbSize = 96
    

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

    info.cbSize = GetConsoleScreenBufferInfoEx.Size

    哪个应该为您处理计算。

    【讨论】:

    • 是的,但是不要硬编码大小!这就是编译器的用途。
    • 有趣的是,我已经尝试过这个并且我没有对大小进行硬编码。如果我使用96(硬编码),那么函数就会成功。如果我按照我的定义将结构的所有成员加起来,大小为 93。因此,似乎某处的结构定义存在错误。
    • @Amazed - 由于结构对齐没有错误 - msdn.microsoft.com/en-us/library/71kf49f1%28v=vs.80%29.aspx
    • 那么,我是否应该检测我是否在 WoW64 中运行并明确使用96 如果是的话?在 WoW64 下使用 CONSOLE_SCREEN_BUFFER_INFOEX.Size 返回 93。
    • @Amazed,在 WoW64 下大小不应该改变 - 它仍然是 96。如果 CONSOLE.SCREEN_BUFFER_INFOEX.Size 返回 93,那么它的布局不正确。尝试使用 StructureAlignment 属性将对齐方式设置为 8,我相信这是 Windows 使用的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    相关资源
    最近更新 更多