• 0

C# adding a console window in a windows app


Question

17 answers to this question

Recommended Posts

  • 0

I don't think there is any .NET call for that, but the native function is AllocConsole and you could call that with interop:

[DllImport("kernel32")]
static extern int AllocConsole();

static void Main()
{
        AllocConsole();
}

If you call AllocConsole before calling any console functions, that should be all you need. Otherwise you need to also manually set up stdout and friends. FreeConsole is the opposite of AllocConsole, and is used to get rid of the console.

  • 0

using System.Runtime.InteropServices;

[DllImport("kernel32.dll", EntryPoint = "AllocConsole", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

private static extern int AllocConsole();

AllocConsole();

Console.WriteLine("Hello World!"); // outputs to console window

  • 0

Wouldn't it make more sense for a GUI app to have a dialog/form/whatever with a textbox that you write to instead? Is there something about the console that you specifically need? If you're just trying to "check what you're doing," I think allocating a console is inappropriate. Especially since the Windows console hasn't changed since Windows 98

It's usually better to tell us what goal you're trying to achieve. Lots of people use the wrong tool for the job.

  • 0

The console is a quick and useful way of outputting debug messages.

Check out the Debug.Writeline method if you are using Visual Studio, this is an easy way of outputting debug messages http://msdn.microsoft.com/en-us/library/system.diagnostics.debug.writeline.aspx

  • 0

Check out the Debug.Writeline method if you are using Visual Studio, this is an easy way of outputting debug messages http://msdn.microsoft.com/en-us/library/system.diagnostics.debug.writeline.aspx

I do not write managed code, but presumably it is equivalent to the native debugging functions, which means you actually have to have a debugger attached in order to see the output. Sometimes you just want to display debug messages without the need for that.

  • 0

Easy, Just go to the property of the project and make the Application Type : Console Application

and Writes Console.WriteLine() :)

That's not what he wants. His application is NOT a console application but he still wants to send text to a console. A solution has already been posting anyway

  • 0

Check out the Debug.Writeline method if you are using Visual Studio, this is an easy way of outputting debug messages http://msdn.microsoft.com/en-us/library/system.diagnostics.debug.writeline.aspx

Use this instead of console, visual studio will then output this for you internally instead of having one extra window spawning for your console, that also goes away if your program crashes.

If you need to read debug-output from deployed applications or similar have a look on Log4Net(http://logging.apache.org/log4net/index.html) that allows you to configure exactly how you want your debug lines to be output (console/debug, file, to server etc)

  • 0

Your not writing managed code but you are using C# :/

Are you talking to me? I do not write managed code in C# or any other language, but I can still guess the answer to simpler questions. I said this to make it clear that I had no expertise and was guessing that it was equivalent to certain native APIs. I was saying that a console can be useful because you sometimes want to output certain information to something other than a debugger. The two complement each other, it's not one or the other.

  • 0

Are you talking to me? I do not write managed code in C# or any other language, but I can still guess the answer to simpler questions. I said this to make it clear that I had no expertise and was guessing that it was equivalent to certain native APIs. I was saying that a console can be useful because you sometimes want to output certain information to something other than a debugger. The two complement each other, it's not one or the other.

Ah was somehow confusing you with the OP :p :whistle: *sneaks out the back door*

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.