Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2020-07

CrossPlatform problem, INFINITE not defined on Linux

10 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bo Berglund

I am porting an old application from Delphi to Lazarus and Windows to Linux.
This application has a unit ExecWait, which was created to simplify running tool programs from the main program, so it supplies two procedures as:

function ExecAndWait(Cmdline: string; Hide: boolean = false; MaxWait:
DWORD = INFINITE; Prio: TExecPrio = normal): DWORD;
function ExecStart(Cmdline: string; InheritHandle: boolean = true;
Prio: TExecPrio = normal): DWORD;

I have had no problems with this unit when moving from Delphi to FPC, it compiles just fine on Windows.

But when I move the code to Linux (via Subversion) it throws an error on "Run/Quick Compile":

ExecWait.pas(15,79) Error: Identifier not found "INFINITE"

The line that gets the error is the declaration for ExecAndWait above.

On Windows I could check where it is defined and I found it in: C:\Programs\Lazarus_2.0.8\fpc\3.0.4\source\rtl\win\wininc\defines.inc

How do I bring in the corresponding Linux version of this? Is there some specific uses clause to set?

Right now I have this:

uses
  {$IFDEF UNIX}
    unix;
  {$ELSE}
	windows;
  {$ENDIF}

As it compiles in Windows it must be something I have to do when porting to Linux....

Bo Berglund Developer in Sweden
Michael Van Canneyt
to Bo Berglund
Bo Berglund wrote 25 lines
I am porting an old application from Delphi to Lazarus and Windows to Linux. This application has a unit ExecWait, which was created to simplify running tool programs from the main program, so it supplies two procedures as: function ExecAndWait(Cmdline: string; Hide: boolean = false; MaxWait: DWORD = INFINITE; Prio: TExecPrio = normal): DWORD; function ExecStart(Cmdline: string; InheritHandle: boolean = true; Prio: TExecPrio = normal): DWORD; I have had no problems with this unit when moving from Delphi to FPC, it compiles just fine on Windows. But when I move the code to Linux (via Subversion) it throws an error on "Run/Quick Compile": ExecWait.pas(15,79) Error: Identifier not found "INFINITE" The line that gets the error is the declaration for ExecAndWait above. On Windows I could check where it is defined and I found it in: C:\Programs\Lazarus_2.0.8\fpc\3.0.4\source\rtl\win\wininc\defines.inc How do I bring in the corresponding Linux version of this?

You can't. INFINITE is specific to Windows.

You must get rid of the ExecAndWait. This is just the first error you will run into, many more will follow.

The corresponding functions exist in FPC :

ExecuteProcess in SysUtils unit:
https://www.freepascal.org/docs-html/current/rtl/sysutils/executeprocess.html

Various RunCommand procedures in process unit:
https://www.freepascal.org/docs-html/current/fcl/process/runcommand.html

or use TProcess.
https://www.freepascal.org/docs-html/current/fcl/process/tprocess.html

Michael.

Bo Berglund
to Michael Van Canneyt
Michael Van Canneyt wrote 46 lines
On Sun, 19 Jul 2020, Bo Berglund via fpc-pascal wrote: > I am porting an old application from Delphi to Lazarus and Windows to > Linux. > This application has a unit ExecWait, which was created to simplify > running tool programs from the main program, so it supplies two > procedures as: > > function ExecAndWait(Cmdline: string; Hide: boolean = false; MaxWait: > DWORD = INFINITE; Prio: TExecPrio = normal): DWORD; > function ExecStart(Cmdline: string; InheritHandle: boolean = true; > Prio: TExecPrio = normal): DWORD; > > I have had no problems with this unit when moving from Delphi to FPC, > it compiles just fine on Windows. > > But when I move the code to Linux (via Subversion) it throws an error > on "Run/Quick Compile": > > ExecWait.pas(15,79) Error: Identifier not found "INFINITE" > > The line that gets the error is the declaration for ExecAndWait above. > > On Windows I could check where it is defined and I found it in: > C:\Programs\Lazarus_2.0.8\fpc\3.0.4\source\rtl\win\wininc\defines.inc > > How do I bring in the corresponding Linux version of this? You can't. INFINITE is specific to Windows. You must get rid of the ExecAndWait. This is just the first error you will run into, many more will follow. The corresponding functions exist in FPC : ExecuteProcess in SysUtils unit: https://www.freepascal.org/docs-html/current/rtl/sysutils/executeprocess.html Various RunCommand procedures in process unit: https://www.freepascal.org/docs-html/current/fcl/process/runcommand.html or use TProcess. https://www.freepascal.org/docs-html/current/fcl/process/tprocess.html

OK, thanks.
Then I will have to make a rewrite of the unit...

Possibly add IFDEF:s such that the Windows version remains and I create equivalents for Linux using for example TProcess.

Thankfully there are only these two functions to deal with: ExecAndWait() starts a program and waits for it to complete ExecStart() starts a program and leaves it running

These are used exactly 1 time each in the full application...

Bo Berglund Developer in Sweden
Michael Van Canneyt
to Bo Berglund
Bo Berglund wrote 16 lines
> > or use TProcess. > https://www.freepascal.org/docs-html/current/fcl/process/tprocess.html > OK, thanks. Then I will have to make a rewrite of the unit... Possibly add IFDEF:s such that the Windows version remains and I create equivalents for Linux using for example TProcess. Thankfully there are only these two functions to deal with: ExecAndWait() starts a program and waits for it to complete ExecStart() starts a program and leaves it running These are used exactly 1 time each in the full application...

All the more reason to dispense with the unit.
Less code to maintain.

Michael.

Bo Berglund
to Michael Van Canneyt
Michael Van Canneyt wrote 5 lines
> These are used exactly 1 time each in the full application... All the more reason to dispense with the unit. Less code to maintain.

Seems like RunCommandInDir or simply RunCommand can do what the ExecWait with all its setting up of process flags and whatnot does. So as you say I could as well discard the unit altogether and put this call in the main code instead.

Thanks for pointing it out. Original code is from about 2008...

In fact a few years back I created a video handler using ffmpeg as the processing program and when looking back there I see now I used RunCommandInDir. And it is also Windows/Linux.
This was a project built entirely with Lazarus/Fpc of course.

Bo Berglund Developer in Sweden
Bo Berglund
to Michael Van Canneyt: You can't. INFINITE is specific to Windows. You must get rid of the ExecAndWait. This is…
Michael Van Canneyt wrote
Various RunCommand procedures in process unit: https://www.freepascal.org/docs-html/current/fcl/process/runcommand.html

