Board index » delphi » Exec procedure not working (what's wrong?)

Exec procedure not working (what's wrong?)

Quote
In article <4v0h3t$...@clarknet.clark.net> z...@sp.org (Konrad) writes:
>Hi,
>This may seem kindof stupid but I can't get the Exec procedure to
>work. I have:
>{$M $4000,0,0 }  { 16K stack, no heap }
>uses crt, dos;
>---irrelevant parts snipped---
>Procedure dosshell;
>Begin
>  SwapVectors;
>  Exec(c:\dos\dosshell);
>  SwapVectors;
>  if DosError <> 0 then{ Error? }
>    WriteLn('Dos error #', DosError)
>  else
>    WriteLn('Finished')
>end;
>When I run it I get the error: "Error 3: Unknown Identifier" when it
>reaches the "c:\dos\dosshell". It does that on any command-line I've

That's the compile phase, *not* the run phase.  You have a syntax error
because you're using exec() improperly.  Change:

Quote
>  Exec(c:\dos\dosshell);

to

  Exec('c:\dos\dosshell.exe','');

and it should work.  Note that you should use the full name of the program
(with extention), otherwise the DOS call won't find the program.

Quote
>tried. What's wrong with it? (I even tried the example in HELP which
>asks for a command-line and it errors out with "DosError 2" or
>"DosError 3") Anyone have an idea? Thanks a lot. (This is NOT
>homework! :)))
>-------------------------------------------------------------------
>Konrad Rokicki                      "Linux:
>zero-G                                Because a PC
>sroki...@clark.net            is a terrible thing
>                                             to waste!"

--
Scott Earnest          | We now return you to our regularly scheduled |
siny...@{*word*104}space.org | chaos and mayhem. . . .                      |
 

Re:Exec procedure not working (what's wrong?)


Hi,
This may seem kindof stupid but I can't get the Exec procedure to
work. I have:

{$M $4000,0,0 }  { 16K stack, no heap }
uses crt, dos;

---irrelevant parts snipped---

Procedure dosshell;
Begin
  SwapVectors;
  Exec(c:\dos\dosshell);
  SwapVectors;
  if DosError <> 0 then{ Error? }
    WriteLn('Dos error #', DosError)
  else
    WriteLn('Finished')
end;

When I run it I get the error: "Error 3: Unknown Identifier" when it
reaches the "c:\dos\dosshell". It does that on any command-line I've
tried. What's wrong with it? (I even tried the example in HELP which
asks for a command-line and it errors out with "DosError 2" or
"DosError 3") Anyone have an idea? Thanks a lot. (This is NOT
homework! :)))

-------------------------------------------------------------------
Konrad Rokicki                      "Linux:
zero-G                                Because a PC
sroki...@clark.net            is a terrible thing
                                             to waste!"

Re:Exec procedure not working (what's wrong?)


Quote
On Fri, 16 Aug 1996, Konrad wrote:
> This may seem kindof stupid

Hm. Let's see.

Quote
>   Exec(c:\dos\dosshell);

Error 1: Put the program's name in single quotes.
Error 2: The program is probably called DOSSHELL.EXE. Don't forget the
extension.

Now, the whole thing should read:

Exec('c:\dos\dosshell.exe');

Re:Exec procedure not working (what's wrong?)


Quote
>Hi,
>This may seem kindof stupid but I can't get the Exec procedure to
>work. I have:

[zap]

Quote
>Procedure dosshell;
>Begin
>  SwapVectors;
>  Exec(c:\dos\dosshell);
>  SwapVectors;
>  if DosError <> 0 then{ Error? }
>    WriteLn('Dos error #', DosError)
>  else
>    WriteLn('Finished')
>end;

The correct syntax for exec is:
   exec(command,parameter:string);

Therefore, this should work:
  exec(getenv('COMSPEC'),'');

if you want to run a program, you do:
  exec(getenv('COMSPEC'),'/c'+programname);  {programname is string}

There is a valid reason why you use command.com instead of the program
itself: portions of command.com are transient, so you have to get
yourself a fresh copy.

Hope this helps.

---
Robert Wagner
psw...@unix1.sncc.lsu.edu

Re:Exec procedure not working (what's wrong?)


Quote
z...@sp.org (Konrad) wrote:
>Hi,
>This may seem kindof stupid but I can't get the Exec procedure to
>work. I have:
>---irrelevant parts snipped---

Put quotes around c:\dos\dosshell like this :

Exec ('c:\dos\dosshell');

Greetz,
Menno

Re:Exec procedure not working (what's wrong?)


condor (condor batch account) wrote:

Quote
>z...@sp.org (Konrad) wrote:
>>Hi,
>>This may seem kindof stupid but I can't get the Exec procedure to
>>work. I have:

>>---irrelevant parts snipped---

>Put quotes around c:\dos\dosshell like this :

>Exec ('c:\dos\dosshell');

>Greetz,
>Menno

There is a fantastic example program which comes with TP6 which I am
appending herewith.  I hope I am not violating copyright of Borland
International but as I am listing as is I am advised I am OK.

John

_______________________  Cut here _________________________________

{ Turbo Exec }
{ Copyright (c) 1985,90 by Borland International, Inc. }

program ExecDemo;

(*
  Demonstration program that shows how to use the Dos
  unit's Exec procedure to execute DOS commands (including
  running other programs or batch files).

  This program keeps prompting you for a DOS command until
  you enter a blank line.

  When using Exec, make sure you specify a {$M} directive
  so the heap leaves some memory available for the child
  process.
*)

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

uses Dos;

var
  Command: string[127];

begin
  repeat
    Write('Enter DOS command: ');
    ReadLn(Command);
    if Command <> '' then
    begin
      SwapVectors;
      Exec(GetEnv('COMSPEC'), '/C ' + Command);
      SwapVectors;
      if DosError <> 0 then
        WriteLn('Could not execute COMMAND.COM');
      WriteLn;
    end;
  until Command = '';
end.
_______________________ end of source _________________________

Re:Exec procedure not working (what's wrong?)


condor (condor batch account) wrote:

Quote
>z...@sp.org (Konrad) wrote:
>>Hi,
>>This may seem kindof stupid but I can't get the Exec procedure to
>>work. I have:

>>---irrelevant parts snipped---

>Put quotes around c:\dos\dosshell like this :

>Exec ('c:\dos\dosshell');

>Greetz,
>Menno

There is a fantastic example program which comes with TP6 which I am
appending herewith.  I hope I am not violating copyright of Borland
International but as I am listing as is I am advised I am OK.

John

_______________________  Cut here _________________________________

{ Turbo Exec }
{ Copyright (c) 1985,90 by Borland International, Inc. }

program ExecDemo;

(*
  Demonstration program that shows how to use the Dos
  unit's Exec procedure to execute DOS commands (including
  running other programs or batch files).

  This program keeps prompting you for a DOS command until
  you enter a blank line.

  When using Exec, make sure you specify a {$M} directive
  so the heap leaves some memory available for the child
  process.
*)

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

uses Dos;

var
  Command: string[127];

begin
  repeat
    Write('Enter DOS command: ');
    ReadLn(Command);
    if Command <> '' then
    begin
      SwapVectors;
      Exec(GetEnv('COMSPEC'), '/C ' + Command);
      SwapVectors;
      if DosError <> 0 then
        WriteLn('Could not execute COMMAND.COM');
      WriteLn;
    end;
  until Command = '';
end.
_______________________ end of source _________________________

Other Threads