Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C#
Article

Console and WinForm together for easy debugging

Rate me:
Please Sign up or sign in to vote.
3.05/5 (12 votes)
2 Jan 2005 80.4K   2   28   17
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:

C#
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.

C#
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:

C#
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


Written By
Web Developer
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThanks! Pin
Member 117485689-Mar-18 14:55
Member 117485689-Mar-18 14:55 
GeneralDoes not work for me Pin
Zishan Haider26-May-05 4:04
Zishan Haider26-May-05 4:04 
GeneralRe: Does not work for me Pin
Wilfried Mestdagh26-May-05 7:49
Wilfried Mestdagh26-May-05 7:49 
GeneralRe: Does not work for me Pin
Zishan Haider27-May-05 0:43
Zishan Haider27-May-05 0:43 
GeneralRe: Does not work for me Pin
Wilfried Mestdagh27-May-05 2:11
Wilfried Mestdagh27-May-05 2:11 
GeneralRe: Does not work for me Pin
Zishan Haider27-May-05 2:15
Zishan Haider27-May-05 2:15 
GeneralRe: Does not work for me Pin
Timur Seitosmanov14-Jul-10 2:18
Timur Seitosmanov14-Jul-10 2:18 
Generalwrite string Pin
escape_fz5-Jan-05 19:24
escape_fz5-Jan-05 19:24 
GeneralRe: write string Pin
escape_fz5-Jan-05 19:38
escape_fz5-Jan-05 19:38 
GeneralRe: write string Pin
Wilfried Mestdagh5-Jan-05 20:50
Wilfried Mestdagh5-Jan-05 20:50 
GeneralRe: write string Pin
escape_fz5-Jan-05 21:11
escape_fz5-Jan-05 21:11 
GeneralRe: write string Pin
Wilfried Mestdagh5-Jan-05 22:20
Wilfried Mestdagh5-Jan-05 22:20 
GeneralRe: write string Pin
escape_fz6-Jan-05 13:24
escape_fz6-Jan-05 13:24 
GeneralRe: write string Pin
Qwertie25611-Apr-05 11:00
Qwertie25611-Apr-05 11:00 
GeneralRe: write string Pin
Qwertie25611-Apr-05 11:05
Qwertie25611-Apr-05 11:05 
GeneralRe: write string Pin
Wilfried Mestdagh11-Apr-05 20:34
Wilfried Mestdagh11-Apr-05 20:34 
GeneralRe: write string Pin
Atwind19-Mar-20 22:16
Atwind19-Mar-20 22:16 

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

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