how to write string to a dos console of a c# windows application

M

Mullin Yu

i want to build an application of both gui and batch interface by using
windows application project.

i check either passing any args or not. if no, then open the gui
application. if yes, use the batch interface.

my problem is that i can output the string to the dos prompt when running
the exe file at dos prompt:

c:\>test.exe /?

nothing was printed. i expected should print a string on the dos prompt.
should i use the Console object to print the string on the dos prompt?

the following is the codeing:

[STAThread]

static void Main(string[] args)

{

if(args.Length == 0) // start GUI

{

Application.Run(new Form1());

}

else // run batch

{

// Syntax: OutboundConsole.exe /[?|h] /[p|e|f] /[start|close]

// - UPPERCASE of option

// - If the one or more arguments are in wrong format, display syntax screen

Form1 obj = new Form1();

if(args[0].Equals("/?") || args[0].ToUpper().Equals("/H"))

{

obj.displayHelp();

}

}

}

private void displayHelp()

{

Console.WriteLine("Syntax: OutboundConsole.exe /[?|h] /[p|e|f]
/[start|close]");

//MessageBox.Show("test");

}
 
M

Michael Sparks

Mullin Yu said:
nothing was printed. i expected should print a string on the dos prompt.
should i use the Console object to print the string on the dos prompt?

This problem can actually be traced all the way back to the EXE (PE) file
header. Your executable must be identified to the os as running in a
certain "subsystem". You specify the subsystem you want in VS through
Project Settings/Common/General/Output Type.

If you build your project as a "Console Application", the EXE header is
marked as such, and the os will do some work for you. It will open the
three "standard" handles, stdin, stdout, and stderr. The System.Console
class hooks up to those handles that the os opened for you. However, since
you are building a "Windows Application", the os doesn't open those handles
automatically (and it doesn't put a console box on the screen), so
System.Console just redirects to the attached debugger.

All of that said, Win32 wouldn't dare leave us in the dark! There is API
support for creating a console window after-the-fact, and getting the three
standard handles. However, System.Console doesn't even attempt to wrap the
entire Win32 console functionality (annoyingly, that is the case with most
framework classes).

Some brave person made a replacement for System.Console that wraps a much
bigger set of the Win32 console API:
http://www.codeproject.com/csharp/winconsole.asp

It integrates with System.Console, so you don't have to change your code.

HTH
Mike
 
M

Mullin Yu

thanks!

i've looked into it, but not exactly what i want. i want that the message
will be printed at the same console like the following when running at
batch:]

C:\Projects\ccbs\edms\p2\ApplicationServer\Application\OutboundConsole>outbo
undconsole /?
Syntax: OutboundConsole.exe /[?|h] /[p|e|f] /[start|close]
C:\Projects\ccbs\edms\p2\ApplicationServer\Application\OutboundConsole>

if i want to run the GUI, i won't pass any args like:
C:\Projects\ccbs\edms\p2\ApplicationServer\Application\OutboundConsole>outbo
undconsole

but, i really can't print the message at the same console for the windows
application.

thanks!
 
M

Michael Sparks

Mullin Yu said:
i've looked into it, but not exactly what i want. i want that the message
will be printed at the same console like the following when running at
batch:]

Unfortunately, that isn't possible (see below for exception) unless you
compile to the console subsystem.
When cmd.exe launches your process, the os checks if you are in the console
subsystem, and if so, you inherit its (cmd.exe's) standard handles as your
own. Since you are a windows app, I think you will still inherit the
handles, but they won't be connected to your stdin, stdout, and stderr.

To add some insult to injury, there is a new function, AttachConsole, that
does exactly what you need. Problem is, it's only supported on XP and 2003
Server. If you can live with that restriction, you could tweak the
WinConsole code to use that function.

Mike
 
M

Mullin Yu

thanks for your info. i think i can't as the environment is win2k server.

thanks for your kindly advice.

Michael Sparks said:
Mullin Yu said:
i've looked into it, but not exactly what i want. i want that the message
will be printed at the same console like the following when running at
batch:]

Unfortunately, that isn't possible (see below for exception) unless you
compile to the console subsystem.
When cmd.exe launches your process, the os checks if you are in the console
subsystem, and if so, you inherit its (cmd.exe's) standard handles as your
own. Since you are a windows app, I think you will still inherit the
handles, but they won't be connected to your stdin, stdout, and stderr.

To add some insult to injury, there is a new function, AttachConsole, that
does exactly what you need. Problem is, it's only supported on XP and 2003
Server. If you can live with that restriction, you could tweak the
WinConsole code to use that function.

Mike
 

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