title: c++ buffer backup failure
body: <p>I am working on a console c++ project in which I need to backup a region of the console buffer so I can edit it and revert the changes when done. I came up with this two functions but both have problems since the first outputs an errorcode of <strong>1</strong> <code>[ERROR_INVALID_FUNCTION 1 (0x1) Incorrect function]</code> and the second one outputs <strong>87</strong> <code>[ERROR_INVALID_PARAMETER 87 (0x57) The parameter is incorrect]</code> according to <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms681381" rel="nofollow noreferrer">MSDN error code list</a>. I also checked the documentation for <a href="https://docs.microsoft.com/en-us/windows/console/readconsoleoutput" rel="nofollow noreferrer">ReadConsoleOutput</a> and <a href="https://docs.microsoft.com/en-us/windows/console/writeconsoleoutput" rel="nofollow noreferrer">WriteConsoleOutput</a> APIs but I'm not able to find a solution.</p>
  
  <pre><code>void backupBuffer(PCHAR_INFO buffer, COORD pos, COORD size) {
      SMALL_RECT rect = { pos.X,  pos.Y, pos.X + size.X - 1, pos.Y + size.Y - 1 };
      COORD buffer_index = { 0, 0 };
      BOOL success = 
      ReadConsoleOutput(GetStdHandle(STD_OUTPUT_HANDLE),
                        buffer,
                        buffer_index,
                        pos,
                        &amp;rect);
      if (!success) {
          int error = GetLastError();
                      cout &lt;&lt; error &lt;&lt; endl;
      }
  }
  
  void setBuffer(PCHAR_INFO buffer, COORD pos, COORD size) {
      SMALL_RECT rect = { pos.X,  pos.Y, pos.X + size.X - 1, pos.Y + size.Y - 1 };
      COORD buffer_index = { 0, 0 };
      BOOL success =
      WriteConsoleOutput(GetStdHandle(STD_OUTPUT_HANDLE),
                         buffer,
                         buffer_index,
                         pos,
                         &amp;rect);
      if (!success) {
          int error = GetLastError();
          cout &lt;&lt; error &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>Here's the call:</p>
  
  <pre><code>// width = 10, height = 10 and the console
  // window dimensions are 30 x 120 but the buffer
  // height should be bigger since I'm able to scroll
  // down for a while before reaching the end. 
  PCHAR_INFO buffer = new CHAR_INFO[width * height];
  COORD pos, size;
  pos.X = 20; pos.Y = 2;
  size.X = width; size.Y = height;
  backupBuffer(buffer, pos, size);
  
  // Visual Stuff (cout in that area...)
  
  setBuffer(buffer, pos, size);
  </code></pre>
  
  <p>What should I do to fix this?</p>