Code:
for (i=0; i<=tlen;i++)
indexes start at 0 and end 1 less than length of string.

Instead of
Code:
HANDLE hConIn = CreateFileW(L"CONIN$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
to get the handle for stdin, use
Code:
HANDLE hConIn = GetStdHandle(STD_INPUT_HANDLE);