Данный пример демонстрирует возможность запуска Windows Form приложения в либо режиме консоли (консольное окно)
либо как как обычное Windows Form приложение.
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace FormConsole
{
public class ConsoleOUT : IDisposable
{
//
// Импортируем фнкции из kernel32.dll
//
[DllImport("kernel32.dll", SetLastError=true)]
protected static extern bool AllocConsole();
[DllImport("kernel32.dll", SetLastError=false)]
protected static extern bool FreeConsole();
[DllImport("kernel32.dll", SetLastError=true)]
protected static extern IntPtr GetStdHandle( int nStdHandle );
[DllImport("kernel32.dll", SetLastError=true)]
protected static extern bool SetStdHandle( int nStdHandle,
IntPtr hConsoleOutput );
[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
protected static extern IntPtr CreateFile(
string fileName,
int desiredAccess,
int shareMode,
IntPtr securityAttributes,
int creationDisposition,
int flagsAndAttributes,
IntPtr templateFile );
[DllImport("kernel32.dll", ExactSpelling=true, SetLastError=true)]
protected static extern bool CloseHandle( IntPtr handle );
private static IntPtr conOut;
private static IntPtr oldOut;
/// <summary>
/// ConsoleOUT
/// </summary>
public ConsoleOUT()
{
if (oldOut == IntPtr.Zero)
oldOut = GetStdHandle( -11 );
if (!AllocConsole())
throw new Exception("AllocConsole");
conOut = CreateFile( "CONOUT$", 0x40000000, 2,
IntPtr.Zero, 3, 0, IntPtr.Zero );
if (!SetStdHandle(-11, conOut))
throw new Exception("SetStdHandle");
Stream standard = Console.OpenStandardOutput();
StreamWriter writer = new StreamWriter(standard);
writer.AutoFlush = true;
//
Console.SetOut(writer);
Console.SetError(writer);
}
/// <summary>
/// Dispose
/// </summary>
public void Dispose()
{
if (! CloseHandle(conOut))
throw new Exception("CloseHandle");
conOut = IntPtr.Zero;
if (! FreeConsole())
throw new Exception("FreeConsole");
if (!SetStdHandle(-11, oldOut))
throw new Exception("SetStdHandle");
}
}
}