Board index » delphi » 'exec' command

'exec' command

For a couple of days now I've been trying to get the 'exec' command to
work, could someone please just give me an example, say I wanted to
execute a program in the path C:\temp\pascal.exe how would I write that
using the 'exec' command in pascal.

Thanks For All Your Help.

 

Re:'exec' command


Quote
Gavin Carter wrote:

> For a couple of days now I've been trying to get the 'exec' command to
> work, could someone please just give me an example, say I wanted to
> execute a program in the path C:\temp\pascal.exe how would I write that
> using the 'exec' command in pascal.

Exec('c:\temp\pascal.exe','parameters');

This command required correct memory settings, for example:
{$M 16384,0,0}

------
Fooly

Re:'exec' command


Quote
Pal Fulajtar wrote:

> Gavin Carter wrote:

> > For a couple of days now I've been trying to get the 'exec' command to
> > work, could someone please just give me an example, say I wanted to
> > execute a program in the path C:\temp\pascal.exe how would I write that
> > using the 'exec' command in pascal.

> Exec('c:\temp\pascal.exe','parameters');

> This command required correct memory settings, for example:
> {$M 16384,0,0}

I'll agree with your post, except for one minor detail -- you should ALWAYS call
swapvectors(); both before and after the call to exec().  Also, it would be a
good idea to check the value of the DosError variable after calling any external
DOS function.

Quote
> ------
> Fooly

--
Scott Earnest                      | _,-""-_,-""-_,-""-_,-""-_,-""-_,-" |
set...@ix.netcom.com (primary)     | We now return you to our regularly |
siny...@{*word*104}space.org (alternate) | scheduled chaos and mayhem. . . .  |

Re:'exec' command


-=[ In:gavin_car...@dial.pipe was heard to say... ]=-

 In> For a couple of days now I've been trying to get the 'exec' command
 In> to  work, could someone please just give me an example, say I wanted to
 In> execute a program in the path C:\temp\pascal.exe how would I write
 In> that  using the 'exec' command in pascal.

 In> Thanks For All Your Help.

PROGRAM Example;

uses Dos;                           { you need to specify this unit       }

