Example #1
0
        public void Initialize()
        {
            if (initialized)
            {
                return;
            }

            IntPtr hwnd = LoggerUtils.GetConsoleWindow();

            initialized = true;
            // Console app
            if (hwnd != IntPtr.Zero)
            {
                buffer = LoggerUtils.GetStdHandle(LoggerUtils.STD_OUTPUT_HANDLE);
                return;
            }

            // Windows app
            bool success = LoggerUtils.AllocConsole();

            handleRoutine = new HandlerRoutine(Handler);
            LoggerUtils.SetConsoleCtrlHandler(handleRoutine, true);

            this.Visible = false;

            if (!success)
            {
                return;
            }

            buffer = LoggerUtils.CreateConsoleScreenBuffer(LoggerUtils.GENERIC_READ | LoggerUtils.GENERIC_WRITE,
                                                           LoggerUtils.FILE_SHARE_READ | LoggerUtils.FILE_SHARE_WRITE, IntPtr.Zero, LoggerUtils.CONSOLE_TEXTMODE_BUFFER, IntPtr.Zero);

            bool result = LoggerUtils.SetConsoleActiveScreenBuffer(buffer);

            //Set console output buffer size
            IntPtr handle = LoggerUtils.CreateFile(
                "CONOUT$",                                                    // name
                LoggerUtils.GENERIC_WRITE | LoggerUtils.GENERIC_READ,         // desired access
                LoggerUtils.FILE_SHARE_WRITE | LoggerUtils.FILE_SHARE_READ,   // share access
                null,                                                         // no security attributes
                LoggerUtils.OPEN_EXISTING,                                    // device already exists
                0,                                                            // no flags or attributes
                IntPtr.Zero);                                                 // no template file.

            consoleHandle = new SafeFileHandle(handle, true);
            const UInt16 conWidth  = 256;
            const UInt16 conHeight = 2500;
            Coord        dwSize    = new Coord(conWidth, conHeight);

            LoggerUtils.SetConsoleScreenBufferSize(consoleHandle.DangerousGetHandle(), dwSize);

            LoggerUtils.SetStdHandle(LoggerUtils.STD_OUTPUT_HANDLE, buffer);
            LoggerUtils.SetStdHandle(LoggerUtils.STD_ERROR_HANDLE, buffer);

            Title = "Logger Console";

            Stream       s      = Console.OpenStandardInput(LoggerUtils._DefaultConsoleBufferSize);
            StreamReader reader = null;

            if (s == Stream.Null)
            {
                reader = StreamReader.Null;
            }
            else
            {
                reader = new StreamReader(s, Encoding.GetEncoding(LoggerUtils.GetConsoleCP()),
                                          false, LoggerUtils._DefaultConsoleBufferSize);
            }

            Console.SetIn(reader);

            // Set up Console.Out
            StreamWriter writer = null;

            s = Console.OpenStandardOutput(LoggerUtils._DefaultConsoleBufferSize);
            if (s == Stream.Null)
            {
                writer = StreamWriter.Null;
            }
            else
            {
                writer = new StreamWriter(s, Encoding.GetEncoding(LoggerUtils.GetConsoleOutputCP()),
                                          LoggerUtils._DefaultConsoleBufferSize);
                writer.AutoFlush = true;
            }

            Console.SetOut(writer);

            s = Console.OpenStandardError(LoggerUtils._DefaultConsoleBufferSize);
            if (s == Stream.Null)
            {
                writer = StreamWriter.Null;
            }
            else
            {
                writer = new StreamWriter(s, Encoding.GetEncoding(LoggerUtils.GetConsoleOutputCP()),
                                          LoggerUtils._DefaultConsoleBufferSize);
                writer.AutoFlush = true;
            }

            Console.SetError(writer);

            //
            // Disable Close as this would close the main app also
            //
            hwnd = LoggerUtils.GetConsoleWindow();
            IntPtr hMenu = LoggerUtils.GetSystemMenu(hwnd, false);

            LoggerUtils.DeleteMenu(hMenu, LoggerUtils.SC_CLOSE, LoggerUtils.MF_BYCOMMAND);
            NativeMethods.SetWindowPos(hwnd, new IntPtr(-1), 0, 0, 0, 0, NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOSIZE);
        }