473,396 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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");

}
Nov 15 '05 #1
5 4028
"Mullin Yu" <mu*******@ctil.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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
Nov 15 '05 #2
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\Applica tion\OutboundConsole>outbo
undconsole /?
Syntax: OutboundConsole.exe /[?|h] /[p|e|f] /[start|close]
C:\Projects\ccbs\edms\p2\ApplicationServer\Applica tion\OutboundConsole>

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

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

thanks!

"Michael Sparks" <mi************@remove.this.sbcglobal.net> wrote in message
news:Pa*******************@newssvr24.news.prodigy. com...
"Mullin Yu" <mu*******@ctil.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

Nov 15 '05 #3
"Mullin Yu" <mu*******@ctil.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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
Nov 15 '05 #4
thanks for your info. i think i can't as the environment is win2k server.

thanks for your kindly advice.

"Michael Sparks" <mi************@remove.this.sbcglobal.net> wrote in message
news:Ci*****************@newssvr23.news.prodigy.co m...
"Mullin Yu" <mu*******@ctil.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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

Nov 15 '05 #5
Mullin,
In addition to Michael's comment, the following articles discusses a method
for having an app that functions as both a console app & a windows app.

http://msdn.microsoft.com/msdnmag/is...A/default.aspx

Hope this helps
Jay

"Mullin Yu" <mu*******@ctil.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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");

}

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: FrodoBaggins | last post by:
Dear Team, I am running Visual Studio 2003 Version 7.1.3088 on Windows Server 2003. I have written a C# application that must write to the event log. When it attempts to write to the event log,...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
2
by: Boba | last post by:
Hi, I'm programming a WinForm application. I would like to enter commands that will send output that will help me to locate bugs in the future. I know that there is a way to send output by...
3
by: Bill Cohagan | last post by:
I'm writing a console app in c# and am encountering a strange problem. I'm trying to use redirection of the standard input stream to read input from a (xml) file. The following code snippet is from...
9
by: ALI-R | last post by:
Hi,, I have two questions : 1) Is it mandatory that config file of a desktop application must be App.config 2) Is it possible to update config file in your code?? thanks for your help. ALI
6
by: Jim Heavey | last post by:
Realizing that I can use Debug.write to write a message to the "output" window, the Console.Write is accepted when keyed in, but where does the output go?
0
by: Seth | last post by:
For some reason my service works fine except that it will create the file in my c drive, but will not write to the file. Sorry if this is a duplicate post, i have found some that ask the same...
4
by: Shil | last post by:
Hi, In VC++.Net 2005 visual studio, if I create a new winform drag and drop a button, then double click it to write click event code, then it auto generates the template code for the event in...
0
by: Evolve | last post by:
Hi, I'd like to write an application which would encrypt a file using GnuPG. It would be a Windows Application which would run a GnuPG as a Console Application. I found this code but it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.