i am trying to read a character on screen (to be fed to another function) using readconsoleoutputcharacter from windows.h (i know it is not ideal but i am using it to learn).
i found this code while searching for info:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <iostream>
#include <windows.h>
int main()
{
char buf[BUFSIZ];
COORD coord = { 0,0 };
DWORD num_read;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (ReadConsoleOutputCharacter(hConsole, (LPTSTR)buf, (DWORD)BUFSIZ, coord, (LPDWORD)&num_read) == 0)
{
std::cout << "Error reading console." << '\n';
return 1;
}
std::cout << buf;
}
|
i tried using it and it works, but it returns an error when exiting the program.
the error says:
"run-time check failure #2 - stack around the variable 'buf' was corrupted"
it seems that if i create an infinite loop the error does not happen, and anything the program does after these lines of code still executes fine (with or without the infinite loop). at least thats how it seems to work, although i have had some experiments that seem to conflict that idea.
so in theory it works okay, but the error is obviously trying to point something out to me that i dont understand.
can anybody help me with this problem?
thanks! <3
and if there is a better way to read a character from a position on the screen i am open to learning it.