Sharp-Developer.NET     Powered by ASP.NET
Current language is English
Switch to Russian
Main
  My blog
  Articles
  Take fun
  Useful links
.NET Utils
  RegexTuner .NET
  Code2Html .NET
Google
  Toolbar buttons
.NET CodeBank
  WinForms
  Helper classes
  #ManagmentConsole
ASP.NET Projects
  AdSenseASP.NET
About me
  CV / Resume

GUI Console

This is an example how you can use Console in you GUI applications. With a helper class that get a work with managing console on itself.

GUI Console Dowbload:

GuiConsoleSrc.zip [C#,5KB]
Source codes solution

GuiConsoleBin.zip [10KB]
Example's binary

Note:
This application required Microsoft .NET Framewowrk

GuiConsole class

For gettting access to console we should export some procedures from system library 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 );
The implementation of the class that help you to work with console in GUI application consist from 2 methods. The first one create console.
public static void CreateConsole()
{
    if (hasConsole)
        return;

    if (oldOut == IntPtr.Zero)
        oldOut = GetStdHandle( -11 );

    if (! AllocConsole())
        throw new Exception("AllocConsole() failed";

    conOut = CreateFile( "CONOUT$" 0x40000000, 2, IntPtr.Zero, 3, 0, IntPtr.Zero );

    if (! SetStdHandle(-11, conOut))
        throw new Exception("SetStdHandle() failed";

    StreamToConsole();

    hasConsole = true;
}

private static void StreamToConsole()
{
    Stream cstm = Console.OpenStandardOutput();
    StreamWriter cstw = new StreamWriter( cstm );
    cstw.AutoFlush = true;
    Console.SetOut( cstw );
    Console.SetError( cstw );
}
We store in static private variables pointers to the current and old standard ouptut. Also we setuping flag is console already opened or not.
Here is a declaration of private variables:
private static bool hasConsole = false;
private static IntPtr conOut;
private static IntPtr oldOut;
When we finishing to work with console we should to close one and repair previous state of standard output. This functionalyty realized in the next method:
public static void ReleaseConsole()
{
    if (! hasConsole)
        return;

    if (! CloseHandle(conOut))
        throw new Exception("CloseHandle() failed";

    conOut = IntPtr.Zero;

    if (! FreeConsole())
        throw new Exception("FreeConsole() failed";

    if (! SetStdHandle(-11, oldOut))
        throw new Exception("SetStdHandle() failed";

    hasConsole = false;
}

ALL CODES AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

Terms of Use   |   Advertising   |   Feedback
Copyright © 2003-2006 Alexander S. Trakhimenok. Designed by Webmax.ru
RegexTuner .NET

The most powerful utility for understanding, writing & debugging regular expressions in the easiest way.
Find out more facts
Your feedback!