If I set the proj type to be Windows, then I don't get
a console, so I never see any output.
If I set the proj type to command-line, then the console
appears even when the Windows Forms appear.
Is ther a happy medium, or do I need seperate GUI
and command-line EXEs?
Thanks,
Chad
--
Greg Ewing [MVP]
http://www.claritycon.com/
"Chad Myers" <cmy...@N0.SP.4M.austin.rr.com> wrote in message
news:HTBx9.258018$8o3.7...@twister.austin.rr.com...
public class Form1 : System.Windows.Forms.Form
{
[DllImport("kernel32", SetLastError=true)]
private static extern bool AllocConsole();
[DllImport("kernel32", SetLastError=true)]
private static extern bool FreeConsole();
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
Console.WriteLine("Hello!");
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Form1.AllocConsole();
Application.Run(new Form1());
Form1.FreeConsole();
}
}
}
Of course you could extend this to only allocate the console
conditionally...
Tom Shelton
If you compile a Form based exe as a Console Application, you can still
run the Form as a owned of the Console application,
static void Main(string[] args)
{
if( args.Length == 0 )
{
// Runs the app with both a console window and a Form.
Application.Run(new Form1());
}
else
{
// Runs as a console window only
Console.WriteLine( "Test" );
string test = Console.ReadLine();
}
}
I think a better option would be to create two projects, using a dll to
hold common methodology.
Chris R.
"Chad Myers" <cmy...@N0.SP.4M.austin.rr.com> wrote in message
news:HTBx9.258018$8o3.7...@twister.austin.rr.com...
Thanks Tom!
-c
"Tom Shelton" <to...@dakcs.com> wrote in message
news:ex3fL7FhCHA.2288@tkmsftngp12...
I suppose I could just not display the initial form, but I
would like to output to the console when it's in cmd-line mode.
The only question is, will Tom's recommendation to use
AllocConsole and FreeConsole create a new console, even when
running it from another.
-c
"Chris R" <sot...@bellsouth.net> wrote in message
news:BUDx9.2740$7F2....@news.bellsouth.net...
Yes it will, it can't be done otherways. I actualy read it somewhere in the
Win32API documentation. I remember there was some problems with the new
console (created with AllocConsole) not getting the rigth IO handles.
Yes, it will.
Tom Shelton
Hrm, that kinda sucks. So if I spawn from a console, I'll end
up with two consoles?
-c
Yep.... And to be honest, I just can't think of a good way around this...
When I answered the original post, I somehow had it in my head that you
wanted to conditionally have your windows app have a console available.
Sorry.
Tom Shelton
> > Hrm, that kinda sucks. So if I spawn from a console, I'll end
> > up with two consoles?
> >
> > -c
>
> Yep.... And to be honest, I just can't think of a good way around
this...
> When I answered the original post, I somehow had it in my head that
you
> wanted to conditionally have your windows app have a console
available.
> Sorry.
No problem. It was still a good answer.
I found that there is a method called AttachConsole, but you must have
the PID of the process whose console you wish to attach. I couldn't
figure out a way to get my parent or spawning or calling process id
to determine if its was cmd.exe or not. Oh well, I'll just do the
two-exe trick.
-c