[fpc-pascal] Executing another program from Pascal

Marco van de Voort marcov at stack.nl
Wed Mar 29 09:05:09 CEST 2006


> In good old days the Turbo Pascal had an EXEC procedure that could 
> execute another program by invoking the DOS command interpreter.

Dos.exec still exist, but as a substitute sysutils.ExecuteProcess is
recommended. (1.9.2+ though) 

Note that both don't exec the command itself.
 
> I have found in the unix unit a function FPEXECV(path, pp) that may be 
> used for it, but it leaves a few questions to me.
> 
> According to the docs the function does not return (if it succeeds). Is 
> there no way of calling another program and then continue the calling 
> program? Could it be done by starting a new thread, putting the call 
> into that thread and let that thread not return while the main program 
> continues? Will it leave allocated memory, that is never freed?

See the earlier replies (and the source of executeprocess)
 
> In the doc?s example the FPEXECV(path, pp) has the same pchar in the 
> path and as the first pchar of the pchar array that pp points to. I have 
> tried similar examples, and they always seem to work with the same phar 
> in the two places, but not otherwise. Is there some deeper philosophy in 
> having the program-name in both path and in the first phar of the array?

Note that fp* calls are calls that roughly comply to unix standards. A man
page for the call minus the "fp" is usually available. (so "man execv" in
this case). From the FreeBSD version:

     .....  The
     first argument, by convention, should point to the file name associated
     with the file being executed.  The list of arguments must be terminated
     by a NULL pointer.....

So the answer is "by convention" :-)

Also have a look at the tprocess class in the VCL, it allows for executing
processes and keeping pipes to it open.

Only if these two instruments (tprocess/executeprocess) are not enough, then
you need to mess with OS specific routines like execv, or windows like
shellexecute.






More information about the fpc-pascal mailing list