
Untitled
By: a guest on
May 30th, 2012 | syntax:
None | size: 1.30 KB | hits: 19 | expires: Never
Behavior in WinForm/Console Hybrid Application
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern bool AllocConsole();
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern bool AttachConsole(int pid);
...
if (!AttachConsole(-1))
{
AllocConsole();
}
// nowin is a bool that is set based on a parameter
if (nowin)
{
if (!AttachConsole(-1))
{
AllocConsole();
}
//... Console mode code here...
}
else
{
// run in window
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(argDict));
}
[STAThread]
public static int Main(string[] args)
{
if (args.Length > 0)
{
// run console code here
}
else
{
// start up the win form app
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
return 0;
}
static void Main(params string[] args)
{
if (args.Length > 0 && args[0] == "consolemode")
{
// do stuff
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}