Recent

Author Topic: Having trouble with TProcess and RunCommand on Windows  (Read 5338 times)

rail5

  • Newbie
  • Posts: 4
Having trouble with TProcess and RunCommand on Windows
« on: December 24, 2021, 04:28:50 am »
I should say I haven't used Windows for something like ten years, so I'm a bit lost trying to solve this one on my own

The problem I'm having is something like this:

Code: Pascal  [Select][+][-]
  1. RunCommand('liesel', Options, ReturnString)

(where "liesel" is a cross-platform binary on both Windows & Linux, and "Options" is a TStringArray containing elements like '-i', 'some-input-path', '-g', '-v', '-o', 'some-output-path')

Runs just fine on Linux hosts, but fails on Windows with no error message & no execution

Conversely,

Code: Pascal  [Select][+][-]
  1. RunCommand('liesel', ['-h'], ReturnString)

Runs just fine on both systems


Likewise,

Code: Pascal  [Select][+][-]
  1. Process1 := TProcess.Create(nil);
  2. Process1.Executable := 'liesel';
  3.  
  4. for i := 0 to (Length(Options)-1) do
  5. begin
  6.   Process1.Parameters.Add(Options[i]);
  7. end;
  8.  
  9. Process1.Options := [poUsePipes];
  10. Process1.Execute;

Runs just fine on Linux, but doesn't run at all on Windows


