Recent

Author Topic: SOLVED - Gaining Output from running a process  (Read 501 times)

zxandris

  • Jr. Member
  • **
  • Posts: 92
SOLVED - Gaining Output from running a process
« on: March 08, 2024, 08:00:03 pm »
I'm using the following code and trying to gain the output from what I'm running, but it just hangs if I run it with the option to pipe the output, has anyone got any idea what I'm doing wrong please?

Code: Pascal  [Select][+][-]
  1. function RunAndPAuse2(EXEFile, cmdline: string; const show : TShowWindowOptions; const pipe : Boolean) : String;
  2. var
  3.   AProcess: TProcess;
  4.   aStringList : TStringList;
  5. begin
  6.   // Now we will create the TProcess object, and
  7.   // assign it to the var AProcess.
  8.   AProcess := TProcess.Create(application);
  9.   try
  10.        aProcess.Executable:= exeFile;
  11.        AProcess.Parameters.text := CmdLine;
  12.  
  13.        // We will define an option for when the program
  14.        // is run. This option will make sure that our program
  15.        // does not continue until the program we will launch
  16.        // has stopped running.
  17.  
  18.        AProcess.Options := [];
  19.        if pipe then  AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes]
  20.        else AProcess.Options := AProcess.Options + [poWaitOnExit];
  21.  
  22.        AProcess.ShowWindow := Show;
  23.  
  24.        // Now that AProcess knows what the commandline is
  25.        // we will run it.
  26.        AProcess.Execute;
  27.  
  28.        if Pipe then
  29.        begin
  30.             // Now read the output of the program we just ran into a TStringList.
  31.             AStringList := TStringList.Create;
  32.             try
  33.                AStringList.LoadFromStream(AProcess.Output);
  34.                // Save the output to a file and clean up the TStringList.
  35.                // debugLog(AStringList.text);
  36.                result := AStringList.text;
  37.             finally
  38.                    AStringList.Free;
  39.             end;
  40.       end else Result := '';
  41.   finally
  42.          AProcess.Free;
  43.   end;
  44. end;
  45.  
« Last Edit: March 09, 2024, 05:14:21 am by zxandris »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: Gaining Output from running a process
« Reply #1 on: March 08, 2024, 08:02:25 pm »

Thaddy

  • Hero Member
  • *****
  • Posts: 14158
  • Probably until I exterminate Putin.
Re: Gaining Output from running a process
« Reply #2 on: March 08, 2024, 08:44:51 pm »
What you are doing wrong is just not reading the documentation. (not the wiki, but the documentation)
The solution Marco mentioned is better and has been there for some time.
Re-inventing wheels as a hexagon is never a good idea. It is bumpy.
« Last Edit: March 08, 2024, 08:46:58 pm by Thaddy »
Specialize a type, not a var.

zxandris

  • Jr. Member
  • **
  • Posts: 92
Re: Gaining Output from running a process
« Reply #3 on: March 08, 2024, 09:21:12 pm »
That would be great but according to the compiler the runcommand is depreciated, so I was hoping to come up with another solution...

TRon

  • Hero Member
  • *****
  • Posts: 2398
Re: Gaining Output from running a process
« Reply #4 on: March 08, 2024, 09:28:59 pm »
That would be great but according to the compiler the runcommand is depreciated, so I was hoping to come up with another solution...
*sight* (this has been asked and answered a couple of times before you asked  ;D )

Try to read the article in the link carefully... and note
Quote
The version using CmdLine attempts to split the command line in a binary and separate command-line arguments. This version of the function is deprecated.
Meaning, that that particular version of the function, and only that particular version of the function is deprecated. So, the other one is not deprecated :)

Do the help files support bold, underlined, choosing a bigger and colored font ? *hint*

Thaddy

  • Hero Member
  • *****
  • Posts: 14158
  • Probably until I exterminate Putin.
Re: Gaining Output from running a process
« Reply #5 on: March 08, 2024, 09:35:45 pm »
Or  >:( >:D :D
Specialize a type, not a var.

bytebites

  • Hero Member
  • *****
  • Posts: 624
Re: Gaining Output from running a process
« Reply #6 on: March 08, 2024, 09:50:15 pm »
Code: Pascal  [Select][+][-]
  1. function RunCommand(
  2.  
  3.   const cmdline: TProcessString;
  4.  
  5.   out outputstring: string
  6.  
  7. ):Boolean; deprecated;

TRon

  • Hero Member
  • *****
  • Posts: 2398
Re: Gaining Output from running a process
« Reply #7 on: March 08, 2024, 09:50:24 pm »
@Thaddy
:D

I have a notion that it might be more frustrating for marcov to read such (wrong) assumptions because he spend all that time perfecting the function.

zxandris

  • Jr. Member
  • **
  • Posts: 92
SOLVED - Re: Gaining Output from running a process
« Reply #8 on: March 09, 2024, 05:14:00 am »
I'll put my hands up here, I should've looked more closely into that.  That's my bad, sorry.  I saw the message from the compiler and sort of paniced, lol.

I've looked into that properly now and have solved my issue.

Thanks guys,

CJ

zxandris

  • Jr. Member
  • **
  • Posts: 92
Re: SOLVED - Gaining Output from running a process
« Reply #9 on: March 09, 2024, 05:26:16 am »
I just realised for those that come next, there was no actual solve posted.  So, in the interests of that here's what I used in the end from the advice given :-

Code: Pascal  [Select][+][-]
  1. RunCommand(cmdLine, cline, output, [poWaitOnExit, poNoConsole, poUsePipes], swoHide);
  2.  

That ran my external exe, hidden and got the output I wanted, where cmdLine was the exe and cline was an array of TProcessString.  I had to convert from a string list as that's how I had that before hand.

For anyone with the same problem, I hope this helps.

 

TinyPortal © 2005-2018