42 lines
1.6 KiB
C#
42 lines
1.6 KiB
C#
using System;
|
|
using System.Text;
|
|
using System.IO;
|
|
using System.Runtime.InteropServices;
|
|
using Microsoft.Win32.SafeHandles;
|
|
|
|
namespace Aiwaz.Common
|
|
{
|
|
public class Win32Console : IDisposable
|
|
{
|
|
[DllImport("kernel32.dll", EntryPoint = "FreeConsole", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int FreeConsole();
|
|
[DllImport("kernel32.dll", EntryPoint = "GetStdHandle", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
|
|
private static extern IntPtr GetStdHandle(int nStdHandle);
|
|
[DllImport("kernel32.dll", EntryPoint = "AllocConsole", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
|
|
private static extern int AllocConsole();
|
|
|
|
private const int STD_OUTPUT_HANDLE = -11;
|
|
private const int MY_CODE_PAGE = 437;
|
|
|
|
public Win32Console()
|
|
{
|
|
AllocConsole();
|
|
IntPtr stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
SafeFileHandle safeFileHandle = new SafeFileHandle(stdHandle, true);
|
|
FileStream fileStream = new FileStream(safeFileHandle, FileAccess.Write);
|
|
Encoding encoding = System.Text.Encoding.GetEncoding(MY_CODE_PAGE);
|
|
StreamWriter standardOutput = new StreamWriter(fileStream, encoding);
|
|
standardOutput.AutoFlush = true;
|
|
Console.SetOut(standardOutput);
|
|
}
|
|
|
|
#region IDisposable Members
|
|
|
|
public void Dispose()
|
|
{
|
|
FreeConsole();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |