Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2011-01

GUI Front-end to Windows console application???

8 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bo Berglund

I need to build a GUI application, which should act as a simple-to-use front-end to a Windows console program.
The console program accepts a number of different commands, which are specified on its command line as parameters separated by spaces. When the console program is started with these parameters it performs its task and then exits.

The command line parameters are rather complex and this is why I would like to build the GUI application in Delphi7 so I can present a comfortable view of it.

So now I need it to do the following:

1) Run the console program with the given parameters.

2) Read back the output of the console program (it will be printed in the console).

3) Detect when the console program asks something and let the user read this in the GUI app and then respond to it by typing in the GUI.

In the past (Using Delphi) and for other reasons I created a few helpers which I called ExecStart and ExecWait. Both of these starts a program, but the Wait variation also waits until the program exits. I used CreateProcess to start the program and WaitForSingleObject to check if it finished OK using its handle.

So with ExecStart I can start a program with command line parameters all right, but how do I pick up the data printed to the console by the program while it runs?

And how do I enter new input that the program may require when it does not finish execution?

Any pointers to how this can be done appreciated.

Bo Berglund Developer in Sweden

Reply by email

Sven Barth
to Bo Berglund
Bo Berglund wrote 37 lines
I need to build a GUI application, which should act as a simple-to-use front-end to a Windows console program. The console program accepts a number of different commands, which are specified on its command line as parameters separated by spaces. When the console program is started with these parameters it performs its task and then exits. The command line parameters are rather complex and this is why I would like to build the GUI application in Delphi7 so I can present a comfortable view of it. So now I need it to do the following: 1) Run the console program with the given parameters. 2) Read back the output of the console program (it will be printed in the console). 3) Detect when the console program asks something and let the user read this in the GUI app and then respond to it by typing in the GUI. In the past (Using Delphi) and for other reasons I created a few helpers which I called ExecStart and ExecWait. Both of these starts a program, but the Wait variation also waits until the program exits. I used CreateProcess to start the program and WaitForSingleObject to check if it finished OK using its handle. So with ExecStart I can start a program with command line parameters all right, but how do I pick up the data printed to the console by the program while it runs? And how do I enter new input that the program may require when it does not finish execution? Any pointers to how this can be done appreciated.

You could use TProcess for this (found in unit "process").

It's documentation is here:
http://www.freepascal.org/docs-html/fcl/process/tprocess.html

And on the Wiki are examples for using it:
http://wiki.lazarus.freepascal.org/Executing_External_Programs

I hope this helps you further (if not feel free to ask, of coures :) ).

Regards,
Sven

Reply by email

Andreas Schneider
to Bo Berglund: I need to build a GUI application, which should act as a simple-to-use front-end to a…
Bo Berglund wrote 35 lines
I need to build a GUI application, which should act as a simple-to-use front-end to a Windows console program. The console program accepts a number of different commands, which are specified on its command line as parameters separated by spaces. When the console program is started with these parameters it performs its task and then exits. The command line parameters are rather complex and this is why I would like to build the GUI application in Delphi7 so I can present a comfortable view of it. So now I need it to do the following: 1) Run the console program with the given parameters. 2) Read back the output of the console program (it will be printed in the console). 3) Detect when the console program asks something and let the user read this in the GUI app and then respond to it by typing in the GUI. In the past (Using Delphi) and for other reasons I created a few helpers which I called ExecStart and ExecWait. Both of these starts a program, but the Wait variation also waits until the program exits. I used CreateProcess to start the program and WaitForSingleObject to check if it finished OK using its handle. So with ExecStart I can start a program with command line parameters all right, but how do I pick up the data printed to the console by the program while it runs? And how do I enter new input that the program may require when it does not finish execution? Any pointers to how this can be done appreciated.

Sure. uses process; --> TProcess does everything you need; from waiting, to piping. Have fun ;-)

Best Regards, Andreas

Reply by email

Bo Berglund
to Sven Barth: You could use TProcess for this (found in unit "process"). It's documentation is here:…
Sven Barth wrote 18 lines
On 13.01.2011 21:47, Bo Berglund wrote: > > Any pointers to how this can be done appreciated. > > You could use TProcess for this (found in unit "process"). It's documentation is here: http://www.freepascal.org/docs-html/fcl/process/tprocess.html And on the Wiki are examples for using it: http://wiki.lazarus.freepascal.org/Executing_External_Programs I hope this helps you further (if not feel free to ask, of coures :) ). Regards, Sven

Thanks Sven!
This is exactly what I was looking for! :-)
Good examples too.

Bo Berglund Developer in Sweden

Reply by email

Bo Berglund
to Bo Berglund
Bo Berglund wrote 26 lines
On Thu, 13 Jan 2011 22:02:32 +0100, Sven Barth wrote: >On 13.01.2011 21:47, Bo Berglund wrote: >> >> Any pointers to how this can be done appreciated. >> >> > >You could use TProcess for this (found in unit "process"). > >It's documentation is here: >http://www.freepascal.org/docs-html/fcl/process/tprocess.html > >And on the Wiki are examples for using it: >http://wiki.lazarus.freepascal.org/Executing_External_Programs > >I hope this helps you further (if not feel free to ask, of coures :) ). > >Regards, >Sven Thanks Sven! This is exactly what I was looking for! :-) Good examples too.

I have used this for my test program and there was an annoying thing that I had to fix:
Whenever I call the Execute method a black console window pops up and since I use pipes there is nothing shown in this window.

To stop this from happening I set the ShowWindow property to swoHIDE.

So far so good, but when I read the help on TProcess it says that this property applies only to Windows. So what happens on Linux? Will the console show up on screen then?
Or is it automatically hidden in Linux?

Can't test myself because I am on Windows XP.

Bo Berglund Developer in Sweden

Reply by email

Sven Barth
to Bo Berglund
Bo Berglund wrote 37 lines
On Thu, 13 Jan 2011 23:02:03 +0100, Bo Berglund wrote: > On Thu, 13 Jan 2011 22:02:32 +0100, Sven Barth > wrote: > >> On 13.01.2011 21:47, Bo Berglund wrote: >>> >>> Any pointers to how this can be done appreciated. >>> >>> >> >> You could use TProcess for this (found in unit "process"). >> >> It's documentation is here: >> http://www.freepascal.org/docs-html/fcl/process/tprocess.html >> >> And on the Wiki are examples for using it: >> http://wiki.lazarus.freepascal.org/Executing_External_Programs >> >> I hope this helps you further (if not feel free to ask, of coures :) ). >> >> Regards, >> Sven > > Thanks Sven! > This is exactly what I was looking for! :-) > Good examples too. > I have used this for my test program and there was an annoying thing that I had to fix: Whenever I call the Execute method a black console window pops up and since I use pipes there is nothing shown in this window. To stop this from happening I set the ShowWindow property to swoHIDE.

Include "poNoConsole" to the Options property of your TProcess instance. This should stop the window from appearing.

Show quoted text 8 lines
So far so good, but when I read the help on TProcess it says that this property applies only to Windows. So what happens on Linux? Will the console show up on screen then? Or is it automatically hidden in Linux? Can't test myself because I am on Windows XP.

Using the property Options from above this should work in a cross platform way.

Regards,
Sven

Reply by email

Andreas Schneider
to Bo Berglund: On Thu, 13 Jan 2011 23:02:03 +0100, Bo Berglund I have used this for my test program and…
Bo Berglund wrote 5 lines
So far so good, but when I read the help on TProcess it says that this property applies only to Windows. So what happens on Linux? Will the console show up on screen then? Or is it automatically hidden in Linux?

There is nothing to hide on *nix. STDOUT is available for all processes and if that gets used by any tty is another matter. Want a console? Run the tool inside a console. Don't want one? Don't run it inside a console.
It's (afaik) only Windows (or PE for that matter) that has this (kinda) stupid flag to distinguish between a console app and a GUI app.

Just as side node: even Microsoft has to work around this design decision for VisualStudio (and maybe other tools) that should behave differently if run from commandline or from GUI. Because in the first case they should only output to the cmd and not show any window and in the latter case they should show a window and not use stdout.

Reply by email

Sven Barth
to Andreas Schneider
Andreas Schneider wrote 9 lines
On Mon, 17 Jan 2011 23:52:58 +0100, Bo Berglund wrote: > So far so good, but when I read the help on TProcess it says that this > property applies only to Windows. So what happens on Linux? > Will the console show up on screen then? > Or is it automatically hidden in Linux? There is nothing to hide on *nix. STDOUT is available for all processes and if that gets used by any tty is another matter. Want a console? Run the tool inside a console. Don't want one? Don't run it inside a console.

TProcess may start a console window application (e.g. XTerm though). This can be suppressed using poNoConsole.

Show quoted text 8 lines
It's (afaik) only Windows (or PE for that matter) that has this (kinda) stupid flag to distinguish between a console app and a GUI app. Just as side node: even Microsoft has to work around this design decision for VisualStudio (and maybe other tools) that should behave differently if run from commandline or from GUI. Because in the first case they should only output to the cmd and not show any window and in the latter case they should show a window and not use stdout.

An application that is compiled as CUI application can use a GUI without problem (I'm sometimes simply adding {$apptype console} to ease debugging). The real difference only appears if you run in a console (e.g. cmd) or in a fullscreen console only environment (on Windows servers): In the first case if you run a GUI compiled application in cmd the shell won't wait till the process terminates while it will wait for one compiled as CUI.
In the second case you'll either get an error message if you start a GUI application or the system will switch to GUI mode (this is implemented in ReactOS for example).

Regards,
Sven

Reply by email