Recent

Author Topic: TProcess Modifying Input Stream  (Read 1797 times)

bobby6478

  • New Member
  • *
  • Posts: 19
TProcess Modifying Input Stream
« on: January 22, 2020, 06:14:28 pm »
I am running a TProcess which is a process that normally executes in the terminal if I was running it not using Pascal. So far, I have been able to read the output by using

Code: Pascal  [Select][+][-]
  1. a := TStringList.create;
  2. a.loadFromStream(myProcess.output)

Now, I would like to modify the input; that is to say, I'd like to provide some input to the process that does the same thing as writing to the terminal would do.

Code: Pascal  [Select][+][-]
  1. myProcess.input.writeansistring('some string')

does not work, and to even try doing that took me a while to figure out. If anyone has any idea how I could provide commands to my TProcess, it would be greatly appreciated.





dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: TProcess Modifying Input Stream
« Reply #1 on: January 22, 2020, 07:00:39 pm »
I am running a TProcess which is a process that normally executes in the terminal if I was running it not using Pascal. So far, I have been able to read the output by using

Code: Pascal  [Select][+][-]
  1. a := TStringList.create;
  2. a.loadFromStream(myProcess.output)

Now, I would like to modify the input; that is to say, I'd like to provide some input to the process that does the same thing as writing to the terminal would do.

Code: Pascal  [Select][+][-]
  1. myProcess.input.writeansistring('some string')

does not work, and to even try doing that took me a while to figure out. If anyone has any idea how I could provide commands to my TProcess, it would be greatly appreciated.

Try the docs:

https://www.freepascal.org/docs-html/current/fcl/process/tprocess.parameters.html
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

bobby6478

  • New Member
  • *
  • Posts: 19
Re: TProcess Modifying Input Stream
« Reply #2 on: January 22, 2020, 07:08:25 pm »
Try the docs:

https://www.freepascal.org/docs-html/current/fcl/process/tprocess.parameters.html

Unfortunately, I've been through the entire docs, and I believe such a procedure or ability exists in TProcess but at least to me it is unintuitive from reading the docs. The TProcess parameters add procedure is for adding modifiers to the executable. So you might run some executable with flags, and that is what parameters add. I am looking for adding input AFTER execution has already started.

dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: TProcess Modifying Input Stream
« Reply #3 on: January 22, 2020, 07:09:58 pm »
Try the docs:

https://www.freepascal.org/docs-html/current/fcl/process/tprocess.parameters.html

Unfortunately, I've been through the entire docs, and I believe such a procedure or ability exists in TProcess but at least to me it is unintuitive from reading the docs. The TProcess parameters add procedure is for adding modifiers to the executable. So you might run some executable with flags, and that is what parameters add. I am looking for adding input AFTER execution has already started.

TProcess is not an interactive console.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: TProcess Modifying Input Stream
« Reply #4 on: January 22, 2020, 07:32:18 pm »
The process that you execute as well as your executing process must be prepared for this.

It is possible, Lazarus communicates with the debugger this way.

But instead of trying to hack it into your existing app, is to start over with  small console programs. One for the calling function and one for the callee.

Best is to look at the skeleton for runcommand() which is based on the "large output" example in the docs. Never read anything without checking the number of available, and never read more than available.


lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: TProcess Modifying Input Stream
« Reply #5 on: January 22, 2020, 07:46:37 pm »
Code: Pascal  [Select][+][-]
  1. myProcess.input.writeansistring('some string')

does not work, and to even try doing that took me a while to figure out. If anyone has any idea how I could provide commands to my TProcess, it would be greatly appreciated.

The problem, probably, is that writeansistring writes four bytes with the length before writing the characters making up the string, which may confuse the external program. You should write just the characters in the string with something like:
Code: Pascal  [Select][+][-]
  1. myProcess.Input.Write(SomeString[1], Length(SomeString))

HTH
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: TProcess Modifying Input Stream
« Reply #6 on: January 23, 2020, 03:58:45 pm »
Explain "does not work" (why do people keep using this vague expression without details? we're not oracles, some of us even hate Oracle).

Depending on what your program input mechanism is, it might be waiting for a terminal character, usually a #10 or #13. I mostly just give LineEnding in the hope that the program will truncate the extra carriage return and line feed anyway.

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: TProcess Modifying Input Stream
« Reply #7 on: January 23, 2020, 04:23:17 pm »
(why do people keep using this vague expression without details? we're not oracles, some of us even hate Oracle).
Because they think they are in Delphi. ;)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: TProcess Modifying Input Stream
« Reply #8 on: January 23, 2020, 04:30:01 pm »
(why do people keep using this vague expression without details? we're not oracles, some of us even hate Oracle).
Because they think they are in Delphi. ;)
Trying to date Pythia? Or her son Python? Sounds familiar... ;D
« Last Edit: January 23, 2020, 04:32:09 pm by Thaddy »
Specialize a type, not a var.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: TProcess Modifying Input Stream
« Reply #9 on: January 23, 2020, 04:37:57 pm »
In the days of the Pytia/Pythia then Delphi was the center of the World. Symbolized through the Omphalos.

 

TinyPortal © 2005-2018