process exeuction with interactive mode lazarus / 21-General ---------------------------------------------------------------------- fpc2pas · 2021-06-13 16:08 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function exec_hold(command:string):string;var  StartInfo: TStartupInfo;  ProcInfo: TProcessInformation;  CreateOk: boolean;  input: String;  path : String;  cmd : String;begin  { fill with known state }  FillChar(StartInfo, SizeOf(TStartupInfo), #0);  FillChar(ProcInfo, SizeOf(TProcessInformation), #0);  StartInfo.cb := SizeOf(TStartupInfo);   cmd := 'c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe';   input := '-command ' + command;  CreateOk := CreateProcess(PChar(cmd), PChar(input), nil, nil, false, CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS, nil,     nil, StartInfo, ProcInfo);  { check to see if successful }  if CreateOk then     while WaitForSingleObject(ProcInfo.hProcess, INFINITE) = WAIT_TIMEOUT do    begin    // may or may not be needed. Usually wait for child processes   // WaitForMultipleObjects(ProcInfo.hProcess, INFINITE);  sleep(50);     end;end; ---------------------------------------------------------------------- Edson · 2021-06-13 20:48 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. ---------------------------------------------------------------------- fpc2pas · 2021-06-14 12:16 Quote from: Edson on June 13, 2021, 10:48:00 pmI 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 · 2021-06-14 15:49 Quote from: fpc2pas on June 14, 2021, 02:16:19 pmthanks 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? ---------------------------------------------------------------------- engkin · 2021-06-14 16:26 Are you developing a console app, or are you controlling a console app, or maybe both? ---------------------------------------------------------------------- fpc2pas · 2021-06-15 08:29 Quote from: engkin on June 14, 2021, 06:26:29 pmAre 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 · 2021-06-15 14:42 Using UnTerminal you can interact with "Powershell" or any other command line process. Check the Example1 on the GitHub. ---------------------------------------------------------------------- fpc2pas · 2021-06-15 14:48 Quote from: Edson on June 15, 2021, 04:42:31 pmUsing 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 · 2021-06-15 15:44 Quote from: fpc2pas on June 15, 2021, 04:48:00 pmthanks 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1;uses Interfaces, UnTerminal, SysUtils;type  TProc=class    proc: TConsoleProc;    procedure procLineCompleted(const lin: string);    constructor Create;    destructor Destroy; override;  end;constructor TProc.Create;begin  proc := TConsoleProc.Create(nil);  proc.LineDelimSend := LDS_CRLF;  proc.OnLineCompleted:=@procLineCompleted;end; destructor TProc.Destroy;begin  proc.Destroy;end; procedure TProc.procLineCompleted(const lin: string);begin  //Line received  writeln('--'+lin);  //To respond, do something like: proc.SendLn('dir');end; var  p: TProc;  i: Integer;begin  writeln('Executing process');  p := TProc.Create;  p.proc.Open('cmd','');  p.proc.Loop(3);  //Execute process until finished or pass 3 seconds  p.proc.Close;  writeln('Finished');  p.Destroy;end.  This a very basic sample test. Just for test. No interaction, no processing no response. Just opening a CMD command. ---------------------------------------------------------------------- fpc2pas · 2021-06-15 16:11 Quote from: Edson on June 15, 2021, 05:44:03 pmQuote from: fpc2pas on June 15, 2021, 04:48:00 pmthanks 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1;uses Interfaces, UnTerminal, SysUtils;type  TProc=class    proc: TConsoleProc;    procedure procLineCompleted(const lin: string);    constructor Create;    destructor Destroy; override;  end;constructor TProc.Create;begin  proc := TConsoleProc.Create(nil);  proc.LineDelimSend := LDS_CRLF;  proc.OnLineCompleted:=@procLineCompleted;end; destructor TProc.Destroy;begin  proc.Destroy;end; procedure TProc.procLineCompleted(const lin: string);begin  //Line received  writeln('--'+lin);  //To respond, do something like: proc.SendLn('dir');end; var  p: TProc;  i: Integer;begin  writeln('Executing process');  p := TProc.Create;  p.proc.Open('cmd','');  p.proc.Loop(3);  //Execute process until finished or pass 3 seconds  p.proc.Close;  writeln('Finished');  p.Destroy;end.  This a very basic sample test. Just for test. No interaction, no processing no response. Just opening a CMD command. nice thank you ---------------------------------------------------------------------- fpc2pas · 2021-07-27 15:49 Quote from: Edson on June 15, 2021, 05:44:03 pmQuote from: fpc2pas on June 15, 2021, 04:48:00 pmthanks 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1;uses Interfaces, UnTerminal, SysUtils;type  TProc=class    proc: TConsoleProc;    procedure procLineCompleted(const lin: string);    constructor Create;    destructor Destroy; override;  end;constructor TProc.Create;begin  proc := TConsoleProc.Create(nil);  proc.LineDelimSend := LDS_CRLF;  proc.OnLineCompleted:=@procLineCompleted;end; destructor TProc.Destroy;begin  proc.Destroy;end; procedure TProc.procLineCompleted(const lin: string);begin  //Line received  writeln('--'+lin);  //To respond, do something like: proc.SendLn('dir');end; var  p: TProc;  i: Integer;begin  writeln('Executing process');  p := TProc.Create;  p.proc.Open('cmd','');  p.proc.Loop(3);  //Execute process until finished or pass 3 seconds  p.proc.Close;  writeln('Finished');  p.Destroy;end.  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 · 2021-07-27 17:22 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. ---------------------------------------------------------------------- fpc2pas · 2021-07-28 02:14 Quote from: Edson on July 27, 2021, 07:22:30 pmDo 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 · 2021-07-28 03:33 i did some debugging, and the problem is on poUsePipes as it failed to pip powershell on affected systems. ---------------------------------------------------------------------- Edson · 2021-07-28 04:27 In Windows 10 I can run Powershell without problems. ---------------------------------------------------------------------- fpc2pas · 2021-07-28 04:31 did you try that on windows 7 ? as I mentioned it is not working in some builds of windows 10 ---------------------------------------------------------------------- fpc2pas · 2021-07-28 04:35 Quote from: Edson on July 28, 2021, 06:27:04 amIn Windows 10 I can run Powershell without problems. it also not working with this build number Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Major  Minor  Build  Revision-----  -----  -----  --------10     0      10240  0 ---------------------------------------------------------------------- badmintonfan · 2023-05-23 07:27 Quote from: Edson on July 28, 2021, 06:27:04 amIn Windows 10 I can run Powershell without problems. The command Set-ExecutionPolicy Unrestricted executed,but there is no any output ---------------------------------------------------------------------- Edson · 2023-05-30 15:33 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 . ---------------------------------------------------------------------- badmintonfan · 2023-12-25 07:05 Quote from: Edson on May 30, 2023, 05:33:56 pmI 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 ---------------------------------------------------------------------- Edson · 2023-12-29 18:54 Quote from: badmintonfan on December 25, 2023, 08:05:48 amThe memo can not display Chinese character properly Hi. I think this is related to this issue: https://github.com/t-edson/UnTerminal/issues/6 ---------------------------------------------------------------------- badmintonfan · 2024-01-02 14:29 Quote from: Edson on December 29, 2023, 07:54:29 pmHi. I think this is related to this issue: https://github.com/t-edson/UnTerminal/issues/6 Yes,it is. Thank you!