I'm using OutputDebugStringW in my application. Since WinDBG does not automatically append a newline, I had to add the \n
in my output buffer:
#include <iostream>
#include <windows.h>
int main()
{
wchar_t buff[100];
ZeroMemory(buff, sizeof(buff));
_snwprintf_s(buff, sizeof(buff) / sizeof(wchar_t) - 1, L"[test] %d", GetCurrentThreadId());
buff[wcslen(buff)] = L'\n';
OutputDebugStringW(buff);
char buff2[100];
ZeroMemory(buff2, sizeof(buff2));
snprintf(buff2, sizeof(buff2), "[test2] %d", GetCurrentThreadId());
buff2[strlen(buff2)] = '\n';
OutputDebugStringA(buff2);
}
It works but DbgView prints unexpected things:
If I don't append the newline both of them will work just fine. What is the problem here?
As pointed out by Retired Ninja, _snwprintf_s fill out the buffer with another value if the program is build in DEBUG mode. In this case the wchar_t is filled with 0xFEFE:
If I compile the program in Release mode, the string will be NULL terminated.