I am trying to play around with WinAPI to be able to manipulate the console, mostly just be able to write whatever I want, wherever I want without having to rewrite the whole console. I remember that I once got it to work earlier but that was long ago and I seem to have lost that code... whoops.
Anyway, I remember, that I succeeded with much less effort than what it is taking me now.
I am using this MS Docs page for reference and I remember using it earlier, successfully.
Right now, there is truly only a couple of lines that I am trying to get to work:
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
HANDLE hndl = GetStdHandle(STD_INPUT_HANDLE);
if (hndl == INVALID_HANDLE_VALUE)
{
cout << "Invalid handle, error " << GetLastError();
return 0;
}
long unsigned int *chars_written = new long unsigned int;
if (!WriteConsoleOutputCharacter(hndl, "mystring", 8, {20, 30}, chars_written))
{
cout << "Could not write, error " << GetLastError();
return 0;
}
return 0;
}
The result is the console window displaying "Could not write, error 6" and then ending the application.
Error 6, according to System Error Codes is "the handle is invalid".
What am I doing wrong? I must be missing something.
It seems to be important that I am trying to work in Code::Blocks.
BONUS: I tried MS Visual Studio with the full WinAPI SDK (some important parts seem to be missing in Code::Blocks) and, while the main problem is the same, the functions in MS Visual Studio don't seem to all fit the official reference I am using, e.g. WriteConsoleOutputCharacter requires an LPCWSTR as its 2nd argument instead of a LPCSTR as mentioned in the source and as works in Code::Blocks. Windows Data Types
Edit: I found that WriteConsoleOutputCharacter is a macro actually, and is defined differently between Code::Blocks and MS Visual Studio, as two different, existing in both versions functions: WriteConsoleOutputCharacterA() and WriteConsoleOutputCharacterW(), which sadly, is not mentioned in the MS Docs.
Thank you in advance, Maurice.