Click here to Skip to main content
14,602,124 members

Console and WinForm together for easy debugging

Rate this:
3.05 (12 votes)
Please Sign up or sign in to vote.
3.05 (12 votes)
2 Jan 2005
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

Share

About the Author

Wilfried Mestdagh
Web Developer
Belgium Belgium
No Biography provided

Comments and Discussions

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

Article
Posted 2 Jan 2005

Stats

71.3K views
27 bookmarked