Skip to content

Full UTF-16 Unicode support via OutputDebugStringW? #419

Closed

Description

@nomennescio

I noticed #389 was closed, but with a specific direction: trying to translate to UTF-8 and VERY experimental UTF-8 OS support. There was also talk about making DebugViewPP's codepage UTF-8 by default.

In the issue you write You can see that 日本語 passed through OutputDebugStringW is literally translated into a call OutputDebugStringA("???"); by windows. However I CAN'T see that; I see two calls, and two recordings of those calls in DebugViewPP. I see no evidence of an additional/intermediate call to OutputDebugStringA. But I didn't check any DebugViewPP code paths here.

void EncodingTest()
{
    const wchar_t* utf16Message = L"This message is UTF-16 encoded and send through OutputDebugStringW \u65E5\u672C\u8A9E"; // 日本語 UTF-16 encoded
    OutputDebugStringW(utf16Message);

    // 日本語 😀" encoded as UTF-8 bytes
    const char* utf8Message = "This message is UTF-8 encoded and send through OutputDebugStringA (with special windows settings): "
        "\xE6\x97\xA5" // 日 // Ni
        "\xE6\x9C\xAC" // 本 // Hon
        "\xE8\xAA\x9E" // 語 // Go
        " "
        "\xF0\x9F\x98\x80"; // 😀

    OutputDebugStringA(utf8Message);
}
Image

The API documentation is a bit ambiguous here:
https://learn.microsoft.com/en-us/windows/win32/api/debugapi/nf-debugapi-outputdebugstringw

"To force OutputDebugStringW to return Unicode strings, debuggers are required to call the WaitForDebugEventEx function to opt into the new behavior."

and

"OutputDebugStringW converts the specified string based on the current system locale information and passes it to OutputDebugStringA to be displayed. As a result, some Unicode characters may not be displayed correctly."

The second remark seems to suggest Windows always does a translation via OutputDebugStringA, BUT the first remarks strongly suggests it will properly send UTF-16 chars if the application uses WaitForDebugEventEx instead.

Furthermore, https://learn.microsoft.com/en-us/windows/win32/api/debugapi/nf-debugapi-waitfordebugeventex has:
"Important In the past, the operating system did not output Unicode strings via OutputDebugStringW and instead only output ASCII strings. To force OutputDebugStringW to correctly output Unicode strings, debuggers are required to call WaitForDebugEventEx to opt into the new behavior. On calling WaitForDebugEventEx, the operating system will know that the debugger supports Unicode and is specifically opting into receiving Unicode strings."

and

https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-output_debug_string_info

typedef struct _OUTPUT_DEBUG_STRING_INFO {
  LPSTR lpDebugStringData;
  WORD  fUnicode;
  WORD  nDebugStringLength;
} OUTPUT_DEBUG_STRING_INFO, *LPOUTPUT_DEBUG_STRING_INFO;

"fUnicode

The format of the debugging string. If this member is zero, the debugging string is ANSI; if it is nonzero, the string is Unicode."

Unicode. As we're reading from the target process address space, shouldn't this be the same Unicode string it writes with OutputDebugStringW?

I would really appreciate to be able to send full UTF-16 Unicode strings regardless of OS code page settings, using OutputDebugStringW. It would help tremendously with debugging UTF-16 Unicode enabled applications. Could you please investigate if this is still possible in principle?

