Execute a Window Application in Command Line

  • Thread starter Thread starter Joey
  • Start date Start date
J

Joey

I want my window application to run also in command line mode.
When the application activated from the command line console, it should
notify the console whether it succeeded or failed
Is it possible to set the output stream of a window application to the
command line (cmd.exe process)?
 
Hi Joey,

AFAIK there is no such a thing as application run from the command line and
run from windows. The application could be written as a console application
in which case Windows automatically creates a console window and attaches it
to the process redirecting stdin, stdout and stderr to this windows. However
if your application is not marked as console application you can do it
yourself. From the application's point of view there is no difference
between those two modes.
 
Joey,

I have done something similar with a windows service. What you need to
do is to have a command line parameter that tells the app that you want to
run as console or window. Then upon inspection of the param '/c' or
something along those lines, you load and execute your console version and
don't show your main form. otherwise, show the form and proceed as normal.

Paul
 
Hi,

Thanks for the reply!
You said:
The application could be written as a console application
in which case Windows automatically creates a console window and attaches it
to the process redirecting stdin, stdout and stderr to this windows. However
if your application is not marked as console application you can do it
yourself.

How can I do it myself?
 
Hi Paul,
Does your console version also writes to the CMD window?
Do you load another exe when you get the param?
 
Hi Ram,

Unfortunately AFIAK .NET doesn't have managed support for creating consoles.
I'm not sure whether it will support it in the next version. To achieve that
you should use PInvoke and calling Windows API. The API for creating and
attaching a console is AllocConsole. Look at MSDN for more info about it.
List of all console functions you can at

offline:

ms-help://MS.MSDNQTR.2003APR.1033/dllproc/base/console_functions.htm

online

http://msdn.microsoft.com/library/d...u/winprog/unicode_layer_console_functions.asp
 
Unfortunately AFIAK .NET doesn't have managed support for creating consoles.

?!?!?!?!?? Have you checked out the "File > New > Project > Visual C#
Projects > Console Application" ??

Yes, there are some drawbacks in terms of mostly keyboard support
(waiting and reading just a single keypress and such), but you can
most definitely write fully functional console apps in C# / .NET.

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Hi Marc,

Have you read the messages in the thread?!?!?!?!
As far as I understand the question is "How to attach a console to a already
running windows forms application". But I might be wrong.
 
Hi Ram if you want to have always that console window runing the easiest way
is to start a WindowsForms application and then go to the project settings
and change the 'Output Type' to 'Console Application'. This way you'll have
windows application the will have console window attached as soon as you
start the application. The other way is to create a console application (as
Marc suggested and add reference to System.Windows.Forms and may one or two
assemblies more) and then in the main method only you have to do is to
create a form and call Application.Run(form). Where 'form' is reference to
the form you just created (the main form of the application). You can also
move the entry point (main method) in the form class and get rid of the
class the wizard created for you
 
Mine actually didn't write to the command window, rather a txt log file and
the system event log, primarily because it was a service. But using the
information in this thread, it is not difficult to start with a console app
and display your form based on args[] passed in.

caveat is that you will have to have a command window open when you run your
window form app.

Paul
 
Indeed Paul that is a caveat.

I have the same sort of problem as the originator of this thread
(joey), so i'll add my bit to it here.

Actually this is a very generic requirement: Create an application that
behaves either as a command line app (so you can e.g schedule it with cmd
line parameters) or run it as an interactive forms application (started by
the user) depending on command line parameters. You do not want 2 programs,
you just want to direct the utput to a command window or a forms window.
There seems two approaches (and I have questions with each);

1) compile as a winexe (forms), but then you do not have a console window
where you can write to. I read in this thread that you cannot create and
attach to a console in managed code ... but is that true???

2) compile as exe (cmd line app). It is no problem to open windows forms in
a cmd line app. However, the console that is being openend at the begin of
execution remains visible. Can I hide this console somehow when running
interactive?

Martin
 
Hi Martin,
1) compile as a winexe (forms), but then you do not have a console window
where you can write to. I read in this thread that you cannot create and
attach to a console in managed code ... but is that true???

AFAIK No. You can attach to a console, but only by using PInvoke.
2) compile as exe (cmd line app). It is no problem to open windows forms in
a cmd line app. However, the console that is being openend at the begin of
execution remains visible. Can I hide this console somehow when running
interactive?

Unfortunately my answer is again No.

What I would suggest is to have a command line switch that gives a hint to
the application haw to start.
e.g. if you start the application like

myapp <enter>

it tuns in GUI mode

if you run it like

myapp -c <enter>

it runs in console mode.
 
Martin,

You don't have to have 2 programs. The difference between 'console' and GUI
application is that the former gets 'console window' attached upon starting.
Beside this there is no difference. And that is the reason why one cannot
find out at run time how the program was started.
The only way around this is to give a hint to the program. One of the
several posible ways is to do that via command line arguments.
 
Hi Guys, I happen to need the similar function so that I can run a
windows application in command line and having the console not returned
until the program finishes. I have gone through you guys postings, and
found no true solution there. Then I have to develop my own solution
according to following logic and it proves to be working fine with me:

Logics for the solution:

1. Console application need a main thread to determine if the
application terminates or not.

2. However, non-console windows application does not have a main thread.
Its simply spawn a child process to Windows system through CWinApp
class. So when you start windows applicaton on command line, it simply
initialize the CWinApp derived class and then handle the instance to
Window system, and at the same time the console is returned.

3. To have a non-console application to run on command line without
return until it finishes, we just simply need to incept the CWinApp
thread and let it to attach to a console application instead of the
default window's system.

With these in mind, I developed a batch mode class that will call the
non-console window's application using Win32 API's CreateProcess() to
call the window's application, so that the window's application will
become a child process of my batch mode caller application, then using
inter-process management funciton: WaitForSingleObject() to force the
child process not to return until it finishes. The batch mode caller
itself has to be a console application of course.

Now you can start the window's application with this batch mode caller,
and the console will not return until the window's application
disappears. This resolves the issue and the window's application does
not require any modification to work with the batch mode caller. Of
course, if you have the source code of the window's application, and
want to make it truly batch mode console application, you could put all
the batch processing functions under InitInstance() and do not let the
Window application even start with the GUI. That way, you turn the
Window Application be able to run in dual modes: One is the batch mode
without gui, and another is the normal window application mode with GUI.

I developed this in .NET 2003 C++ compiler. But it shall applies to
early compilers too.

I have provided enough information in above for any knowledgable
programmer to develop such batch mode caller by him/herself. But if
anyone is really interested in my code, please contact me via email and
it can be purchased from me with a modest fee (That should be paid by
your company anyway). Good lucks!
 
Hong Xia said:
Hi Guys, I happen to need the similar function so that I can run a
windows application in command line and having the console not returned
until the program finishes. I have gone through you guys postings, and
found no true solution there. Then I have to develop my own solution
according to following logic and it proves to be working fine with me:

I'm sure there nothing wrong with your solution, but the framework can
already do this, eg:

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "notepad.exe";
p.Start();
p.WaitForExit();

David
 
Back
Top