Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Hybrid console/windows app

4 views
Skip to first unread message

Chad Myers

unread,
Nov 4, 2002, 4:45:11 PM11/4/02
to
I know this has been asked before, but is there a good way
to make a hybrid EXE that can be both a command-line and
Windows Forms app?

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]

unread,
Nov 4, 2002, 6:52:47 PM11/4/02
to
Chad, is this just for debugging? If it is, anything that you
Console.Write() goes to the Output window when you have a WinForm
application. If it's not for debugging in the debugger I don't understand
the use case. Could you clarify if that's not it?

--
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...

Tom Shelton

unread,
Nov 4, 2002, 7:04:58 PM11/4/02
to

"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


Chris R

unread,
Nov 4, 2002, 7:07:36 PM11/4/02
to
The program needs to know, at compile time, whether it's going to be a
Windows Application or a Console Application.

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...

Chad Myers

unread,
Nov 4, 2002, 10:32:21 PM11/4/02
to
I haven't tested this yet (as I'm not at work) but this
looks like a winner.

Thanks Tom!
-c

"Tom Shelton" <to...@dakcs.com> wrote in message
news:ex3fL7FhCHA.2288@tkmsftngp12...

Chad Myers

unread,
Nov 4, 2002, 10:33:54 PM11/4/02
to
What I need is a Windows app, that if passed a switch from the
command line or Start->Run prompt will run in cmd-line mode.

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...

Smola

unread,
Nov 5, 2002, 4:18:19 AM11/5/02
to

"Chad Myers" <cmy...@N0.SP.4M.austin.rr.com> wrote in message
news:C_Gx9.258754$8o3.7...@twister.austin.rr.com...

> What I need is a Windows app, that if passed a switch from the
> command line or Start->Run prompt will run in cmd-line mode.
>
> 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.

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.


Tom Shelton

unread,
Nov 5, 2002, 1:15:34 PM11/5/02
to

"Chad Myers" <cmy...@N0.SP.4M.austin.rr.com> wrote in message
news:C_Gx9.258754$8o3.7...@twister.austin.rr.com...

> What I need is a Windows app, that if passed a switch from the
> command line or Start->Run prompt will run in cmd-line mode.
>
> 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
>

Yes, it will.

Tom Shelton


Chad Myers

unread,
Nov 5, 2002, 2:04:27 PM11/5/02
to

"Tom Shelton" <to...@dakcs.com> wrote in message
news:OMu4mcPhCHA.4088@tkmsftngp08...

Hrm, that kinda sucks. So if I spawn from a console, I'll end
up with two consoles?

-c


Tom Shelton

unread,
Nov 5, 2002, 3:22:50 PM11/5/02
to

"Chad Myers" <cmy...@N0.SP.4M.austin.rr.com> wrote in message
news:%CUx9.264049$8o3.7...@twister.austin.rr.com...

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


Chad Myers

unread,
Nov 5, 2002, 4:45:40 PM11/5/02
to

"Tom Shelton" <to...@dakcs.com> wrote in message
news:eijfujQhCHA.1364@tkmsftngp12...

> > 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


0 new messages