Created
September 3, 2018 02:25
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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