I have a WinForm project that opens a console for debugging. This is very common for game development in C++. When I try this in C#, I'm not getting the desired result. I think it's because I need to set the standard output to the console.
I was following this: http://vbcity.com/blogs/jatkinson/archive/2010/02/17/windows-interop-inside-kernel32-part-1-allocconsole-amp-createhardlink.aspx
While it used Console.WriteLine(), it skips setting the output handle to the console. How can I set that in C#?
My code:
namespace MVS
{
public partial class frmMVS : Form
{
[DllImport("kernel32")]
static extern bool AllocConsole();
public frmMVS()
{
InitializeComponent();
AllocConsole();
Console.WriteLine("Hello World!");
}
}
}
Current attempt (though nothing is outputted to the console): Source: No console output when using AllocConsole and target architecture x86
namespace MVS
{
public partial class frmMVS : Form
{
public frmMVS()
{
InitializeComponent();
CreateConsole();
Console.WriteLine("Hello World!");
}
public static void CreateConsole()
{
AllocConsole();
// stdout's handle seems to always be equal to 7
IntPtr defaultStdout = new IntPtr(7);
IntPtr currentStdout = GetStdHandle(StdOutputHandle);
if (currentStdout != defaultStdout)
// reset stdout
SetStdHandle(StdOutputHandle, defaultStdout);
// reopen stdout
TextWriter writer = new StreamWriter(Console.OpenStandardOutput())
{ AutoFlush = true };
Console.SetOut(writer);
}
// P/Invoke required:
private const UInt32 StdOutputHandle = 0xFFFFFFF5;
[DllImport("kernel32.dll")]
private static extern IntPtr GetStdHandle(UInt32 nStdHandle);
[DllImport("kernel32.dll")]
private static extern void SetStdHandle(UInt32 nStdHandle, IntPtr handle);
[DllImport("kernel32")]
static extern bool AllocConsole();
}
}
Current solution: For now, my current solution is to remove all this interop code and set the Output Type to Console Application from my Windows application. This allows both the form and console to work side by side. Because users running the application will also see the console, remember to set the Output Type back to Windows before publishing. Since the console is used for debugging anyway, this seems like an easy solution for its purpose. I don't know how it's going to affect the solution as I develop, so an alternative is still of interest.