Writing to the console from a GUI application

E

eduardorp1

I have a GUI application that needs to be invoked from the command line
as well. It should work both as a GUI and as a console application.

When I invoke it from the command line, it reads all the command line
options perfectly. However, I can't use Console.WriteLine to write
messages to the console (the lines don't appear at the console window).

Is it possible? What should I do to "connect" the console?

My Main() is:

static void Main(string[] args)
{
if (args.Length == 0)
{
Application.Run(new WorkspaceForm());
return;
}
if (args.Length == 2 && args[0].ToLower() == "-mycommand")
{
DoMyCommand(args[1]);
return;
}
Console.WriteLine("Usage:\n\napplication_name -mycommand
<file>\n");
Console.WriteLine(" Performs mycommand on file.\n");
}


Thanks
Eduardo
 
M

Michael Nemtsev

Hello (e-mail address removed),

You need to use API function AllocConsole
See samlpe of usin it in C# there http://groups.google.com/group/micr..._frm/thread/dc49042db7b51e3b/622ca7a3ebfde2df
I have a GUI application that needs to be invoked from the command
line as well. It should work both as a GUI and as a console
application.

When I invoke it from the command line, it reads all the command line
options perfectly. However, I can't use Console.WriteLine to write
messages to the console (the lines don't appear at the console
window).

Is it possible? What should I do to "connect" the console?

My Main() is:

static void Main(string[] args)
{
if (args.Length == 0)
{
Application.Run(new WorkspaceForm());
return;
}
if (args.Length == 2 && args[0].ToLower() == "-mycommand")
{
DoMyCommand(args[1]);
return;
}
Console.WriteLine("Usage:\n\napplication_name -mycommand
<file>\n");
Console.WriteLine(" Performs mycommand on file.\n");
}
Thanks
Eduardo
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
 
S

sdbillsfan

The code looks fine, it doesn't write the usage statement out if you
pass the incorrect parameters?
 
S

sdbillsfan

Why would he need to use AllocConsole? He wants to write to the console
from which his application was invoked

Michael said:
Hello (e-mail address removed),

You need to use API function AllocConsole
See samlpe of usin it in C# there http://groups.google.com/group/micr..._frm/thread/dc49042db7b51e3b/622ca7a3ebfde2df
I have a GUI application that needs to be invoked from the command
line as well. It should work both as a GUI and as a console
application.

When I invoke it from the command line, it reads all the command line
options perfectly. However, I can't use Console.WriteLine to write
messages to the console (the lines don't appear at the console
window).

Is it possible? What should I do to "connect" the console?

My Main() is:

static void Main(string[] args)
{
if (args.Length == 0)
{
Application.Run(new WorkspaceForm());
return;
}
if (args.Length == 2 && args[0].ToLower() == "-mycommand")
{
DoMyCommand(args[1]);
return;
}
Console.WriteLine("Usage:\n\napplication_name -mycommand
<file>\n");
Console.WriteLine(" Performs mycommand on file.\n");
}
Thanks
Eduardo
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
E

eduardorp1

Yes, I want to write to the console where the user typed the
application name and parameters.

It doesn't show the usage lines.

I've found some more info about this, and nobody seems to have a
solution.

One solution implies changing the aplication to a console application,
and releasing the console if it should be run in GUI mode, which will
flash a black window. Another implies creating two applications with
the same name, but one with .EXE and one with .COM extensions. Both are
very bad...

It's hard to accept that "computers can't do this", isn't it? It's
something so simple.

Maybe it's a question of getting the handlers for STDIN and STDOUT and
passing them to Console.In and Console.Out?...

Eduardo

Why would he need to use AllocConsole? He wants to write to the console
from which his application was invoked

Michael said:
Hello (e-mail address removed),

You need to use API function AllocConsole
See samlpe of usin it in C# there http://groups.google.com/group/micr..._frm/thread/dc49042db7b51e3b/622ca7a3ebfde2df
I have a GUI application that needs to be invoked from the command
line as well. It should work both as a GUI and as a console
application.

When I invoke it from the command line, it reads all the command line
options perfectly. However, I can't use Console.WriteLine to write
messages to the console (the lines don't appear at the console
window).

Is it possible? What should I do to "connect" the console?

My Main() is:

static void Main(string[] args)
{
if (args.Length == 0)
{
Application.Run(new WorkspaceForm());
return;
}
if (args.Length == 2 && args[0].ToLower() == "-mycommand")
{
DoMyCommand(args[1]);
return;
}
Console.WriteLine("Usage:\n\napplication_name -mycommand
<file>\n");
Console.WriteLine(" Performs mycommand on file.\n");
}
Thanks
Eduardo
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top