Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.11 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using Microsoft.Win32.SafeHandles;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6.  
  7. namespace DLL
  8. {
  9.     /// <summary>
  10.     /// Allocates and manages a new console for the calling process.    
  11.     /// </summary>
  12.     public static class ConsoleCreator
  13.     {
  14.         [DllImport("kernel32.dll", EntryPoint = "AllocConsole", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  15.         private static extern int AllocConsole();
  16.  
  17.         [DllImport("kernel32.dll", EntryPoint = "FreeConsole", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  18.         private static extern int FreeConsole();
  19.  
  20.         [DllImport("kernel32.dll")]
  21.         private static extern IntPtr GetConsoleWindow();
  22.  
  23.         #region hiding windows
  24.         [DllImport("user32.dll")]
  25.         private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  26.  
  27.         private const int SW_HIDE = 0;
  28.         private const int SW_SHOW = 5;
  29.         #endregion
  30.  
  31.         [DllImport("kernel32.dll", EntryPoint = "GetStdHandle", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  32.         private static extern IntPtr GetStdHandle(int nStdHandle);
  33.  
  34.         private const int STD_OUTPUT_HANDLE = -11;
  35.  
  36.         /// <summary>
  37.         /// Allocate new console.
  38.         /// </summary>
  39.         /// <exception cref="InvalidOperationException">Console already attached.</exception>
  40.  
  41.         public static void CreateConsoleWindow()
  42.         {
  43.             var handle = GetConsoleWindow();
  44.  
  45.             if (GetConsoleWindow() == IntPtr.Zero)
  46.             {
  47.                 AllocConsole();
  48.                 IntPtr stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  49.                 SafeFileHandle safeFileHandle = new SafeFileHandle(stdHandle, true);
  50.                 FileStream fileStream = new FileStream(safeFileHandle, FileAccess.Write);
  51.                 StreamWriter standardOutput = new StreamWriter(fileStream) { AutoFlush = true };
  52.                 Console.OutputEncoding = Encoding.UTF8;
  53.                 Console.SetOut(standardOutput);
  54.             }
  55.             else
  56.             {
  57.                 throw new InvalidOperationException("Console already attached");
  58.             }
  59.          
  60.         }
  61.         /// <summary>
  62.         /// Remove console.
  63.         /// </summary>
  64.         public static void RemoveConsoleWindow()
  65.         {
  66.             FreeConsole();
  67.             //redirect output to standard output.
  68.             var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true };
  69.             Console.SetOut(sw);
  70.         }
  71.  
  72.         /// <summary>
  73.         /// Show console.
  74.         /// </summary>
  75.         public static void ShowConsoleWindow()
  76.         {
  77.             var handle = GetConsoleWindow();
  78.             ShowWindow(handle, SW_SHOW);
  79.         }
  80.  
  81.         /// <summary>
  82.         /// Hide console.
  83.         /// </summary>
  84.         public static void HideConsoleWindow()
  85.         {
  86.             var handle = GetConsoleWindow();
  87.             ShowWindow(handle, SW_HIDE);
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement