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

Console and WinForm together for easy debugging

, 2 Jan 2005
Rate this:
Please Sign up or sign in to vote.
Using a console in a Windows application to easily generate log and/or debug information.

Introduction

Often, you need a console window together with a WinForm application. It can be very handy for debugging purposes whilst developing, but also for a (temporary) logging of some data. It is very simple to do. Following program demonstrates it, using P/Invoke.

The only things you have to add to your project are following:

public class Win32
{
        [DllImport("kernel32.dll")]
        public static extern Boolean AllocConsole();
        [DllImport("kernel32.dll")]
        public static extern Boolean FreeConsole();
}

In the using clause, you have to add this, which is needed to call external applications like in this case the win32 API DLL.

using System.Runtime.InteropServices;

Testing the code

To test it, start a new Windows application, drop a CheckBox on the form, name it ViewConsole and copy following code:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;   // needed to call external application

namespace WindowsApplication1
{
    partial class Form1: Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void ViewConsole_CheckedChanged(object sender, EventArgs e)
        {
            if (ViewConsole.Checked)
                Win32.AllocConsole();
            else
                Win32.FreeConsole();
        }
    }

    public class Win32
    {
        [DllImport("kernel32.dll")]
        public static extern Boolean AllocConsole();
        [DllImport("kernel32.dll")]
        public static extern Boolean FreeConsole();
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Wilfried Mestdagh
Web Developer
Belgium Belgium
No Biography provided

Comments and Discussions

 
GeneralDoes not work for me PinmemberZishan Haider26-May-05 4:04 
GeneralRe: Does not work for me PinmemberWilfried Mestdagh26-May-05 7:49 
GeneralRe: Does not work for me PinmemberZishan Haider27-May-05 0:43 
GeneralRe: Does not work for me PinmemberWilfried Mestdagh27-May-05 2:11 
GeneralRe: Does not work for me PinmemberZishan Haider27-May-05 2:15 
GeneralRe: Does not work for me Pinmemberorial14-Jul-10 2:18 
Generalwrite string Pinmemberescape_fz5-Jan-05 19:24 
GeneralRe: write string Pinmemberescape_fz5-Jan-05 19:38 
GeneralRe: write string PinmemberWilfried Mestdagh5-Jan-05 20:50 
GeneralRe: write string Pinmemberescape_fz5-Jan-05 21:11 
GeneralRe: write string PinmemberWilfried Mestdagh5-Jan-05 22:20 
GeneralRe: write string Pinmemberescape_fz6-Jan-05 13:24 
GeneralRe: write string PinmemberDavid Piepgrass11-Apr-05 11:00 
GeneralRe: write string PinmemberDavid Piepgrass11-Apr-05 11:05 
GeneralRe: write string PinmemberWilfried Mestdagh11-Apr-05 20:34 

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 | Mobile
Web01 | 2.8.140721.1 | Last Updated 2 Jan 2005
Article Copyright 2005 by Wilfried Mestdagh
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid