17.3.17 Reading From the Screen Buffer

Using ReadConsoleOutputCharacter, you can read a string of UNICODE or ASCII characters from a screen buffer by specifying the row and column of the character cell at which to begin and the number of characters to read. Using ReadConsoleOutputAttribute , you can read a string of attributes from a screen buffer by specifying the first cell and the number to read. These functions treat the screen buffer as a string of consecutive character cells rather than a two-dimensional array, so if the number to read extends past the end of one row the data is read from the beginning of the next. If the number to read extends past the end of the screen buffer, the characters or attributes are read up to the end and the actual number read is returned. The following code fragment reads a string of 80 characters from row 20 of the screen buffer:

HANDLE hOutput;

CHAR CharString[80];

DWORD dwCharsRead;

BOOL bSuccess;

COORD Coord;

Coord.X = 0;

Coord.Y = 19;

bSuccess = ReadConsoleOutputCharacter(hOutput,

CharString,

80,

Coord,

&dwCharsRead

);

Using ReadConsoleOutput, you can read blocks of character/attribute pairs. This works in much the same way as WriteConsoleOutput . A source rectangle in the screen buffer identifies the region from which to copy. The data read from this rectangle is copied into the destination array beginning at the specified coordinates. The destination buffer is treated as a two dimensional array whose elements contain both an ASCII or UNICODE character and a color attributes field. The size of the destination array is specified in terms of character rows and columns. The actual rectangle that is copied can be clipped if either the source rectangle extends outside the boundaries of the screen buffer, or the destination rectangle extends outside the boundaries of the destination buffer. An example of using ReadConsoleOutput is given in the section above on WriteConsoleOutput.