Can't change console application window title with virtual terminal sequences

Viewed 185

I read this article by Microsoft about using of console virtual terminal sequences.

I followed this sequence to change the window title:

ESC ] 2 ; <string> BEL  

I also tried:

ESC ] 0 ; <string> BEL  

But it doesn't change the title.

This is my code:

#include <stdio.h>
#include <wchar.h>
#include <windows.h>

int main()
{
    // https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences

    // Set output mode to handle virtual terminal sequences
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hOut == INVALID_HANDLE_VALUE)
    {
        return GetLastError();
    }

    DWORD dwMode = 0;
    if (!GetConsoleMode(hOut, &dwMode))
    {
        return GetLastError();
    }

    dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
    if (!SetConsoleMode(hOut, dwMode))
    {
        return GetLastError();
    }

    // Try some Set Graphics Rendition (SGR) terminal escape sequences
    wprintf(L"hello");
    wprintf(L"\x1b]2;titel\x07");
    return 0;
}

It doesn't change the console window title.

1 Answers

The Microsoft document suggests:

Note that these sequences are OSC “Operating system command” sequences, and not a CSI like many of the other sequences listed, and as such starts with “\x1b]”, not “\x1b[”. As OSC sequences, they are ended with a String Terminator represented as and transmitted with ESC \ (0x1B 0x5C). BEL (0x7) may be used instead as the terminator, but the longer form is preferred.

It wouldn't be the first time MS didn't implement something. Try Esc \ - it's worked for me.

That said, I have noticed during debugging that the window title doesn't change immediately and since you terminate the program right after the sequence, you might not be seeing the effect.

Related