Recent

Author Topic: process exeuction with interactive mode  (Read 5429 times)

fpc2pas

  • New Member
  • *
  • Posts: 18
process exeuction with interactive mode
« on: June 13, 2021, 06:08:07 pm »
hey folks

i am trying to do something that i need for my project,  to use the code shared below to execute a command and keep waiting until the finish, and that works fine but what I need is to keep that process open and accept multiple commands maybe three, four ..etc

Code: Pascal  [Select][+][-]
  1. function exec_hold(command:string):string;
  2. var
  3.   StartInfo: TStartupInfo;
  4.   ProcInfo: TProcessInformation;
  5.   CreateOk: boolean;
  6.   input: String;
  7.   path : String;
  8.   cmd : String;
  9. begin
  10.   { fill with known state }
  11.   FillChar(StartInfo, SizeOf(TStartupInfo), #0);
  12.   FillChar(ProcInfo, SizeOf(TProcessInformation), #0);
  13.   StartInfo.cb := SizeOf(TStartupInfo);
  14.  
  15.   cmd := 'c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe';
  16.    input := '-command ' + command;
  17.   CreateOk := CreateProcess(PChar(cmd), PChar(input), nil, nil, false, CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS, nil,
  18.      nil, StartInfo, ProcInfo);
  19.   { check to see if successful }
  20.   if CreateOk then
  21.      while WaitForSingleObject(ProcInfo.hProcess, INFINITE) = WAIT_TIMEOUT do
  22.     begin
  23.     // may or may not be needed. Usually wait for child processes
  24.    // WaitForMultipleObjects(ProcInfo.hProcess, INFINITE);
  25.     sleep(50);
  26.  
  27.     end;
  28. end;  
« Last Edit: June 13, 2021, 06:12:47 pm by fpc2pas »

Edson

  • Hero Member
  • *****
  • Posts: 1197
Re: process exeuction with interactive mode
« Reply #1 on: June 13, 2021, 10:48:00 pm »
I recommend to use my library UnTerminal https://github.com/t-edson/UnTerminal.

It's a wrapper for TProccess that include events. Check the sample projects for a simple use.
Lazarus 2.0.10 - FPC 3.2.0 - x86_64-win64 on Windows 8

fpc2pas

  • New Member
  • *
  • Posts: 18
Re: process exeuction with interactive mode
« Reply #2 on: June 14, 2021, 02:16:19 pm »
I recommend to use my library UnTerminal https://github.com/t-edson/UnTerminal.

It's a wrapper for TProccess that include events. Check the sample projects for a simple use.

thanks but not sure that will work with console apps! have you done any demo with the console?

Edson

  • Hero Member
  • *****
  • Posts: 1197
Re: process exeuction with interactive mode
« Reply #3 on: June 14, 2021, 05:49:16 pm »
thanks but not sure that will work with console apps! have you done any demo with the console?

The sample projects, in Windows, use the CMD process to send "dir" command and interact with the shell.

I don't know what console App do you want to control. The Windows Powershell?

Lazarus 2.0.10 - FPC 3.2.0 - x86_64-win64 on Windows 8

engkin

  • Hero Member
  • *****
  • Posts: 2858
Re: process exeuction with interactive mode
« Reply #4 on: June 14, 2021, 06:26:29 pm »
Are you developing a console app, or are you controlling a console app, or maybe both?

fpc2pas

  • New Member
  • *
  • Posts: 18
Re: process exeuction with interactive mode
« Reply #5 on: June 15, 2021, 10:29:19 am »
Are you developing a console app, or are you controlling a console app, or maybe both?

i am developing a console app that interact with PowerShell process

Edson

  • Hero Member
  • *****
  • Posts: 1197
Re: process exeuction with interactive mode
« Reply #6 on: June 15, 2021, 04:42:31 pm »
Using UnTerminal you can interact with "Powershell" or any other command line process. Check the Example1 on the GitHub.

Lazarus 2.0.10 - FPC 3.2.0 - x86_64-win64 on Windows 8

fpc2pas

  • New Member
  • *
  • Posts: 18
Re: process exeuction with interactive mode
« Reply #7 on: June 15, 2021, 04:48:00 pm »
Using UnTerminal you can interact with "Powershell" or any other command line process. Check the Example1 on the GitHub.

thanks but this still GUI, my question can I use it for uterminal with my console app?

Edson

  • Hero Member
  • *****
  • Posts: 1197
Re: process exeuction with interactive mode
« Reply #8 on: June 15, 2021, 05:44:03 pm »
thanks but this still GUI, my question can I use it for uterminal with my console app?

I think you can use UnTerminal in console Apps, just include the LCL packet dependency, use the "Interfaces" unit, and temporize properly the events:

Code: Pascal  [Select][+][-]
  1. program project1;
  2. uses Interfaces, UnTerminal, SysUtils;
  3. type
  4.   TProc=class
  5.     proc: TConsoleProc;
  6.     procedure procLineCompleted(const lin: string);
  7.     constructor Create;
  8.     destructor Destroy; override;
  9.   end;
  10. constructor TProc.Create;
  11. begin
  12.   proc := TConsoleProc.Create(nil);
  13.   proc.LineDelimSend := LDS_CRLF;
  14.   proc.OnLineCompleted:=@procLineCompleted;
  15. end;
  16.  
  17. destructor TProc.Destroy;
  18. begin
  19.   proc.Destroy;
  20. end;
  21.  
  22. procedure TProc.procLineCompleted(const lin: string);
  23. begin
  24.   //Line received
  25.   writeln('--'+lin);
  26.   //To respond, do something like: proc.SendLn('dir');
  27. end;
  28.  
  29. var
  30.   p: TProc;
  31.   i: Integer;
  32. begin
  33.   writeln('Executing process');
  34.   p := TProc.Create;
  35.   p.proc.Open('cmd','');
  36.   p.proc.Loop(3);  //Execute process until finished or pass 3 seconds
  37.   p.proc.Close;
  38.   writeln('Finished');
  39.   p.Destroy;
  40. end.
  41.  

This a very basic sample test. Just for test. No interaction, no processing no response. Just opening a CMD command.
Lazarus 2.0.10 - FPC 3.2.0 - x86_64-win64 on Windows 8

fpc2pas

  • New Member
  • *
  • Posts: 18
Re: process exeuction with interactive mode
« Reply #9 on: June 15, 2021, 06:11:30 pm »
thanks but this still GUI, my question can I use it for uterminal with my console app?

I think you can use UnTerminal in console Apps, just include the LCL packet dependency, use the "Interfaces" unit, and temporize properly the events:

Code: Pascal  [Select][+][-]
  1. program project1;
  2. uses Interfaces, UnTerminal, SysUtils;
  3. type
  4.   TProc=class
  5.     proc: TConsoleProc;
  6.     procedure procLineCompleted(const lin: string);
  7.     constructor Create;
  8.     destructor Destroy; override;
  9.   end;
  10. constructor TProc.Create;
  11. begin
  12.   proc := TConsoleProc.Create(nil);
  13.   proc.LineDelimSend := LDS_CRLF;
  14.   proc.OnLineCompleted:=@procLineCompleted;
  15. end;
  16.  
  17. destructor TProc.Destroy;
  18. begin
  19.   proc.Destroy;
  20. end;
  21.  
  22. procedure TProc.procLineCompleted(const lin: string);
  23. begin
  24.   //Line received
  25.   writeln('--'+lin);
  26.   //To respond, do something like: proc.SendLn('dir');
  27. end;
  28.  
  29. var
  30.   p: TProc;
  31.   i: Integer;
  32. begin
  33.   writeln('Executing process');
  34.   p := TProc.Create;
  35.   p.proc.Open('cmd','');
  36.   p.proc.Loop(3);  //Execute process until finished or pass 3 seconds
  37.   p.proc.Close;
  38.   writeln('Finished');
  39.   p.Destroy;
  40. end.
  41.  

This a very basic sample test. Just for test. No interaction, no processing no response. Just opening a CMD command.

nice thank you

fpc2pas

  • New Member
  • *
  • Posts: 18
Re: process exeuction with interactive mode
« Reply #10 on: July 27, 2021, 05:49:10 pm »
thanks but this still GUI, my question can I use it for uterminal with my console app?

I think you can use UnTerminal in console Apps, just include the LCL packet dependency, use the "Interfaces" unit, and temporize properly the events:

Code: Pascal  [Select][+][-]
  1. program project1;
  2. uses Interfaces, UnTerminal, SysUtils;
  3. type
  4.   TProc=class
  5.     proc: TConsoleProc;
  6.     procedure procLineCompleted(const lin: string);
  7.     constructor Create;
  8.     destructor Destroy; override;
  9.   end;
  10. constructor TProc.Create;
  11. begin
  12.   proc := TConsoleProc.Create(nil);
  13.   proc.LineDelimSend := LDS_CRLF;
  14.   proc.OnLineCompleted:=@procLineCompleted;
  15. end;
  16.  
  17. destructor TProc.Destroy;
  18. begin
  19.   proc.Destroy;
  20. end;
  21.  
  22. procedure TProc.procLineCompleted(const lin: string);
  23. begin
  24.   //Line received
  25.   writeln('--'+lin);
  26.   //To respond, do something like: proc.SendLn('dir');
  27. end;
  28.  
  29. var
  30.   p: TProc;
  31.   i: Integer;
  32. begin
  33.   writeln('Executing process');
  34.   p := TProc.Create;
  35.   p.proc.Open('cmd','');
  36.   p.proc.Loop(3);  //Execute process until finished or pass 3 seconds
  37.   p.proc.Close;
  38.   writeln('Finished');
  39.   p.Destroy;
  40. end.
  41.  

This a very basic sample test. Just for test. No interaction, no processing no response. Just opening a CMD command.


i think it is works very well with cmd process but for PowerShell it is not working in older windows 10 os or windows 7 , windows 2012 or windows server 2016

Edson

  • Hero Member
  • *****
  • Posts: 1197
Re: process exeuction with interactive mode
« Reply #11 on: July 27, 2021, 07:22:30 pm »
Do you have Powershell in Windows 12 or windows server 2016?

I recommend to test using only the class TProcess to control Powershell in Windows 12 or windows server 2016.

If you success, then test using UnTerminal, because it's just a wrapper for TProcess.
Lazarus 2.0.10 - FPC 3.2.0 - x86_64-win64 on Windows 8

fpc2pas

  • New Member
  • *
  • Posts: 18
Re: process exeuction with interactive mode
« Reply #12 on: July 28, 2021, 04:14:46 am »
Do you have Powershell in Windows 12 or windows server 2016?

I recommend to test using only the class TProcess to control Powershell in Windows 12 or windows server 2016.

If you success, then test using UnTerminal, because it's just a wrapper for TProcess.

i have tried to test but you can see the picture below, that's it hangs on, maybe some I/O messed up not sure yet solution for this one

fpc2pas

  • New Member
  • *
  • Posts: 18
Re: process exeuction with interactive mode
« Reply #13 on: July 28, 2021, 05:33:14 am »
i did some debugging, and the problem is on poUsePipes as it failed to pip powershell on affected systems. 

Edson

  • Hero Member
  • *****
  • Posts: 1197
Re: process exeuction with interactive mode
« Reply #14 on: July 28, 2021, 06:27:04 am »
In Windows 10 I can run Powershell without problems.
Lazarus 2.0.10 - FPC 3.2.0 - x86_64-win64 on Windows 8

 

TinyPortal © 2005-2018