Firstly you should check the return value of ReadFile
. If it returned FALSE
then the read failed and the data might be gibberish.
Also, check the value of read
. It will say how many characters were read. Don't try to work with anything in the buffer beyond the value of read
bytes.
Next, the ReadFile
function receives raw bytes, it does not interpret them. If your system has TCHAR
defined as a 16-bit wchar_t
, then this call will read 8192
bytes from the input stream. It should work correctly.
You didn't say how you determined that the buffer received "gibberish". If you are trying to pretend that the buffer contains actual wchar_t
s but the input stream did not contain wchar_t
s then it will not make sense. The solution to this is: Don't Do That.
Finally, TCHAR
has not been relevant for the last 15 years. Use either char
or wchar_t
according to your needs.