Programmatically pop XNA Console window

Started by
4 comments, last by Flimflam 12 years, 12 months ago
Hi everyone, I have been trying to figure this out for weeks on how to programmatically open a console window alongside an XNA game. I know you can open a console window by right clicking your project->properties->Application->Output type->"Console Application" but this makes it always appear. I am wondering how to have this same effect but via code. You may be asking why? The answer is simple, I want the Console window for Debugging & scripting purposes when running in debug mode on windows but any other time I just want the game to run.

In C# and DirectX games I use to just use the below code to achieve this, I am wondering if something similar works in XNA (note this opens a window but it is unable to accept input or write output):

[DllImport("kernel32.dll")]
public static extern Int32 AllocConsole();
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetConsoleWindow();
public void init() {
AllocConsole();
IntPtr MyConsole = GetConsoleWindow();
SetWindowPos(MyConsole, 0, 0, 0, 0, 0, 0x0001);
Console.WriteLine("*************************************************************");
Console.WriteLine("\t\t\tDEBUG CONSOLE");
Console.WriteLine("*************************************************************");
}


Any help or suggestions would be greatly appreciated.

Remember to mark someones post as helpful if you found it so.

Journal:

http://www.gamedev.net/blog/908-xxchesters-blog/

Portfolio:

http://www.BrandonMcCulligh.ca

Company:

www.gwnp.ca

Advertisement
You could set the output type to console window, and then P/Invoke GetConsoleWindow() and ShowWindow() on the returned IntPtr to hide it when you're not in debug mode.

That's what I'd do if I wanted to use an actual console window.

You could set the output type to console window, and then P/Invoke GetConsoleWindow() and ShowWindow() on the returned IntPtr to hide it when you're not in debug mode.

That's what I'd do if I wanted to use an actual console window.


PInvoke works but it feels like more of a bandaid than a fix. By this I am meaning that when I start my application in Release mode the window briefly shows up than closes right as the games window opens. Thank you for your sugegstion though, I am going to use it for now but hopefully will find a way to just conditionally mark the application as Console VS Window like I could in regular C#.

Thanks.

Remember to mark someones post as helpful if you found it so.

Journal:

http://www.gamedev.net/blog/908-xxchesters-blog/

Portfolio:

http://www.BrandonMcCulligh.ca

Company:

www.gwnp.ca

I wrote my own python console window that appears in game before, it'd serve your requirements nicely allowing output as well as the power of python scripting which works excellently with XNA. Have a look here: http://xnaconsole.codeplex.com/ The code used is, I found a bit overkill in parts. I made a simplified version in a couple hundred lines of code it's that easy and quick to implement :)
Portfolio & Blog:http://scgamedev.tumblr.com/

I wrote my own python console window that appears in game before, it'd serve your requirements nicely allowing output as well as the power of python scripting which works excellently with XNA. Have a look here: http://xnaconsole.codeplex.com/ The code used is, I found a bit overkill in parts. I made a simplified version in a couple hundred lines of code it's that easy and quick to implement :)


Thank you for your suggestion but this is not a viable option for me. I already have a great scripting system in place and I do not have the time (nor desire) to learn another scripting language.

Remember to mark someones post as helpful if you found it so.

Journal:

http://www.gamedev.net/blog/908-xxchesters-blog/

Portfolio:

http://www.BrandonMcCulligh.ca

Company:

www.gwnp.ca

PInvoke works but it feels like more of a bandaid than a fix. By this I am meaning that when I start my application in Release mode the window briefly shows up than closes right as the games window opens. Thank you for your sugegstion though, I am going to use it for now but hopefully will find a way to just conditionally mark the application as Console VS Window like I could in regular C#.

Thanks.


Well, it's not really a band-aid fix. It's just one of two valid methods of allocating a console. A lot of games do it. I know I've seen more than a few titles that will briefly show a console and then hide it immediately afterward. You can either do it that way or allocate a console yourself (as per your code in the first post). Both will ultimately achieve your goals.

This topic is closed to new replies.

Advertisement