using System; using System.Runtime.InteropServices; namespace OpenTap { class AnsiColorCodeFix { private const int STD_OUTPUT_HANDLE = -11; private const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004; private const uint DISABLE_NEWLINE_AUTO_RETURN = 0x0008; [DllImport("kernel32.dll")] private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode); [DllImport("kernel32.dll")] private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode); [DllImport("kernel32.dll", SetLastError = true)] private static extern IntPtr GetStdHandle(int nStdHandle); [DllImport("kernel32.dll")] public static extern uint GetLastError(); /// /// This should work for win10 version 1511 or later /// public static bool TryEnableForWin10() { var iStdOut = GetStdHandle(STD_OUTPUT_HANDLE); if (!GetConsoleMode(iStdOut, out uint outConsoleMode)) { // this happens when std out is redirected e.g. when running isolated. // We rely on the parent process to have completed this fix return false; } outConsoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; if (!SetConsoleMode(iStdOut, outConsoleMode)) { return false; } return true; } } }