[solved] start external program by source lazarus / 21-General ---------------------------------------------------------------------- Nicole · 2022-11-16 09:20 The link below looks as it would answer my question, but it is overwhelming, I tried them all. My trouble is point 2. All ways start my software fine, but then I seem to fail to pass over the correct parameters. https://wiki.freepascal.org/Executing_External_Programs#SysUtils.Execu teProcess What I want, 1. start a program: c:\myPath\myApp.exe 2. wait until the job of the external software is done 3. work on in my source with the next line of code I tried (pls ignore parameters, they are not correct):    SysUtils.ExecuteProcess();    RunCommand('pdftotext', ['-simple', PDF_FILE], err, [poNoConsole]);    ShellExecute(0, nil, some more..); Shall work with all three, so my question is: Please give me a hint for the correct parameters for point 2 and 3. In my dreams the app will come to front, but I can live with it, if it does not. Windows 7 at the moment, shall work at Windows 10 as well. ---------------------------------------------------------------------- KodeZwerg · 2022-11-16 09:39 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 const  SEE_MASK_NOCLOSEPROCESS = $00000040;  SEE_MASK_FLAG_DDEWAIT   = $00000100; type  PShellExecuteInfoA = ^TShellExecuteInfoA;  PShellExecuteInfo = PShellExecuteInfoA;  _SHELLEXECUTEINFOA = record  cbSize: DWORD;    fMask: ULONG;    Wnd: HWND;    lpVerb: PAnsiChar;  lpFile: PAnsiChar;    lpParameters: PAnsiChar;    lpDirectory: PAnsiChar;    nShow: Integer;    hInstApp: HINST;    { Optional fields }    lpIDList: Pointer;    lpClass: PAnsiChar;    hkeyClass: HKEY;    dwHotKey: DWORD;    hIcon: THandle;    hProcess: THandle;  end;  TShellExecuteInfoA = _SHELLEXECUTEINFOA;  TShellExecuteInfo = TShellExecuteInfoA; function ShellExecuteEx(lpExecInfo: PShellExecuteInfo):BOOL; stdcall; external 'shell32.dll' name 'ShellExecuteExA'; function ProcessMessages : Boolean;const WM_QUIT  = $0012;var Msg : TMsg;begin Result := False; while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do begin  if Msg.Message = WM_QUIT then  begin Exit end else begin   TranslateMessage(Msg);   DispatchMessage(Msg);  end; end; Result := True;end; function ExecuteShell(const Executable, Commands: String; const ShowConsole, DoWait: Boolean): Cardinal; var  ProcessInfo: TShellExecuteInfo; begin  Result := Cardinal($FFFFFFFF);  FillChar(ProcessInfo, SizeOf(TShellExecuteInfo), #0);  with ProcessInfo do  begin   cbSize:= SizeOf( TShellExecuteInfo );   fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_FLAG_DDEWAIT;   Wnd := GetActiveWindow();   lpVerb := 'open';   lpFile:= PChar(Executable);   lpParameters := PChar(Commands);   lpDirectory := PChar(ExtractFilePath(Executable));   if ShowConsole then nShow := SW_SHOWNORMAL else nShow := SW_HIDE;  end;  if ShellExecuteEx(@ProcessInfo) then  begin   if DoWait then while WaitForSingleObject(ProcessInfo.hProcess, 500) <> WAIT_OBJECT_0 do ProcessMessages;   if not GetExitCodeProcess(ProcessInfo.hProcess, Result) then Result := Cardinal($FFFFFFFF);   CloseHandle(ProcessInfo.hProcess);  end  else  begin   Result := GetLastError;   exit;  end; end;used like: 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 procedure TForm1.Button1Click(Sender: TObject);begin Memo1.Lines.Add('Started');  ExecuteShell(ParamStr(0),'', True, True); Memo1.Lines.Add('Ended');end;Enjoy. (Windows only) ---------------------------------------------------------------------- Thaddy · 2022-11-16 09:39 You should use excecuteprocess with [inheritedhandles] option. That should be enough. Otherwise use TProcess or TAsyncProcess. See: https://www.freepascal.org/docs- html/rtl/sysutils/executeprocess.html ---------------------------------------------------------------------- Thaddy · 2022-11-16 09:42 @KodeZwerk That is Windows only and not recommended unless OP only wants to compile for Windows. Usually it is better to present a cross-platform option, certaily on this forum, where distribution between Windows and others is more than 50/50 on the side of other OS's. Plz take care of that. It also helps to ensure the FPC mantrum is kept in honor: write once, compile anywhere. In this case  a X-platform option does not even come with any penalty. ---------------------------------------------------------------------- KodeZwerg · 2022-11-16 09:51 Quote from: Thaddy on November 16, 2022, 10:42:25 am@KodeZwerk That is Windows only and not recommended unless OP only wants to compile for Windows. Quote from: Nicole on November 16, 2022, 10:20:49 am1. In my dreams the app will come to front, but I can live with it, if it does not. 2. Windows 7 at the moment, shall work at Windows 10 as well. 1. Yes. 2. Yes. @thaddy, what have i not respected? ---------------------------------------------------------------------- Thaddy · 2022-11-16 09:52 Quote from: KodeZwerg on November 16, 2022, 10:51:16 am@thaddy, what have i not respected? Present a cross platform solution. ---------------------------------------------------------------------- KodeZwerg · 2022-11-16 09:53 Quote from: Thaddy on November 16, 2022, 10:42:25 am@KodeZwerkMy nickname ends with a "g" not "k" ---------------------------------------------------------------------- KodeZwerg · 2022-11-16 09:57 Quote from: Thaddy on November 16, 2022, 10:52:24 amQuote from: KodeZwerg on November 16, 2022, 10:51:16 am@thaddy, what have i not respected? Present a cross platform solution. You can do that way better than me, I am a beginner at programming. OP asked for windows 7/10 and I served a solution that works flawless with windows 7/10. Nothing more to expect from me. Have a nice day. ---------------------------------------------------------------------- Nicole · 2022-11-16 10:12 Thank you for the answers, and I appreciate the interesting discussion about the way to do. I will keep the code because I think over to switch to Linux on a rainy day in the future. For this purpose, an own directory waits for this rainy day, which shall allow this. In the meanwhile, I want a quick and dirty solution to have the software run. The link looks as it would answer the question for the "waiting", - unfortunately it does not. The flag link leads in a circle back to the original source. This opens my software fine, but does not wait for it to finish. Can you kindly provide, what to fill in? SysUtils.ExecuteProcess('C:\xxxxx.exe', '', []); ---------------------------------------------------------------------- Zvoni · 2022-11-16 10:25 If i understood nicole correctly: She wants to call "pdftotext" passing arguments to it (layout, PDF-Filename etc.), and her program shall wait (!) until pdftotext is done. Have a closer look  here: https://wiki.freepascal.org/Executing_External_Programs#TProcess In "A simple Example"-Source-Code 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 // We will define an option for when the program  // is run. This option will make sure that our program  // does not continue until the program we will launch  // has stopped running.                vvvvvvvvvvvvvv  AProcess.Options := AProcess.Options + [poWaitOnExit];  //THIS ONE ---------------------------------------------------------------------- Dzandaa · 2022-11-16 10:40 Hi, Have a look at https://github.com/t-edson/UnTerminal I use it in Windows and Linux to execute an external Command line programs with parameters written in C/C++, capture back the output to a memo, can terminate the external program and signal when it is terminated. Include an example that execute a command line written in C++ that send back the arguments (C++ code include). Hope that help. B-> ---------------------------------------------------------------------- Nicole · 2022-11-16 10:56 @Zvoni, thank you, finally this link is for dummies as well! It works now exactly as I want it to. Thank you all others for the answers as well. ---------------------------------------------------------------------- wp · 2022-11-16 11:11 Quote from: Nicole on November 16, 2022, 10:20:49 amWhat I want, 1. start a program: c:\myPath\myApp.exe 2. wait until the job of the external software is done 3. work on in my source with the next line of code Simulated your pdftotext by a good-old prime number calculation program: 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 primes;// Adapted from//   https://www.quora.com/How-do-I-program-to- find-all-the-prime-number-up-to-a-point-in-PASCAL uses  SysUtils; function prime(n: integer): boolean;var  i: integer; max: real;begin  if n = 2 then    prime := true  else if (n <= 1) or (n mod 2 = 0) then    prime := false  else begin    prime := true;    i := 3;    max := sqrt(n);    while i <= max do begin      if n mod i = 0 then begin        prime := false;        exit      end;      i := i + 2    end  endend; { Test and display primes 0 .. ParamStr(1) }var  n: integer;  max: Integer;begin  if ParamCount = 1 then    max := StrToInt(ParamStr(1))  else    max := 100;  for n := 0 to max do    if (prime(n)) then      WriteLn(n, ' ');end. Then I called this from an LCL project by using the RunCommand procedure. The following code is working, and I don't even need the poWaitOnExit argument: 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 procedure TForm1.Button1Click(Sender: TObject);var  s: String;begin  RunCommand('prime.exe', ['2000000'], s, [poNoConsole]);  Memo1.Lines.Text := s;end;For calculation of the prime numbers up to 2 millions as requested by the commandline argument of prime.exe, there is some noticeable delay until the memo is populated. This indicates that the prime number calculation is operating. And when RunCommand is finished, the next program instruction, Memo1.Lines.Text := s, is executed, and the memo is filled with the prime numbers found. So, the program DID wait until RunCommand is finished. The poNoConsole option suppresses the console flashing up. I am not sure whether I understand your request that "the app will come to front".  Which "app"? The console? Well this is hidden by the poNoConsole option. Or your own application? Why should it? Or do you want to see the output of the started program also in the console? In this case you need some kind of "tee.exe" which gets the output of one process and directs it to the console and to RunCommand. I'll have to dig in my old codes to find it... ---------------------------------------------------------------------- KodeZwerg · 2022-11-16 11:14 I successful tested Thaddys and Zvonis version with Windows 10, since I am interested whats happening and willing to learn x-compile. Thaddy just had spelled the "inheritedhandles" wrong. 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 uses  Process, SysUtils, ... procedure TForm1.Button1Click(Sender: TObject);var  AProcess: TProcess;begin  Memo1.Lines.Add('Started');  ExecuteProcess(ParamStr(0), '', [ExecInheritsHandles]); {  AProcess := TProcess.Create(Self);  AProcess.Executable := ParamStr(0);  AProcess.Parameters.Add('');  AProcess.Options := AProcess.Options + [poWaitOnExit];  AProcess.Execute;  AProcess.Free;}  Memo1.Lines.Add('Ended');end;