reading a character using readconsoleoutputcharacter

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.
Last edited on
Always read the manual page.
https://docs.microsoft.com/en-us/windows/console/readconsoleoutputcharacter

nLength [in]
The number of screen buffer character cells from which to read. The size of the buffer pointed to by the lpCharacter parameter should be nLength * sizeof(TCHAR).

You're assuming char, but chances are, you're compiling with UNICODE enabled (which has been the default for the last decade or so in visual studio).

Result - windows thinks the buffer is much larger than you say.
At least until it checks the stack.

You should be able to do this without all those casts.
All they do is serve to mask errors which might otherwise be reported.

Casts are a last resort when you know what you're doing.
i have read the documentation but it is hard to understand, i am still very new to C++
i am starting to piece things together a little though.

changing the char to TCHAR seems to have done the trick, thanks.
i would love to know more about how i can avoid the casts though. this code is a copy and paste code that i was trying to use to mould into my own code while learning how it works, but i couldnt get it to work until you helped.

i have adjusted the code for a function to the following.
how can this be further improved?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int readCharacter(COORD position)
{
	TCHAR buf[BUFSIZ];	//bufsize can equal 1?
	DWORD num_read;

	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	if (ReadConsoleOutputCharacter(hConsole, (LPTSTR)buf, (DWORD)BUFSIZ, position, (LPDWORD)&num_read) == 0)
	{
		std::cout << "Error reading console." << '\n';
		return -1;
	}
	return *buf;
}


i tested using the value "1" instead of "BUFSIZ" and it worked fine. i only need a single character so is this okay?

this returns the value of the char, which i can use without problems, but i would like to understand this better if anybody can help me with that?

thanks
Last edited on
Registered users can post here. Sign in or register to post.