Activity

  1. janwilmans commented on Oct 17, 2026

    @janwilmans
    Collaborator

    please re-test this on your machine, it should be fixed in v1.21 and up.

  2. nomennescio commented on Nov 17, 2026

    @nomennescio
    Author

    Hi Jan, I tested this with the following code

    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <tchar.h>
    
    int APIENTRY _tWinMain (HINSTANCE instance, HINSTANCE prev, LPTSTR cmdline, int show) {
      OutputDebugStringW (cmdline);
      MessageBox (NULL, cmdline, _T ("CmdLine"), MB_OK); // to compare outputs
      return 0;
    }
    

    Compile into ods.exe, then run from a commandline:

    ods.exe Café ☕
    

    The output does show up DebugView++, however not as UTF16. It does show up correctly in the MessageBox, hence the cmdline parameter itself is correctly handled.

    DebugView++ output:
    image

    Messagebox output:

    image
  3. nomennescio commented on Nov 17, 2026

    @nomennescio
    Author

    This was with v1.31.0.0

    Strangely enough, it showed an exit procedure line that HAS the correct output taking it from the invocation:

    image
  4. nomennescio commented on Nov 17, 2026

    @nomennescio
    Author

    After chasing down the merged code, I was able to make it work; to have a resource file with embedded manifest that sets the codepage of the active process (!) to UTF8 (only supported since late versions of Windows 10). For those who run into this, I've used <file.rc>:

    1 24
    BEGIN
        "<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>\n"
        "<assembly manifestVersion=""1.0"" xmlns=""urn:schemas-microsoft-com:asm.v1"">\n"
        "  <assemblyIdentity type=""win32"" name=""file"" version=""1.0.0.0""/>\n"
        "  <application>\n"
        "    <windowsSettings>\n"
        "      <activeCodePage xmlns=""http://schemas.microsoft.com/SMI/2019/WindowsSettings"">UTF-8</activeCodePage>\n"
        "    </windowsSettings>\n"
        "  </application>\n"
        "</assembly>\n"
    END
    

    Compile and link this into your executable that wants to send using OutputDebugStringW. To be honest, this a completely broken design on the part of Windows.

  5. janwilmans commented on Nov 17, 2026

    @janwilmans
    Collaborator

    for a program you control, sending UTF-8 with OutputDebugStringA is the simplest route. It needs no manifest and doesn’t depend on any code page setting, in the sender or in DebugViewPP. You only need the manifest trick from @nomennescio 's comment if the program has to keep using OutputDebugStringW.

  6. janwilmans commented on Nov 17, 2026

    @janwilmans
    Collaborator

    @nomennescio thanks for that clarification!

  7. nomennescio commented on Nov 18, 2026

    @nomennescio
    Author

    for a program you control, sending UTF-8 with OutputDebugStringA is the simplest route. It needs no manifest and doesn’t depend on any code page setting, in the sender or in DebugViewPP. You only need the manifest trick from @nomennescio 's comment if the program has to keep using OutputDebugStringW.

    Do you know if that is supported in older Windows versions?

  8. WebDancer69 commented on Nov 19, 2026

    @WebDancer69

    There seems to be some confusion around UTF-16 support and WaitForDebugEventEx(). When using OutputDebugStringW() with no debugger attached, but using a listener to the "legacy" shared buffer which e.g. DebugView++ is using, the strings are always converted to ANSI when sent from the client process, using the codepage of the client process. Using WaitForDebugEventEx() to get full UTF-16 support with OutputDebugStringW() only works when the logger attaches as a debugger, which uses a completely different mechanism to transfer the strings.

    One solution that works is to set the system code page to UTF-8, if both the client app and DebugView++ is using the default codepage. The conversion between UTF-8 ("ANSI") and UTF-16 ("Unicode") can handle all characters. But setting UTF-8 globally may not be a good option with some (legacy) programs.

    A preferred solution is to only set UTF-8 explicitly on your client app. But then you depend on DebugView++ also decoding the sent buffer as UTF-8. You also have the issue where <activeCodePage> now can select other codepages in recent Windows versions.

    The full solution could then be:
    DebugView++ is set to always be UTF-8 internally with <activeCodePage>, and (if needed) convert incoming strings to UTF-8 before using them.
    Getting the client / source codepage can be tricky. The default should be to use the system code page. The actual codepage a process is using can be read from the process PEB, but this is undocumented and possibly unstable, in addition access will not be allowed for all processes. Using configurable codepage settings in DebugView++ for given process names is probably the best solution.

    (From what I can find: UTF-8 codepage has been available as a system codepage since Windows 10 1803, and in <activeCodePage> since Windows 10 1903. Support for non-UTF-8 code pages in <activeCodePage> was added in Windows 11 21H2. )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions