Recent

Author Topic: How can you control a console application from a Lazarus application?  (Read 20226 times)

eny

  • Hero Member
  • *****
  • Posts: 1658
Re: How can you control a console application from a Lazarus application?
« Reply #15 on: January 13, 2012, 10:18:26 pm »
My conclusion is that the problem is with input into Pascal executables.
Not really.
Standard IO gets buffered. It works, but you'll only get to see the flushed end result because of the limited amount of data.
If you want to influence this buffering you'll probably have to create your own file handles for standard input and output and also do the process creation yourself. Not the most trivial task.
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

HappyLarry

  • Full Member
  • ***
  • Posts: 155
Re: How can you control a console application from a Lazarus application?
« Reply #16 on: January 13, 2012, 11:57:05 pm »
I am not sure that I understand.

Are you saying that the 'Hello' string generated by the console app is not enough data to be shown in the controlling application?
Use Lazarus and Free Pascal and stand on the shoulders of giants . . . very generous giants. Thank you.

eny

  • Hero Member
  • *****
  • Posts: 1658
Re: How can you control a console application from a Lazarus application?
« Reply #17 on: January 14, 2012, 09:38:40 pm »
Yes; in case there is a readln, the buffers are not flushed and you will not see the intermediate results.
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

HappyLarry

  • Full Member
  • ***
  • Posts: 155
Re: How can you control a console application from a Lazarus application?
« Reply #18 on: January 14, 2012, 11:08:35 pm »
In Pascal programs, readln is the only way of the user inputting data interactively. Your explanation would mean that the TProcess method of controlling a Pascal console app would never work. It would therefore be impossible to control a Pascal console app with TProcess.

Is that correct?
« Last Edit: January 14, 2012, 11:26:07 pm by HappyLarry »
Use Lazarus and Free Pascal and stand on the shoulders of giants . . . very generous giants. Thank you.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How can you control a console application from a Lazarus application?
« Reply #19 on: January 15, 2012, 12:57:14 am »
Quote
In Pascal programs, readln is the only way of the user inputting data interactively.
(annoying sound) wrong ;)
You can do a lot more ways for interactive input. See keyboard unit.
Quote
Your explanation would mean that the TProcess method of controlling a Pascal console app would never work. It would therefore be impossible to control a Pascal console app with TProcess.

Is that correct?
Depending on what you mean by "control". Surely you could still do it, it's just you can't see the intermediate output, and that's the problem of the program being executed, not the controlling one (and it's not because it's written in Pascal, you can try programs written in other languages whose implementation does input/output buffering). Call Flush(Output) before each ReadLn to force output buffer flush. But this way, you lose speed. Remember that buffering is meant to reduce slow I/O calls.

HappyLarry

  • Full Member
  • ***
  • Posts: 155
Re: How can you control a console application from a Lazarus application?
« Reply #20 on: January 15, 2012, 02:55:47 pm »
@Leledumbo No need to be annoyed. I was directed by ludob to a class that BigChimp had written that makes it easier to control Console applications from a Lazarus program. I tested that class on some simple programs (above) and reported what I found. These results were that it didn't work properly in those cases.

  Eny was suggesting why that might be and I was just making sure I understood what he meant. No one has suggested anywhere previously that the code wouldn't work on certain sorts of console apps. Live and learn eh?!

As a matter of interest, I also tested the class on SQLite3 and a chess engine called Stockfish. It didn't work properly on those either. I have also tested the 'Lazarus recommended' ProcessDemo code on my test programs and that doesn't work properly either - probably because BigChimp's class is based on ProcessDemo. If everything is recorded here it might prevent some people from doing the same. 

Thanks for the suggestion of Flush(Output). I will try putting that into BigChimp's code and seeing if that improves things.
« Last Edit: January 15, 2012, 03:01:08 pm by HappyLarry »
Use Lazarus and Free Pascal and stand on the shoulders of giants . . . very generous giants. Thank you.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: How can you control a console application from a Lazarus application?
« Reply #21 on: January 15, 2012, 03:06:24 pm »
HappyLarry, let me know if you figure it out & I'll gladly update the code  :)

(Also, perhaps Leledumbo meant "the buzzer that sounds in a TV quiz when you give the wrong answer" by "annoyed sound"; just a guess  ;) )

Thanks for seeing this through and documenting what you find. People like you help to improve documentation and examples which will help new people down the line...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

HappyLarry

  • Full Member
  • ***
  • Posts: 155
Re: How can you control a console application from a Lazarus application?
« Reply #22 on: January 15, 2012, 09:26:51 pm »
Quote
Call Flush(Output) before each ReadLn to force output buffer flush. But this way, you lose speed. Remember that buffering is meant to reduce slow I/O calls.
I assume I am to write this code inside my 'test programs' - the console applications Hello0,  Hello1, Hello2 etc. - especially since:

Quote
Surely you could still do it, it's just you can't see the intermediate output, and that's the problem of the program being executed, not the controlling one
Although I can modify them in this case, in general I would not have access to the source code of the console application that I want to control and so this would not be possible. For instance, what if I wanted to control SQLite3 or Stockfish which weren't even written in Pascal?

The original idea was to write a Lazarus application that controlled an already-written console application (perhaps where I only had the executable). In other words, to provide a simple GUI interface to run any console application.

The idea doesn't seem that complicated - just substituting text typed into an edit box for keyboard input and then capturing the resulting console output and displaying it in a memo box. I imagined this would be possible with TProcess, but the buffering issues pointed out by eny and Leledumbo suggest it is not at all easy.

« Last Edit: January 15, 2012, 09:44:31 pm by HappyLarry »
Use Lazarus and Free Pascal and stand on the shoulders of giants . . . very generous giants. Thank you.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How can you control a console application from a Lazarus application?
« Reply #23 on: January 15, 2012, 11:31:36 pm »
Quote
(Also, perhaps Leledumbo meant "the buzzer that sounds in a TV quiz when you give the wrong answer" by "annoyed sound"; just a guess   )
Yep :D
Quote
Although I can modify them in this case, in general I would not have access to the source code of the console application that I want to control and so this would not be possible. For instance, what if I wanted to control SQLite3 or Stockfish which weren't even written in Pascal?
Well, just try on them. I have a major project whose job is to serve as a GUI frontend over a console app, but unfortunately it's non-interactive and even I don't expose the intermediate result.

The problem here is that the controlling program can't see intermediate result from controlled program because no output has been generated from it. And AFAIK it can't be forced by the controlling program.

HappyLarry

  • Full Member
  • ***
  • Posts: 155
Re: How can you control a console application from a Lazarus application?
« Reply #24 on: January 16, 2012, 12:22:34 am »
HappyLarry  :) (that's me) said in reply to eny (half a lifetime ago) ;)
Quote
It would therefore be impossible to control a Pascal console app with TProcess.

Is that correct?

Your last post seems to agree  :o that this is pretty much correct if the console app is interactive and uses I/O buffering (as Pascal console apps do). So for instance, it is very hard to write a Lazarus program to be a GUI front-end to an interactive compiled Pascal program where you could not amend the source code, even if it were very simple like the ones I have used as test examples. As for writing a GUI front-end to programs like SQLite3, where the interface is known but the code can't be easily amended . . .  forget it!  :(

I think the wiki should make this clear.

So in terms of interactive external applications that use I/O buffering (like Pascal), apart from starting them, TProcess is not very useful.  Harsh . . .  :-\  . . . or fair?
« Last Edit: January 16, 2012, 12:36:04 am by HappyLarry »
Use Lazarus and Free Pascal and stand on the shoulders of giants . . . very generous giants. Thank you.

TurboRascal

  • Hero Member
  • *****
  • Posts: 672
  • "Good sysadmin. Bad programmer."™
Re: How can you control a console application from a Lazarus application?
« Reply #25 on: January 23, 2012, 03:07:56 pm »
1. The reason that
AProcess.Input.Write(AInput[1],length(AInput));
didn't work is that the expected input is
'Fred'#13#10
not
'Fred'#13

Concerning this problem, there is a constant defined for each platform, I think it's called "LineEnding" (I'm lazy to look it up now  :-[ ), which you should append to "Fred" (or whatever is expected). That would ensure that the correct line ending is used depending on the OS...
Regards, ArNy the Turbo Rascal
-
"The secret is to give them what they need, not what they want." - Scotty, STTNG:Relics

 

TinyPortal © 2005-2018