Here's a simple example (C++):
Code:
CHAR_INFO buffer[20];
COORD bufferSize = {5, 4};
COORD bufferCoord = {0, 0};
SMALL_RECT readRegion = {0, 0, 100, 100};
AttachConsole(processId);
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
ReadConsoleOutput(hConsole, buffer, bufferSize, bufferCoord, &readRegion);
FreeConsole();
This example assumes the process identifier has been obtained either by enumerating processes or by a call to GetWindowThreadProcessId(). This is the process identifier of the application from which the characters will be read.
A program is not allowed to have two consoles, so if your own program has a console, call FreeConsole() before the snippet above.
lpBuffer does store the text. dwBufferSize specifies the area you want. If you wish to get a 4x5 block as above, then set Y to 4 and X to 5. Yes, dwBufferCoord is the origin of the block (zero-based values). In the example above, I started reading from the top left corner. lpReadRegion is the region you want - the values are clamped to what is specified in dwBufferSize.
To give an analogy, say you want to copy a portion of your screen (the console) to a bitmap in memory (lpBuffer). To do this, you need the bitmap in memory, the size of the bitmap in memory (dwBufferSize) and the dimensions and location of the area of the screen you wish to copy (lpReadRegion). Now suppose you were taking different areas of the screen and copying them to the same bitmap in different locations - in this case the location at which to start copying to the in-memory bitmap is required (dwBufferCoord). Sort of like a BitBlt().
If you don't need all the attribute information, and only need to read one line of characters at a time (not a block), you can use ReadConsoleOutputCharacter(). Example:
Code:
TCHAR buffer[20];
COORD coord = {0, 0};
DWORD nRead;
AttachConsole(processId);
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
ReadConsoleOutputCharacter(hConsole, buffer, 19, coord, &nRead);
FreeConsole();