But it looks like it hangs around until the started process exits... How can I set a timeout on the execution (don't want the whole system to hang if this external program does not exit)?

And if I just want to start a process but not wait for it to exit? Say a s/w updater that requires the application to exit before it can do its thing?

I looked at the options that can be applied to the call and these are somewhat non-obvious:

poWaitOnExit (if I don't use this will the call return at once?) poDetached (what does this do?)
poRunIdle (what does this do?)

Bo Berglund Developer in Sweden
Michael Van Canneyt
to Bo Berglund
Bo Berglund wrote 12 lines
On Sun, 19 Jul 2020 08:46:14 +0200 (CEST), Michael Van Canneyt wrote: > Various RunCommand procedures in process unit: > https://www.freepascal.org/docs-html/current/fcl/process/runcommand.html > But it looks like it hangs around until the started process exits... How can I set a timeout on the execution (don't want the whole system to hang if this external program does not exit)? And if I just want to start a process but not wait for it to exit?

Use TProcess.

https://www.freepascal.org/docs-html/current/fcl/process/tprocess.html

do NOT specify poWaitOnExit in the options, and simply call Execute. It will .

Bo Berglund
to Michael Van Canneyt
Michael Van Canneyt wrote 23 lines
On Sun, 19 Jul 2020, Bo Berglund via fpc-pascal wrote: > On Sun, 19 Jul 2020 08:46:14 +0200 (CEST), Michael Van Canneyt > wrote: > >> Various RunCommand procedures in process unit: >> https://www.freepascal.org/docs-html/current/fcl/process/runcommand.html >> > > But it looks like it hangs around until the started process exits... > How can I set a timeout on the execution (don't want the whole system > to hang if this external program does not exit)? > > And if I just want to start a process but not wait for it to exit? Use TProcess. https://www.freepascal.org/docs-html/current/fcl/process/tprocess.html do NOT specify poWaitOnExit in the options, and simply call Execute. It will clarify:

If I do not specify [poWaitOnExit] to Execute, then it will return immediately with the process running and I can check whenever I like if it is still running using the Running() method. This seems like I can do what I need.

Question:
---------
If I free the TProcess object will that terminate the running process or just detatch from it?
The description for TProcess.Destroy seems to indicate this.

The reason I ask this much is that the use case here is a remotely running monitoring system taking regular measurements and as an option after each measurement an analysis program may be started to evaluate if an alarm should be sent out.
Since the system will be unattended I have to make sure it won't hang anywhere...

Bo Berglund Developer in Sweden
Michael Van Canneyt
to Bo Berglund
Bo Berglund wrote 37 lines
On Sun, 19 Jul 2020 17:51:59 +0200 (CEST), Michael Van Canneyt wrote: > > > On Sun, 19 Jul 2020, Bo Berglund via fpc-pascal wrote: > >> On Sun, 19 Jul 2020 08:46:14 +0200 (CEST), Michael Van Canneyt >> wrote: >> >>> Various RunCommand procedures in process unit: >>> https://www.freepascal.org/docs-html/current/fcl/process/runcommand.html >>> >> >> But it looks like it hangs around until the started process exits... >> How can I set a timeout on the execution (don't want the whole system >> to hang if this external program does not exit)? >> >> And if I just want to start a process but not wait for it to exit? > > Use TProcess. > > https://www.freepascal.org/docs-html/current/fcl/process/tprocess.html > > do NOT specify poWaitOnExit in the options, and simply call Execute. > It will return at once. To clarify: If I do not specify [poWaitOnExit] to Execute, then it will return immediately with the process running and I can check whenever I like if it is still running using the Running() method. This seems like I can do what I need. Question: --------- If I free the TProcess object will that terminate the running process or just detatch from it?

It detaches.

Michael.

Marco van de Voort
to Bo Berglund: On Sun, 19 Jul 2020 08:46:14 +0200 (CEST), Michael Van Canneyt But it looks like it hangs…

Op 7/19/2020 om 5:40 PM schreef Bo Berglund via fpc-pascal:

Show quoted text 9 lines
On Sun, 19 Jul 2020 08:46:14 +0200 (CEST), Michael Van Canneyt wrote: > Various RunCommand procedures in process unit: > https://www.freepascal.org/docs-html/current/fcl/process/runcommand.html > But it looks like it hangs around until the started process exits... How can I set a timeout on the execution (don't want the whole system to hang if this external program does not exit)?

Runcommand handles the most used scenarios only. But in FPC 3.2.0 there have been some changes to make own variants easier.

I happened to make a variant with a timeout last week, and use this as a demonstration for that (3.2.0+) functionality :

https://forum.lazarus.freepascal.org/index.php/topic,50525.msg368880.html#msg368880

but that is as of yet still untested on non-windows. (but it is general in principle)

I'll answer these because they are not documented yet:

poRunIdle -  mainly for tprocess.runcommandloop:  run a sleep() in the main loop that polls the pipes.

                      In general, enable it for runcommands that run longer (to reduce CPU use), disable it if you expect that the process runs very short (e.g. "hostname"  on linux)

poDetached - The Windows implementation of poNoconsole of FPC 3.0.4 and older.   FPC 3.2.0+ ponoconsole is instantiates less of the console, but this causes problems for a few programs (like mysql console). In those cases use podetached instead ponoconsole