【问题标题】:ReadConsoleOutputCharacter gives ERROR_NOT_ENOUGH_MEMORY when requesting more than 0xCFE1 characters, is there a way around that?当请求超过 0xCFE1 个字符时,ReadConsoleOutputCharacter 给出 ERROR_NOT_ENOUGH_MEMORY,有没有办法解决这个问题?
【发布时间】:2014-05-31 00:34:54
【问题描述】:

代码:

#include <windows.h>
#include <stdio.h>
int main() {
  system("mode 128");
  int range = 0xCFE2;
  char* buf = new char[range+1];
  DWORD dwChars;
  if (!ReadConsoleOutputCharacter(
    GetStdHandle(STD_OUTPUT_HANDLE),
    buf,  // Buffer where store symbols
    range,     // Read len chars
    {0,0},    // Read from row=8, column=6
    &dwChars // How many symbols stored
  )) {
    printf("GetLastError: %lu\n", GetLastError());
  }
  system("pause");
  return 0;
}

【问题讨论】:

    标签: c++ winapi console


    【解决方案1】:

    控制台屏幕缓冲区不能大于 64K。缓冲区中的每个字符需要 2 个字节,一个用于字符代码,另一个用于颜色属性。因此,尝试使用 ReadConsoleOutputCharacter() 读取超过 32K 的字符毫无意义。

    你没有真正的问题。

    【讨论】:

      【解决方案2】:

      WriteConsole() 的文档说:

      如果指定字符数的总大小超过可用堆,函数将失败并返回 ERROR_NOT_ENOUGH_MEMORY。

      ReadConsoleOutputCharacter() 可能有类似的限制,如果你试图阅读太多,即使它没有记录。尝试使用GetConsoleScreenBufferInfo() 或类似函数来确定有多少行和列,然后不要读取更多。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-09-27
        • 1970-01-01
        • 2022-06-11
        • 2020-02-09
        • 2020-02-25
        • 1970-01-01
        • 2015-04-09
        • 1970-01-01
        相关资源
        最近更新 更多