Is there something (as I'm hoping there is) incredibly obvious that I'm missing?

Thanks

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1978
  • Former Delphi 1-7, 10.2 user
Re: Having trouble with TProcess and RunCommand on Windows
« Reply #1 on: December 24, 2021, 06:03:29 am »
Check what you have against the declaration:

Code: Pascal  [Select][+][-]
  1. function RunCommand(
  2.   const exename: TProcessString;
  3.   const commands: array of TProcessString;
  4.   out outputstring: string;
  5.   Options: TProcessOptions = [];
  6.   SWOptions: TShowWindowOptions = swoNone
  7. ):Boolean;

It might also be worth adding error checking on the boolean return status which is currently ignored.
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 x86_64 Xcode 13.4
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 aarch64 Xcode 13.4

rail5

  • Newbie
  • Posts: 4
Re: Having trouble with TProcess and RunCommand on Windows
« Reply #2 on: December 24, 2021, 06:37:28 am »
Check what you have against the declaration:

Code: Pascal  [Select][+][-]
  1. function RunCommand(
  2.   const exename: TProcessString;
  3.   const commands: array of TProcessString;
  4.   out outputstring: string;
  5.   Options: TProcessOptions = [];
  6.   SWOptions: TShowWindowOptions = swoNone
  7. ):Boolean;

It might also be worth adding error checking on the boolean return status which is currently ignored.

Hey thanks for the reply

Well after reading your post, setting the project target OS to Win32/Win64 and then clicking "Find declaration of RunCommand / Find declaration of Process" results in: "Error: unit not found: Process"

It still compiles without complaining, and I'm even more flummoxed now as to why RunCommand half-works on Windows builds if it isn't including the Process unit

Tried recompiling the cross-compiler ( as per https://wiki.lazarus.freepascal.org/Cross_compiling_for_Windows_under_Linux ), but same result

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1978
  • Former Delphi 1-7, 10.2 user
Re: Having trouble with TProcess and RunCommand on Windows
« Reply #3 on: December 24, 2021, 07:07:16 am »
You need to add Process to your Uses clause.
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 x86_64 Xcode 13.4
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 aarch64 Xcode 13.4

rail5

  • Newbie
  • Posts: 4
Re: Having trouble with TProcess and RunCommand on Windows
« Reply #4 on: December 24, 2021, 07:32:54 am »
Sorry, I wasn't clear. It's already in the Uses clause (see the screenshot)

The "unit not found" error comes up when I switch the target OS to Win32 or Win64 (and yet, the project still builds)

The reason I tried re-compiling the cross-compiler was that I assumed there was a Win64 "version" of the Process unit that was somehow skipped the first time. Re-compiling didn't fix it though so I'm in the dark

Edit: Also, the compiler logs (from building the cross-compiler) say that the installation of 'fcl-process' was successful

Further edit: /etc/fpc.cfg also contains the line:

Code: Pascal  [Select][+][-]
  1. -Fu/usr/lib/fpc/$fpcversion/units/$FPCTARGET/*

And the "fcl-process" directory (complete with process.o, process.ppu etc) is present in both /usr/lib/fpc/3.2.0/units/x86_64-win64/ and /usr/lib/fpc/3.2.0/units/i386-win32
« Last Edit: December 24, 2021, 07:49:27 am by rail5 »

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1978
  • Former Delphi 1-7, 10.2 user
Re: Having trouble with TProcess and RunCommand on Windows
« Reply #5 on: December 24, 2021, 09:42:34 am »
Check where it's looking for the unit - turn on all messages (Lazarus > Project > Project Options > Compiler options > Verbosity - [ ] Show everything) and copy "All/Original Messages" from messages window to the clipboard at end of compilation. It will show you where it is looking for the Process unit and not finding it.
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 x86_64 Xcode 13.4
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 aarch64 Xcode 13.4

mosquito

  • Full Member
  • ***
  • Posts: 101
Re: Having trouble with TProcess and RunCommand on Windows
« Reply #6 on: December 24, 2021, 11:07:15 am »
Just an idea, if the external executable is not on the same level of the file system, try using the executable's full path.

rail5

  • Newbie
  • Posts: 4
Re: Having trouble with TProcess and RunCommand on Windows
« Reply #7 on: December 24, 2021, 11:44:03 am »
Thanks everybody for replying, and happy holidays by the way

Well, the logs show that the compiler isn't having trouble finding the Process unit (+ Library + Object), so the "unit not found" bug is just isolated in the Source viewer in Lazarus

Code: Pascal  [Select][+][-]
  1. [1.010] (10000) Unitsearch: /usr/lib/fpc/3.2.0/units/x86_64-win64/fcl-process/process.ppu
  2. [1.010] Searching file /usr/lib/fpc/3.2.0/units/x86_64-win64/fcl-process/process.ppu... found
  3. [1.010] (10001) PPU Loading /usr/lib/fpc/3.2.0/units/x86_64-win64/fcl-process/process.ppu
  4. [1.010] (PROCESS)  (10002) PPU Name: /usr/lib/fpc/3.2.0/units/x86_64-win64/fcl-process/process.ppu
  5. [1.010] (PROCESS)  (10011) PPU Source: process.pp not available
  6. [1.010] (PROCESS)  (10011) PPU Source: processbody.inc not available
  7. [1.010] (PROCESS)  (10011) PPU Source: process.inc not available


Which brings me back to square 1 -- RunCommand seems to work on Linux when:
1. provided with a TStringArray of parameters
& 2. provided with an "array literal" so to speak
(as in RunCommand('liesel', SomeTStringArray, ReturnString) vs. RunCommand('liesel', ['-h', '-q', '-c'], ReturnString) for example)

Where on Windows, RunCommand only seems to work when provided with an "array literal,"

+ the TProcess fails (again only on Windows) and I can't tell why (with Parameters.Add() and then Execute). (I also tried with TProcess.CommandLine just to check, and that fails as well)


Is there maybe a problem on Windows just with programmatically determining those parameters as variables instead of being statically-fixed parameters (having the parameters determined at runtime instead of compile time)? The idea is for the user to select parameters from the GUI and then execute

If that's it, is there a way to force the compiler to recognize that a particular variable will only be set at runtime rather than compile time?
« Last Edit: December 24, 2021, 12:10:04 pm by rail5 »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9993
  • FPC developer.
Re: Having trouble with TProcess and RunCommand on Windows
« Reply #8 on: December 24, 2021, 01:30:55 pm »
I use runcommand daily, so it should be something with your setup.

Could you please make a complete minimal console application to give us something to test? Try to enable Delphi mode.

Try also to add the full path to the exe name(e.g. c:\a\b\liesel), maybe in your application the working directory is not what you think it is.

 

TinyPortal © 2005-2018