How to use ANSI escape sequences with CSCRIPT on Windows 10?

醉酒当歌 提交于 2019-12-01 05:12:10

MS documentation states

The following terminal sequences are intercepted by the console host when written into the output stream if the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag is set on the screen buffer handle using the SetConsoleMode flag. You can use GetConsoleMode and SetConsoleMode flags to configure this behavior.

So, just to test, I wrote a simple C program to change the console mode and then act as a pipe or launch another process and wait (sorry, just test code).

#define _WIN32_WINNT   0x0500
#include <windows.h>
#include <stdio.h>
#include <tchar.h>

#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004

int _tmain(int argc, TCHAR *argv[]){

    // Console handlers
    DWORD dwOldMode, dwMode ;
    HANDLE hStdout;

    // Pipe read buffer
    int c;

    // Spawn process variables
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    // Retrieve standard output handle
    hStdout = GetStdHandle( STD_OUTPUT_HANDLE );
    if (! GetConsoleMode( hStdout, &dwOldMode ) ) {
        return 1;
    }

    // Change standard output handle
    dwMode = dwOldMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
    if (! SetConsoleMode( hStdout, dwMode ) ){
        CloseHandle( hStdout );
        return 2;
    }

    if( argc < 2 ) {
        // If there is not an argument, read stdin / write stdout 
        while ( EOF != (c = getchar()) ) putchar( c );    
    } else {
        // Argument is present, create a process and wait for it to end
        ZeroMemory( &si, sizeof(si) );
        si.cb = sizeof(si);
        ZeroMemory( &pi, sizeof(pi) );
        if( !CreateProcess(NULL, argv[1], NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi )){
            printf( "CreateProcess failed (%d).\n", GetLastError() );
            return 3;
        }
        WaitForSingleObject( pi.hProcess, INFINITE );
        CloseHandle( pi.hProcess );
        CloseHandle( pi.hThread );    
    }

    // Restore old console mode
    SetConsoleMode( hStdout, dwOldMode );
    CloseHandle( hStdout );

    return 0;
};

Compiled to run.exe with mingw/gcc. The results are

Now, the output from cscript and findstr is processed and the escape sequences are interpreted.

Also, if instead of running the separate programs, I run cmd.exe itself

Since I have not changed the code from findstr.exe, cscript.exe or cmd.exe, only the environment where they are working it seems that

  • neither cscript nor findstr configure/change the console buffer configuration

  • some internal cmd commands change the buffer configuration (I forget to include it in the capture, but copy test.txt con and prompt also work) or, as you point, they use a different output method

  • the only requirement for an application that writes to the standard output stream is that the console output buffer mode is properly configured.

And no, I don't know how to enable it from pure batch.

Updated Answer to part2: How to make it work in CSCRIPT

I finally found a mechanism to enable VT-100 sequences within CSCRIPT at this SuperUser answer. I copied the relevant text from the answer and posted it below.

Fortunately, the global default can be changed from opt-in to opt-out. The registry key at HKEY_CURRENT_USER\Console\VirtualTerminalLevel sets the global default behavior for processing ANSI escape sequences. Create a DWORD key (if necessary) and set its value to 1 to globally enable (or 0 to disable`) ANSI processing by default.

[HKEY_CURRENT_USER\Console]
"VirtualTerminalLevel"=dword:00000001

Note that this registry setting controls a default, meaning that it only affects console apps which don't explicitly manipulate the console mode by calling SetConsoleMode(...). It follows that, while the registry value may help enable ANSI for console-mode-oblivious apps, it will have no effect on any console-mode-savvy app which (for some reason) may explicitly disable ANSI.

Note that the change only affects newly launched console windows - it will not enable VT-100 for already existing console windows.


Within this thread, DosTips user aGerman discovered you can enable the escape sequences by asynchronously launching PowerShell from within your script. PowerShell configures the output to support the escape sequences, and that support remains even after PowerShell exits, for as long as your CSCRIPT process remains active.

For example, here is some JScript code that will enable the sequences

var ps = WScript.CreateObject("WScript.Shell").Exec("powershell.exe -nop -ep Bypass -c \"exit\"");
while (ps.Status == 0) WScript.Sleep(50);


My original answer to part 1: Why doesn't it work in CSCRIPT

OK, I think I have a viable theory as to why it doesn't work. I believe that there must be some low level way/call/function/method (whatever) to pass stdout to the console that only a few internal commands know about. I base this on the fact that FINDSTR also cannot send functioning escape sequences to the console, as shown below:

I've already shown that both TYPE and ECHO work. I've also verified that SET /P works (not shown). So I suspect that cmd.exe was modified to support the new Windows 10 console functionality.

I would love to see some MS documentation describing the required mechanism to send escape sequences to the console.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!