Sign in to follow this  
XXChester

Programmatically pop XNA Console window

Recommended Posts

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):
[code]
[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("*************************************************************");
}
[/code]

Any help or suggestions would be greatly appreciated.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='Flimflam' timestamp='1302656622' post='4797733']
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.
[/quote]

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.

Share this post


Link to post
Share on other sites
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: [url="http://xnaconsole.codeplex.com/"]http://xnaconsole.codeplex.com/[/url] 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 :)

Share this post


Link to post
Share on other sites
[quote name='Darg' timestamp='1302829991' post='4798623']
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: [url="http://xnaconsole.codeplex.com/"]http://xnaconsole.codeplex.com/[/url] 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 :)
[/quote]

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.

Share this post


Link to post
Share on other sites
[quote name='XXChester' timestamp='1302814241' post='4798555']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.[/quote]

Well, it's not [i]really [/i]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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this