I have inherited code using ReadFile
Windows API method to read single byte from parallel port in a loop.
The code passed CString
instance as the buffer argument, and 1 as the number of bytes to read, something like this:
CString inBuffer = "";
bResult = ReadFile(hCom, inBuffer.GetBuffer(1), 1, &nBytesRead, NULL);
allData += inBuffer.GetBuffer(1);
It worked for long time with this code, but sometimes caused weird problems for example input sent from the machine as "AV01000" was read as "AkVk0k1k0k0k0" - somehow some random character was added after each character read.
Took me long time to figure the source of this behavior, and after changing the code to:
char buffer = '\0';
bResult = ReadFile(hCom, &buffer, 1, &nBytesRead, NULL);
allData += buffer;
It worked flawlessly, reading the exact data sent by the machine.
Is this some kind of buffer overflow in the internal code of CString
? If not, what might explain this weird behavior?