Console content
I want to get the content of the console window. I got the following code but it doesn't work .. can anyone tell me how to get the console content (characters) ?????
DWORD nLength=2;
HWND hWnd = FindWindow("ConsoleWindowClass",NULL);
LPTSTR lpCharacter=" ";
COORD dwReadCoord;
dwReadCoord.X=11;
dwReadCoord.Y=11;
LPDWORD lpNumberOfCharsRead=0;
bool a= ReadConsoleOutputCharacter(hWnd,lpCharacter,nLength,dwReadCoord,lpNumberOfCharsRead);
+3
source to share
1 answer
Here's a way to do it. GetNumCharsInConsoleBuffer
- get the number of characters in the console buffer. Creates a dynamically allocated array with this value. Finally, it will ReadConsoleBuffer
fill your buffer with the contents of the console buffer.
#include <cstdio>
#include <cstdlib>
#include <Windows.h>
DWORD GetNumCharsInConsoleBuffer()
{
CONSOLE_SCREEN_BUFFER_INFO buffer_info;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &buffer_info);
return (DWORD)((buffer_info.dwSize.X * ( buffer_info.dwCursorPosition.Y + 1)) - (buffer_info.dwSize.X - (buffer_info.dwCursorPosition.X + 1)));
}
DWORD ReadConsoleBuffer(char* buffer, DWORD bufsize)
{
DWORD num_character_read = 0;
COORD first_char_to_read = {0};
ReadConsoleOutputCharacterA(GetStdHandle(STD_OUTPUT_HANDLE), buffer, bufsize, first_char_to_read, &num_character_read);
buffer[bufsize-1] = '\0';
return num_character_read;
}
int main(int argc, char** argv)
{
fprintf(stdout, "Writting\nin\nthe\nbuffer\n");
DWORD bufsize = GetNumCharsInConsoleBuffer();
char* buffer = new char[bufsize];
memset(buffer, 0, bufsize);
ReadConsoleBuffer(buffer, bufsize);
puts("\nBuffer contents:");
puts(buffer);
delete[] buffer;
system("pause");
return 0;
}
Output:
Writting
in
the
buffer
Buffer contents:
Writting
in
the
buffer
Appuyez sur une touche pour continuer...
0
source to share