I was trying to set a couple functions to work with the windows console. The function WriteConsoleOutputCharacter()
from the Window API is not printing the full string like it used to, at first i thought it was the way i was doing it, but then i tried to do it out of the function on a new file, and i get the same result:
The code:
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
int main() {
HANDLE console = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleActiveScreenBuffer(console);
wchar_t* data = L"Hello World";
DWORD bytes = 0;
COORD pos = {0,0};
while (1) {
WriteConsoleOutputCharacter(console, data, strlen(data), pos, &bytes);
}
CloseHandle(console);
return 0;
}
The output: H
(the first character of the string).
It does not print when out of a loop.
NOTE: The charset on the preprocessor definition is set to unicode.
The function strlen
should only be used on single-byte character strings, and not on UTF-16 (which Microsoft refers to as "Unicode" or "wide") strings. Since you are using a UTF-16 string, you should be using the function wcslen
instead.
The function call strlen( L"Hello World" );
will return 1
, which is not what you want. It will return 1
because the UTF-16 letter H
is encoded with the byte values 72
followed by 0
, and strlen
will return the position of the first byte with the value 0
that it finds.