Recent

Author Topic: [solved] TProcess with Windows getting Output  (Read 2374 times)

relocate

  • New Member
  • *
  • Posts: 22
[solved] TProcess with Windows getting Output
« on: October 07, 2021, 02:09:05 pm »
Hi,

I'm trying to get the output of an Executeable (for example ipconfig) but it always hungs itself up whenever I use the Pipes Option.

  AProcess := TProcess.Create(nil);
  AProcess.Executable := 'C:\Windows\System32\ipconfig.exe';
  AProcess.Parameters.Add('/all');
  AProcess.Options := AProcess.Options + [poWaitOnExit,poNoConsole,poUsePipes];
  AProcess.Execute;

After that I want to catch the output with an Stringlist, but the program just stops before that, no error.
Without the poUsePipes Option the programs works, but the output is a empty so it throws an exepction (access violation).

What do I miss? (The stringlist is created properly!)

Regards relocate
« Last Edit: October 07, 2021, 08:57:45 pm by relocate »
OS, Lazarus, FPC: Win (no Lazarus, FPC 2.4.4) | CPU-Target: 32Bit

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11455
  • FPC developer.
Re: TProcess with Windows getting Output
« Reply #1 on: October 07, 2021, 02:34:20 pm »
Read the wiki  ( https://wiki.freepascal.org/Executing_External_Programs ), specially the large output case.

In general you the easy way is to use runcommand, that is already a wrapper to simplify straightforward cases. It is also described in the wiki.

relocate

  • New Member
  • *
  • Posts: 22
Re: TProcess with Windows getting Output
« Reply #2 on: October 07, 2021, 02:46:53 pm »
I've read the document, the program just stops after execute (it hungs there), if I use the [poUsePipes] option.
So what else should I do?

May be I give runcommand a try, but look at my foot, I use fpc 2.4.4 (runcommand needs 2.6.2+)
(I know, but actually I do not want to change my setup). And, TProcess has to work, or not.
OS, Lazarus, FPC: Win (no Lazarus, FPC 2.4.4) | CPU-Target: 32Bit

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11455
  • FPC developer.
Re: TProcess with Windows getting Output
« Reply #3 on: October 07, 2021, 03:18:52 pm »
I've read the document, the program just stops after execute (it hungs there), if I use the [poUsePipes] option.
So what else should I do?

You need to have a loop there that reads incrementally. As said simple example is in the large output paragraph, but the runcommand version is more elaborate. (most notably for both stdout and stderr)

And yes, you should upgrade. 2.4.4 is over ten years old. Not upgrading everytime is ok, but ten years is a bit too much.

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: TProcess with Windows getting Output
« Reply #4 on: October 07, 2021, 06:08:48 pm »
hello,
have a look to tProcessEx. TprocessEx includes Real time output management. --> see here

Friendly, J.P
« Last Edit: October 07, 2021, 06:12:11 pm by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Edson

  • Hero Member
  • *****
  • Posts: 1302
Re: TProcess with Windows getting Output
« Reply #5 on: October 07, 2021, 06:56:16 pm »
You can use https://github.com/t-edson/UnTerminal and do:

Code: Pascal  [Select][+][-]
  1. proc.RunInLoop('cmd','/c dir', -1, outText);
  2.  

There you can specify the timeout in seconds (-1 for infinite) and get the output in "outText".

I don't know if it works OK in your compiler version.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: TProcess with Windows getting Output
« Reply #6 on: October 07, 2021, 08:06:28 pm »
Try this (attachment)
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

relocate

  • New Member
  • *
  • Posts: 22
Re: TProcess with Windows getting Output
« Reply #7 on: October 07, 2021, 08:55:35 pm »
@marcov
I rearranged it and it works.

   AProcess            := TProcess.Create(nil);
   AProcess.Executable := 'help';
   AProcess.Parameters.Add('/?');
   AProcess.Options := AProcess.Options + [poWaitOnExit,poUsePipes];
   AProcess.Execute;
   AStringList := TStringlist.create;
   AStringlist.LoadFromStream(AProcess.Output);
   AStringlist.SaveToFile('output.txt');

And now it does what it should. It really was the large output issue.

Yes, I once (back then) I updated to version 2.4.4 and suddenly older but working sourcecode couldn't be compiled anymore.
It took me a while to get it back compiling. So upgrading burned me a bit, and I'm not willingly updating if it is not necassary.

@Jurassic Pork (didn't knew they had pork back in jurassic time).
I really prefer out of the box solutions, sometimes I use external extentions if I see no other possibility.

@Edson (same as above)

@pcurtis
Thank you for your example, I do not use lazarus, but I will break it down to FPC only

@all
Thanks for the support.
« Last Edit: October 07, 2021, 08:58:25 pm by relocate »
OS, Lazarus, FPC: Win (no Lazarus, FPC 2.4.4) | CPU-Target: 32Bit

 

TinyPortal © 2005-2018