示例#1
0
        private void SetUpConsole()
        {
            NativeImport.AllocConsole();

            var outFile    = NativeImport.CreateFile("CONOUT$", NativeImport.GENERIC_WRITE | NativeImport.GENERIC_READ, NativeImport.FILE_SHARE_WRITE, 0, NativeImport.OPEN_EXISTING, /*FILE_ATTRIBUTE_NORMAL*/ 0, 0);
            var safeHandle = new SafeFileHandle(outFile, true);

            NativeImport.SetStdHandle(-11, outFile);

            FileStream   fs     = new FileStream(safeHandle, FileAccess.Write);
            StreamWriter writer = new StreamWriter(fs)
            {
                AutoFlush = true
            };

            Console.SetOut(writer);

            if (NativeImport.GetConsoleMode(outFile, out var cMode))
            {
                NativeImport.SetConsoleMode(outFile, cMode | 0x0200);
            }
        }