Search code examples
c++windowswchar-t

Can't use newline in OutputDebugStringW, weird behavior in DbgView


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:

  1. For OutputDebugStringA, the newline works fine
  2. For OutputDebugStringW, the newline is not recognized somehow

enter image description here

If I don't append the newline both of them will work just fine. What is the problem here?


Solution

  • 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:

    enter image description here

    If I compile the program in Release mode, the string will be NULL terminated.