Reputation: 79
When trying to use the WriteConsoleOutputCharacter function, the application crashes.
COORD pos;
pos.X = 0;
pos.Y = 0;
HANDLE buffer = GetStdHandle(STD_OUTPUT_HANDLE);
LPDWORD written;
char* str = "s";
WriteConsoleOutputCharacter(buffer, str, strlen(str), pos, written);
but the WriteConsole function works correctly:
WriteConsole(buffer1,str,strlen(str),written,NULL);
I'm not getting any error but the Windows "application stopped responding" notification, and i can't use a debugger since the IDE I'm using (Dev C++ 5.11) has a broken one.
Upvotes: 0
Views: 1322
Reputation: 409166
Where is the variable written
pointing? The function will dereference that argument to set the number of characters written. If the variable is not initialized it will have an indeterminate value, and be seemingly pointing at a random location, leading to undefined behavior when being dereferenced.
Instead use a plain DWORD
and use the address-of operator &
:
DWORD written;
WriteConsoleOutputCharacter(buffer, str, strlen(str), pos, &written);
// ^
// Note the address-of operator here
Or if you're not interested in how many characters were written, pass nullptr
instead.
Upvotes: 2