Click here to Skip to main content
Click here to Skip to main content

Tagged as

Attaching a Console to a WinForms application

, 7 Feb 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
How to have a real console window as well as your forms
When debugging a Windows Forms application, the Console class is often used (in place of the Debug class) to write to the IDE's Output window.
 
It is possible to attach one actual console window to the process by using the AllocConsole function from kernel32.dll and its matching FreeConsole to release it.
 
This is not restricted to debugging, but that's probably where it has the most use.
 
An example of usage:
 
using System;
using System.Windows.Forms;
 
namespace FormWithConsole
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
#if DEBUG
            NativeMethods.AllocConsole();
            Console.WriteLine("Debug Console");
#endif
            Application.Run(new FormMain());
#if DEBUG
            NativeMethods.FreeConsole();
#endif
        }
    }
}
 
and the class containing the P/Invoke functions:
 
using System;
using System.Runtime.InteropServices;
 
namespace FormWithConsole
{
    internal static class NativeMethods
    {
        // http://msdn.microsoft.com/en-us/library/ms681944(VS.85).aspx
        /// <summary>
        /// Allocates a new console for the calling process.
        /// </summary>
        /// <returns>nonzero if the function succeeds; otherwise, zero.</returns>
        /// <remarks>
        /// A process can be associated with only one console,
        /// so the function fails if the calling process already has a console.
        /// </remarks>
        [DllImport("kernel32.dll", SetLastError = true)]
        internal static extern int AllocConsole();
 
        // http://msdn.microsoft.com/en-us/library/ms683150(VS.85).aspx
        /// <summary>
        /// Detaches the calling process from its console.
        /// </summary>
        /// <returns>nonzero if the function succeeds; otherwise, zero.</returns>
        /// <remarks>
        /// If the calling process is not already attached to a console,
        /// the error code returned is ERROR_INVALID_PARAMETER (87).
        /// </remarks>
        [DllImport("kernel32.dll", SetLastError = true)]
        internal static extern int FreeConsole();
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

DaveyM69
CEO Dave Meadowcroft
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralAnother way PinmemberGSerjo30-Jul-12 12:52 
GeneralRe: Another way PinmemberJason Vogel18-Mar-13 6:57 
GeneralRe: Thank you very much for the explanation. I get the idea now ... Pinmemberhoernchenmeister8-Feb-12 1:19 
GeneralRe: Hi You can (early in your program) implement a ConsoleTrac... Pinmemberwvd_vegt8-Feb-12 1:04 
GeneralRe: Thanks for your comment. Yes, you are right, I use Console.W... Pinmemberhoernchenmeister7-Feb-12 23:46 
GeneralRe: thanks ! .. I might get to test it later today :-) PinmemberGarth J Lancaster7-Aug-11 14:13 
GeneralReason for my vote of 5 good one. Pinmembernikhi _singh26-Feb-12 18:46 
GeneralJust a minor remark on extern calls. When researching why my... PinmemberTetheredSun6-Feb-12 23:20 
GeneralRe: Good point - in winDef.h BOOL is defined like: typedef i... PinmentorDaveyM697-Feb-12 11:37 
GeneralJust a quick note. The console window has to be attached bef... Pinmemberhoernchenmeister5-Feb-12 22:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 7 Feb 2012
Article Copyright 2010 by DaveyM69
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid