Click here to Skip to main content
6,597,576 members and growing! (18,541 online)
Email Password   helpLost your password?
Languages » C# » Samples     Beginner

Console and WinForm together for easy debugging

By Wilfried Mestdagh

Using a console in a Windows application to easily generate log and/or debug information.
C++, C#, Windows, .NET, Visual Studio, Dev
Posted:2 Jan 2005
Views:35,363
Bookmarked:17 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
11 votes for this article.
Popularity: 2.96 Rating: 2.84 out of 5
4 votes, 36.4%
1

2

3
2 votes, 18.2%
4
5 votes, 45.5%
5

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


Member

Occupation: Web Developer
Location: Belgium Belgium

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
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  
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    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 Jan 2005
Editor: Sumalatha K.R.
Copyright 2005 by Wilfried Mestdagh
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project