Lazarus

Programming => General => Topic started by: fpc2pas on June 13, 2021, 06:08:07 pm

Title: process exeuction with interactive mode
Post by: fpc2pas 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;  
Title: Re: process exeuction with interactive mode
Post by: Edson 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.
Title: Re: process exeuction with interactive mode
Post by: fpc2pas 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?
Title: Re: process exeuction with interactive mode
Post by: Edson 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?

Title: Re: process exeuction with interactive mode
Post by: engkin on June 14, 2021, 06:26:29 pm
Are you developing a console app, or are you controlling a console app, or maybe both?
Title: Re: process exeuction with interactive mode
Post by: fpc2pas 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
Title: Re: process exeuction with interactive mode
Post by: Edson 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.

Title: Re: process exeuction with interactive mode
Post by: fpc2pas 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?
Title: Re: process exeuction with interactive mode
Post by: Edson 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.
Title: Re: process exeuction with interactive mode
Post by: fpc2pas 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
Title: Re: process exeuction with interactive mode
Post by: fpc2pas 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
Title: Re: process exeuction with interactive mode
Post by: Edson 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.
Title: Re: process exeuction with interactive mode
Post by: fpc2pas 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
Title: Re: process exeuction with interactive mode
Post by: fpc2pas 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. 
Title: Re: process exeuction with interactive mode
Post by: Edson on July 28, 2021, 06:27:04 am
In Windows 10 I can run Powershell without problems.
Title: Re: process exeuction with interactive mode
Post by: fpc2pas on July 28, 2021, 06:31:28 am
did you try that on windows 7 ? as I mentioned it is not working in some builds of windows 10
Title: Re: process exeuction with interactive mode
Post by: fpc2pas on July 28, 2021, 06:35:11 am
In Windows 10 I can run Powershell without problems.

it also not working with this build number

Code: Pascal  [Select][+][-]
  1. Major  Minor  Build  Revision
  2. -----  -----  -----  --------
  3. 10     0      10240  0
Title: Re: process exeuction with interactive mode
Post by: badmintonfan on May 23, 2023, 09:27:20 am
In Windows 10 I can run Powershell without problems.
The command Set-ExecutionPolicy Unrestricted executed,but there is no any output
Title: Re: process exeuction with interactive mode
Post by: Edson on May 30, 2023, 05:33:56 pm
I can run the PowerShell too, without problem and send commands. Some commands need to start as Administrator. I'm running on Windows 10.0.19044.  Unfortunately, I don't have a Windows 7 or some other version of Windows 10 to test .
Title: Re: process exeuction with interactive mode
Post by: badmintonfan on December 25, 2023, 08:05:48 am
I can run the PowerShell too, without problem and send commands. Some commands need to start as Administrator. I'm running on Windows 10.0.19044.  Unfortunately, I don't have a Windows 7 or some other version of Windows 10 to test .
The memo can not display Chinese character properly
Title: Re: process exeuction with interactive mode
Post by: Edson on December 29, 2023, 07:54:29 pm
The memo can not display Chinese character properly

Hi. I think this is related to this issue: https://github.com/t-edson/UnTerminal/issues/6
Title: Re: process exeuction with interactive mode
Post by: badmintonfan on January 02, 2024, 03:29:29 pm
Hi. I think this is related to this issue: https://github.com/t-edson/UnTerminal/issues/6
Yes,it is.
Thank you!
TinyPortal © 2005-2018