{$M,4096,0,0}                       { reduce the heap so there's enough   }
                                    { memory for your program             }
BEGIN
   SwapVectors;
   Exec('C:\TEMP\PASCAL.EXE','');
   SwapVectors;
END.

Check your manuals for an explanation of each part of the above program.
I've used the minimum code required so I don't introduce anything not
directly related to using Exec().

        -- Kim Forwood --

  /-=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=-\
  $           Kim Forwood  <kim.forw...@access.cn.camriv.bc.ca>          %
  %              http://goodship.cn.camriv.bc.ca/~kforwood/              $
  $         For what purpose is life, if one cannot live freely?         %
  \-=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=-/

___ Blue Wave/QWK v2.20

Re:'exec' command


Quote

> For a couple of days now I've been trying to get the 'exec' command to
> work, could someone please just give me an example, say I wanted to
> execute a program in the path C:\temp\pascal.exe how would I write that
> using the 'exec' command in pascal.

limit your memory (using the {$M stack,minheap,maxheap} directive)
... and don't forget to use SwapVectors before and after calling exec.

Sample (completely untested}

PROGRAM ExecTest;
{$M 4096,0,65536}  {Set up a 4k stack and a 64k heap (max)}
uses DOS,CRT;

BEGIN
  SwapVectors;
  exec('c:\dos\command.com','/cdir');
  SwapVectors;
END.

--------------------------------------------------------------------------
            Franklin: EMail: frank...@stardate.westfalen.de
--------------------------------------------------------------------------
## CrossPoint v3.02 ##

Re:'exec' command


Quote
> > For a couple of days now I've been trying to get the 'exec' command to
> > work, could someone please just give me an example, say I wanted to
> > execute a program in the path C:\temp\pascal.exe how would I write that
> > using the 'exec' command in pascal.

> limit your memory (using the {$M stack,minheap,maxheap} directive)
> ... and don't forget to use SwapVectors before and after calling exec.

> Sample (completely untested}

> PROGRAM ExecTest;
> {$M 4096,0,65536}  {Set up a 4k stack and a 64k heap (max)}
> uses DOS,CRT;
> BEGIN
>   SwapVectors;
>   exec('c:\dos\command.com','/cdir');
>   SwapVectors;
> END.

   The right approach, more or less, but your example is lacking in a few
things.  First, I would start with 0 Heap_Max, instead of 64K, because
there's no reason to assume that any is needed.  For the person starting
out with Exec, I reccommend {$M 8192,0,0}, to see what works and what
doesn't - it's easy to adjust from there.
   Next, I consider a PPP (Poor Programming Practice) to use an explicit
path in a statement in a program - it severely restricts portability,
since there's no assurance that COMMAND.COM is located where you have it
coded.  It may be on your system, but it isn't on mine (and many others).
Associated with this is the fact that not all operating systems have
"COMMAND.COM" as their command processors.  A good solution to these
issues is to use the system to find it, whe=atever it's named, and run
it:
  Exec (GetEnv('COMSPEC'),'/C '+command_to_execute parameters)
   Next, I suggest always protecting against possible errors and
failures.  This is done with checking 2 things: DOSExitCode (a
self-resetting function) and DOSError (a system variable).  Both will
return 0 is the Exec was successful:
  I := DOSExitCode;
  if I <> 0 then writeln ('DOSExitCode error: ',I);
  if DOSError <> 0 then writeln ('DOSError: ',DOSError)
   Lastly, there's no such thing as "/CDIR"; at the least it should be
"/C DIR"...

Re:'exec' command


Quote
Gavin Carter <gavin_car...@dial.pipex.com> wrote:
>For a couple of days now I've been trying to get the 'exec' command to
>work, could someone please just give me an example, say I wanted to
>execute a program in the path C:\temp\pascal.exe how would I write that
>using the 'exec' command in pascal.
>Thanks For All Your Help.

{$M 8192,0,0}           { Leave memory for child process }

    Writeln(' This program makes a ZIP archive .');
      Command:='pkzip \pdox40\mydata\*.*';
<multiple snips to make generic example>
    begin
      SwapVectors;
      Exec(GetEnv('COMSPEC'), '/C ' + Command);
      SwapVectors;
      If DosError <> 0 then
        WriteLn('ERROR ! ! !  Could not execute COMMAND.COM')
{ process error here }
      Else Begin
        Writeln(' File of live data tables written')
       End;

Re:'exec' command


Some notes to Bryan Coyle's reply ;

Writeln(' File of live data tables written') is like assuming the meal
is good just because you've killed the chicken. You'll want to examine
the program's exit code to check if everything went as intended. Just
check if DosExitCode equals 0 after you've checked whether DosError
equals 0. DosError tells you whther or not the program was found and
could be executed. It is DOS that informs you of this. DosExitCode is
information provided by the program. Convention is that a program
returns exit code 0 if it could complete it's task without errors.
Otherwise it returns an exit code > 0, e.g. because a wrong syntax was
used. In the case of Bryan Cole's example the program you execute is the
command interpreter (in most DOS cases COMMAND.COM, in better cases
4DOS.COM (eat your heart out Bill!). You supply it with the command line
arguments '/C pkzip \pdox40\mydata\*.*'. This tells the command
interpreter to find and execute program pkzip and execute it with the
command line arguments '\pdox40\mydata\*.*'. However, if pkzip cannot be
found (e.g. because it's location is not included in the path
environment variable) then DosError wiil still be 0. That's because the
program you executed (the command interpreter) could be executed without
a problem. Because the command interpreter could not execute pkzip it
will return an exit code > 0. DosExitCode will inform you of this. If
pkzip itself could be executed but encountered an error (e.g. a syntax
error or an archive file that could not be accessed), then the exit code
of pkzip will be > 0 and it will be returned by the command interpreter
as it's own exit code. So in any case DosExitCode will tell you if the
command interpreter or the program executed by the command interpreter
could do it's job.

By using Exec( 'pkzip.exe', \pdox40\mydata\*.*'') you won't be needing
the intermediate part of the command interpreter. But you will have to
know the exact location of pkzip and it's extension (.com or .exe,
you'll need the command interpreter to execute a .bat file).

The $M compiler directive in Bryan's example is very strict. You won't
be able to allocate any memory by using the new operator. Furthermore
you'll soon find that you'll be running out of available memory when
calling Exec. A solution to this problem is given by Michael Tischer in
'Turbo Pascal Intern'. He swaps the running TP program to Ems or disk
and then frees previously allocated memory before executing a child
program. If you want source code of the routines used by him you can
email me. I've modified his code a bit to resolve a bug and to enhance
it a bit.

Re:'exec' command


On Fri, 03 Jan 1997 01:35:34 +0100,  Han van Eekelen
<h...@IAEhv.nl>  

Quote
>Because the command interpreter could not execute pkzip it
>will return an exit code > 0. DosExitCode will inform you of this. If
>pkzip itself could be executed but encountered an error (e.g. a syntax
>error or an archive file that could not be accessed), then the exit code
>of pkzip will be > 0 and it will be returned by the command interpreter
>as it's own exit code. So in any case DosExitCode will tell you if the
>command interpreter or the program executed by the command interpreter
>could do it's job.

I don't know if things work like you describe for 4DOS or other
command shells, but they definitely are not applicable to
command.com.  

DosError contains the result of the Exec function. This is zero,
or the error returned by DOS Function 4Bh, Load and Execute
Program.  Typical values would be 0=Success, 2=File not found,
3=Path not found, 5=Access denied, 8=Insufficient memory.

DosExitCode is the result returned by the child process. This
value depends on the program.

When using the command shell (command.com), DosError will
reflect the success or failure of the Exec Function to locate,
and load the shell.  DosError would be non-zero if there was
insufficient memory, or if GetEnv('COMSPEC') returned an
improper entry.  If the child process does not exist, the shell
will display "bad command or file name", and if there is not
enough memory to load the program, the shell will display
"program too big to fit in memory".  But in both instances, both
DosError and DosExitCode will be zero!  AFAIK, command.com
always returns DosExitCode of zero!  

If you need the DosExitCode returned by the child process you
have to invoke the program directly.  This requires that you
know the full name, so the hardest part is probably remembering
if the program is an .EXE or .COM file.  To search the DOS PATH
simply use FSearch('filename.ext', '.;'+GetEnv('PATH'));  

Lookup FSearch for examples and additional information.

    ...red

Re:'exec' command


Quote
On Fri, 3 Jan 1997, Han van Eekelen wrote:
> Some notes to Bryan Coyle's reply ;

> By using Exec( 'pkzip.exe', \pdox40\mydata\*.*'') you won't be needing
> the intermediate part of the command interpreter. But you will have to
> know the exact location of pkzip and it's extension (.com or .exe,
> you'll need the command interpreter to execute a .bat file).

You failed to mention that Turbo Pascal has a function that allows
searching for an executable and returns a path + program to use in the
first parameter of Exec.
TestPrg := 'UNZIP.EXE';
TmpPath := FSearch(TestPrg,GetEnv('PATH'));
SwapVectors;
 Exec(TestPrg, ' -t \some\dir\filename.zip');
SwapVectors;

Quote
> The $M compiler directive in Bryan's example is very strict. You won't
> be able to allocate any memory by using the new operator. Furthermore
> you'll soon find that you'll be running out of available memory when
> calling Exec. A solution to this problem is given by Michael Tischer in
> 'Turbo Pascal Intern'. He swaps the running TP program to Ems or disk
> and then frees previously allocated memory before executing a child
> program. If you want source code of the routines used by him you can
> email me. I've modified his code a bit to resolve a bug and to enhance
> it a bit.

There is a routine called SPAWNO by Ralf Brown which allows swapping
among EMS/XMS/Disk and perhaps DPMI although I doubt it.  It leaves less
than 1 k behind after swapping and then loads everything back in (all at
a very fast pace).  Also available from his WWW page is his famous
Interrupt Listing.

http://www.cs.cmu.edu/afs/cs.cmu.edu/user/ralf/pub/WWW/ralf-home.html

--
/----------------------------------------------------------------------------\
| Jeff Patterson                       Internet: aa...@fan.nb.ca             |
| Computer Consultant                                                        |
| Author of Trumpet TCP/IP interface routines for Turbo Pascal 7.0           |
| Programmer  | PGP Public Key available from pgp-public-k...@keys.pgp.net   |
| PGP Info: 2048/A8A1DCD5 : E0 9E 9B EF C8 E4 68 3D  B5 9C 72 4C EC 61 DD 7A |
\----------------------------------------------------------------------------/

Other Threads