Click here to Skip to main content
Licence 
First Posted 2 Jan 2005
Views 46,316
Bookmarked 23 times

Console and WinForm together for easy debugging

By Wilfried Mestdagh | 2 Jan 2005
Using a console in a Windows application to easily generate log and/or debug information.
4 votes, 36.4%
1

2

3
2 votes, 18.2%
4
5 votes, 45.5%
5
2.84/5 - 11 votes
μ 2.84, σa 3.35 [?]

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

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralDoes not work for me PinmemberZishan Haider5:04 26 May '05  
GeneralRe: Does not work for me PinmemberWilfried Mestdagh8:49 26 May '05  
GeneralRe: Does not work for me PinmemberZishan Haider1:43 27 May '05  
GeneralRe: Does not work for me PinmemberWilfried Mestdagh3:11 27 May '05  
GeneralRe: Does not work for me PinmemberZishan Haider3:15 27 May '05  
GeneralRe: Does not work for me Pinmemberorial3:18 14 Jul '10  
Generalwrite string Pinmemberescape_fz20:24 5 Jan '05  
GeneralRe: write string Pinmemberescape_fz20:38 5 Jan '05  
GeneralRe: write string PinmemberWilfried Mestdagh21:50 5 Jan '05  
GeneralRe: write string Pinmemberescape_fz22:11 5 Jan '05  
GeneralRe: write string PinmemberWilfried Mestdagh23:20 5 Jan '05  
GeneralRe: write string Pinmemberescape_fz14:24 6 Jan '05  
GeneralRe: write string PinmemberDavid Piepgrass12:00 11 Apr '05  
GeneralRe: write string PinmemberDavid Piepgrass12:05 11 Apr '05  
GeneralRe: write string PinmemberWilfried Mestdagh21:34 11 Apr '05  

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120209.1 | Last Updated 2 Jan 2005
Article Copyright 2005 by Wilfried Mestdagh
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid