PC Review


Reply
Thread Tools Rate Thread

Console and windows together

 
 
Max
Guest
Posts: n/a
 
      22nd Feb 2005
Hi All,

I am developing an application that needs to run in one of two modes:
1. If No command line param is provided the application should run as
Window form.
2. If command line param is provided the application should run as a
console application.

For this i created a windows application with Main

static void Main(string[] cmdLine)
{
Form1 frm = new Form1();
if(cmdLine.Length == 0)
{
frm.InitializeForm();
Application.Run(frm);
}
else
{
frm.ProcessCommandline(cmdLine);
}
}

Problem i have is "Console.writeline" does not work while running the
application as console ie running the application with params.

Pls help

thanks in advance

regards

Max
 
Reply With Quote
 
 
 
 
=?Utf-8?B?SmFrb2IgQ2hyaXN0ZW5zZW4=?=
Guest
Posts: n/a
 
      22nd Feb 2005
You will probably have to redirect the output of the Console using the
Console.SetOut and Console.OpenStandardOutput method (check the docs for this
method).

HTH, Jakob.


"Max" wrote:

> Hi All,
>
> I am developing an application that needs to run in one of two modes:
> 1. If No command line param is provided the application should run as
> Window form.
> 2. If command line param is provided the application should run as a
> console application.
>
> For this i created a windows application with Main
>
> static void Main(string[] cmdLine)
> {
> Form1 frm = new Form1();
> if(cmdLine.Length == 0)
> {
> frm.InitializeForm();
> Application.Run(frm);
> }
> else
> {
> frm.ProcessCommandline(cmdLine);
> }
> }
>
> Problem i have is "Console.writeline" does not work while running the
> application as console ie running the application with params.
>
> Pls help
>
> thanks in advance
>
> regards
>
> Max
>

 
Reply With Quote
 
Brian Gideon
Guest
Posts: n/a
 
      22nd Feb 2005
Max,

[DllImport("kernel32.dll")]
public static extern bool AllocConsole();

[DllImport("kernel32.dll")]
public static extern bool FreeConsole();

public static void Main(string[] args)
{
if (args.Length > 0)
{
AllocConsole();
try
{
// Place console version of code here.
}
finally
{
FreeConsole();
}
}
else
{
// Place windows version of code here.
}
}

There's also an AttachConsole API that you might want to play around
with.

Brian

Max wrote:
> Hi All,
>
> I am developing an application that needs to run in one of two modes:
> 1. If No command line param is provided the application should run as
> Window form.
> 2. If command line param is provided the application should run as a
> console application.
>
> For this i created a windows application with Main
>
> static void Main(string[] cmdLine)
> {
> Form1 frm = new Form1();
> if(cmdLine.Length == 0)
> {
> frm.InitializeForm();
> Application.Run(frm);
> }
> else
> {
> frm.ProcessCommandline(cmdLine);
> }
> }
>
> Problem i have is "Console.writeline" does not work while running the
> application as console ie running the application with params.
>
> Pls help
>
> thanks in advance
>
> regards
>
> Max


 
Reply With Quote
 
Chris Dunaway
Guest
Posts: n/a
 
      22nd Feb 2005
What type of project did you create? Try creating a console
application and then add a reference to the System.Windows.Forms.dll.

You can then use code like this: (vb.Net)

Sub Main(ByVal args() As String)

If args.Length > 0 Then
Console.WriteLine("Started with command line args")
Else
Application.Run(New Form1)
End If

End Sub

Worked ok for me.

 
Reply With Quote
 
Brian Gideon
Guest
Posts: n/a
 
      22nd Feb 2005
Chris,

That works too. The only problem I have with this approach is that a
console window is always created. It may not be a big deal for the OP
though. In that case it is certainly easier than using console APIs.

Brian

Chris Dunaway wrote:
> What type of project did you create? Try creating a console
> application and then add a reference to the System.Windows.Forms.dll.
>
> You can then use code like this: (vb.Net)
>
> Sub Main(ByVal args() As String)
>
> If args.Length > 0 Then
> Console.WriteLine("Started with command line args")
> Else
> Application.Run(New Form1)
> End If
>
> End Sub
>
> Worked ok for me.


 
Reply With Quote
 
Max
Guest
Posts: n/a
 
      23rd Feb 2005
Brain,

I did exactly the same, but while running the application under cmd in
XP,
a new console window fashes. ie. a console window gets created and
exits without displaying any messages.

And i tried what Chris suggested, as you said a console window gets
created when running the app without any arguments.

Any more suggesions.

Max

"Brian Gideon" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Max,
>
> [DllImport("kernel32.dll")]
> public static extern bool AllocConsole();
>
> [DllImport("kernel32.dll")]
> public static extern bool FreeConsole();
>
> public static void Main(string[] args)
> {
> if (args.Length > 0)
> {
> AllocConsole();
> try
> {
> // Place console version of code here.
> }
> finally
> {
> FreeConsole();
> }
> }
> else
> {
> // Place windows version of code here.
> }
> }
>
> There's also an AttachConsole API that you might want to play around
> with.
>
> Brian
>
> Max wrote:
> > Hi All,
> >
> > I am developing an application that needs to run in one of two modes:
> > 1. If No command line param is provided the application should run as
> > Window form.
> > 2. If command line param is provided the application should run as a
> > console application.
> >
> > For this i created a windows application with Main
> >
> > static void Main(string[] cmdLine)
> > {
> > Form1 frm = new Form1();
> > if(cmdLine.Length == 0)
> > {
> > frm.InitializeForm();
> > Application.Run(frm);
> > }
> > else
> > {
> > frm.ProcessCommandline(cmdLine);
> > }
> > }
> >
> > Problem i have is "Console.writeline" does not work while running the
> > application as console ie running the application with params.
> >
> > Pls help
> >
> > thanks in advance
> >
> > regards
> >
> > Max

 
Reply With Quote
 
Brian Gideon
Guest
Posts: n/a
 
      23rd Feb 2005
Max,

Make sure the project output type is "Windows Application". If you run
the application in the debugger it will create a new console window,
but the standard output is redirected to the output window in the IDE
and not the new console window. Look there for the output. If you run
the application from cmd it will create a new console and send the
standard output there. Be sure to put a Console.ReadLine call at the
end so that you have time to read the output. If you want the standard
output to go the command prompt directly you may have to use the
AttachConsole API. I haven't experimented around with that yet.

Brian

Max wrote:
> Brain,
>
> I did exactly the same, but while running the application under
> cmd in XP, a new console window fashes. ie. a console window gets
> created and exits without displaying any messages.
>
> And i tried what Chris suggested, as you said a console window
> gets created when running the app without any arguments.
>
> Any more suggesions.
>
> Max
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
windows FAX console cafetero Windows XP General 1 24th Oct 2006 08:43 PM
How to change _buffer_ size of console window (or can runas inherit console props)? Alex Blekhman Microsoft Windows 2000 CMD Promt 4 18th Mar 2005 09:07 AM
How do I add a console to a windows app? Microsoft VB .NET 2 21st Oct 2004 09:31 PM
Windows XP Fax Console Mrs Lein Windows XP Print / Fax 1 11th Nov 2003 01:45 AM
Windows XP Fax Console Anthony Pica Windows XP General 0 21st Oct 2003 04:08 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:53 AM.