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.