|
using System;
using System.Runtime.InteropServices;
namespace ZipMyCode.Cli
{
/// <summary>
/// Class containing methods responsible for attaching to and detatching from the console.
/// </summary>
static class ConsoleHelper
{
private const int ATTACH_PARENT_PROCESS = -1;
/// <summary>
/// Attaches the process to the console or creates a new if needed.
/// </summary>
public static void AttachOrCreate()
{
if (!AttachConsole(ATTACH_PARENT_PROCESS))
{
AllocConsole();
};
// Write a empty line since the code above doesn't work as intended. Creating or attaching to
// a console somehow produces the first call to Console.WriteLine to print on a new DOS
// prompt.
Console.WriteLine();
}
/// <summary>
/// Detaches the process from the console.
/// </summary>
public static void Detach()
{
FreeConsole();
}
[DllImport("kernel32", SetLastError = true)]
static extern bool AllocConsole();
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool AttachConsole(int dwProcessId);
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool FreeConsole();
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Got my first computer in the 90's and loved it even though it sounded like a coffeemaker.
Now getting paid for designing cool applications, and drinks the coffee instead of listening to it being made.