I have a service which writes in a named pipe, it writes a DWORD, Its is then read by another process(both running in unicode)
When I try to receive the DWORD and convert it to a displayable string (TCHAR,char,wchar_t etc) and print it using printf in command prompt, I get uneven results with newline
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
TCHAR szBuffer[SIZEOF_BUFFER];
DWORD dwRead;
for (;;)
{
if (!ReadFile(hRemoteOutPipe, szBuffer, SIZEOF_BUFFER, &dwRead, NULL) ||
dwRead == 0)
{
DWORD dwErr = GetLastError();
if (dwErr == ERROR_NO_DATA)
break;
}
szBuffer[dwRead / sizeof(TCHAR)] = _T('\0');
// Send it to our stdout
printf("%s",szBuffer);
fflush(stdout);
}
CloseHandle(hRemoteOutPipe);
hRemoteOutPipe = INVALID_HANDLE_VALUE;
::ExitThread(0);
the printf works fine for multibyte, but doesnt work fine for unicode, Kindly help me out
First off:
TCHARwas an idea that made sense in 1995, not so in 2022. You get these weird errors. Secondly, you're tagging as C++, but you're not usingstd::cout. That is really the root cause of the problem:printfdoesn't understandTCHAR, you'd need to use_tprintf.