andrew4582

Console Manager

Apr 27th, 2013
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Security;
  6. using System.Runtime.InteropServices;
  7. using System.Diagnostics;
  8. using System.IO;
  9.  
  10. namespace Common.Diagnostics
  11. {
  12.     [SuppressUnmanagedCodeSecurity]
  13.     public static class ConsoleManager
  14.     {
  15.         private const string Kernel32_DllName = "kernel32.dll";
  16.  
  17.         [DllImport(Kernel32_DllName)]
  18.         private static extern bool AllocConsole();
  19.  
  20.         [DllImport(Kernel32_DllName)]
  21.         private static extern bool FreeConsole();
  22.  
  23.         [DllImport(Kernel32_DllName)]
  24.         private static extern IntPtr GetConsoleWindow();
  25.  
  26.         [DllImport(Kernel32_DllName)]
  27.         private static extern int GetConsoleOutputCP();
  28.  
  29.         public static bool HasConsole
  30.         {
  31.             get { return GetConsoleWindow() != IntPtr.Zero; }
  32.         }
  33.  
  34.         /// <summary>
  35.         /// Creates a new console instance if the process is not attached to a console already.
  36.         /// </summary>
  37.         public static void Show()
  38.         {
  39.             //#if DEBUG
  40.             if (!HasConsole)
  41.             {
  42.                 AllocConsole();
  43.                 InvalidateOutAndError();
  44.             }
  45.             //#endif
  46.         }
  47.  
  48.         /// <summary>
  49.         /// If the process has a console attached to it, it will be detached and no longer visible. Writing to the System.Console is still possible, but no output will be shown.
  50.         /// </summary>
  51.         public static void Hide()
  52.         {
  53.             //#if DEBUG
  54.             if (HasConsole)
  55.             {
  56.                 SetOutAndErrorNull();
  57.                 FreeConsole();
  58.             }
  59.             //#endif
  60.         }
  61.  
  62.         public static void Toggle()
  63.         {
  64.             if (HasConsole)
  65.             {
  66.                 Hide();
  67.             }
  68.             else
  69.             {
  70.                 Show();
  71.             }
  72.         }
  73.  
  74.         static void InvalidateOutAndError()
  75.         {
  76.             Type type = typeof(System.Console);
  77.  
  78.             System.Reflection.FieldInfo _out = type.GetField("_out",
  79.                 System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
  80.  
  81.             System.Reflection.FieldInfo _error = type.GetField("_error",
  82.                 System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
  83.  
  84.             System.Reflection.MethodInfo _InitializeStdOutError = type.GetMethod("InitializeStdOutError",
  85.                 System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
  86.  
  87.             Debug.Assert(_out != null);
  88.             Debug.Assert(_error != null);
  89.  
  90.             Debug.Assert(_InitializeStdOutError != null);
  91.  
  92.             _out.SetValue(null, null);
  93.             _error.SetValue(null, null);
  94.  
  95.             _InitializeStdOutError.Invoke(null, new object[] { true });
  96.         }
  97.  
  98.         static void SetOutAndErrorNull()
  99.         {
  100.             Console.SetOut(TextWriter.Null);
  101.             Console.SetError(TextWriter.Null);
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment