Skip to content

Instantly share code, notes, and snippets.

@ForeverZer0
Created September 3, 2018 02:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ForeverZer0/d1bc9f593765de33a9edbbcd3fffc2e6 to your computer and use it in GitHub Desktop.
Save ForeverZer0/d1bc9f593765de33a9edbbcd3fffc2e6 to your computer and use it in GitHub Desktop.
Simple helper methods for spawning/closing an attached console to a Windows application.
using System;
using System.Runtime.InteropServices;
namespace MyNamespace
{
public static class ConsoleHelper
{
private const int SW_SHOW = 5;
public static void Show()
{
var handle = GetConsoleWindow();
if (handle != IntPtr.Zero)
ShowWindow(handle, SW_SHOW);
else
AllocConsole();
}
public static void Close() => FreeConsole();
[DllImport("kernel32")]
private static extern bool AllocConsole();
[DllImport("kernel32")]
private static extern bool FreeConsole();
[DllImport("kernel32")]
private static extern IntPtr GetConsoleWindow();
[DllImport("user32")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment