[comp.sys.ibm.pc] Can batch files return value to C program?

jfbruno@rodan.acs.syr.edu (John F. Bruno) (03/17/90)

I calling a batch file using the Turbo-C system() call. Is there a way
to get the value of "ERRORLEVEL" upon returning to the Turbo-C program?
system() is declared as an int, but it only returns 0 or -1, I believe
it returns -1 only if it couldn't find COMMAND.COM (and maybe -1 if the
batch file wasn't found, but I haven't tested it for that)  I can't set
an environment variable in the batch file because when I return to the
calling program, that environment is gone. 

I'd appreciate any suggestions
---jb (jfbruno@rodan.acs.syr.edu)

cs4g6ag@maccs.dcss.mcmaster.ca (Stephen M. Dunn) (03/18/90)

In article <2469@rodan.acs.syr.edu> jfbruno@rodan.acs.syr.edu (John F. Bruno) writes:
$I calling a batch file using the Turbo-C system() call. Is there a way
$to get the value of "ERRORLEVEL" upon returning to the Turbo-C program?
$system() is declared as an int, but it only returns 0 or -1, I believe
$it returns -1 only if it couldn't find COMMAND.COM (and maybe -1 if the
$batch file wasn't found, but I haven't tested it for that)  I can't set
$an environment variable in the batch file because when I return to the
$calling program, that environment is gone. 

    Please note that a batch file can not return an errorlevel at all.
Only .COM and .EXE programs return errorlevels, and to get their return
codes they can not be executed using the system () call, since that
invokes COMMAND.COM first and COMMAND.COM doesn't pass back the errorlevel
of any program it spawns.

   There are programs that can update copies of the environment table other
than their own; you may want to get your hands on one or two of those and
see if you can implement some form of return codes using them.
-- 
Stephen M. Dunn                               cs4g6ag@maccs.dcss.mcmaster.ca
          <std_disclaimer.h> = "\nI'm only an undergraduate!!!\n";
****************************************************************************
    "So sorry, I never meant to break your heart ... but you broke mine."

Ralf.Brown@B.GP.CS.CMU.EDU (03/18/90)

In article <26029AB4.25754@maccs.dcss.mcmaster.ca>, cs4g6ag@maccs.dcss.mcmaster.ca (Stephen M. Dunn) wrote:
}    Please note that a batch file can not return an errorlevel at all.
}Only .COM and .EXE programs return errorlevels, and to get their return
}codes they can not be executed using the system () call, since that
}invokes COMMAND.COM first and COMMAND.COM doesn't pass back the errorlevel
}of any program it spawns.

Yet another case in which 4DOS does things the way COMMAND *should* have done
them.  4DOS properly passes back the errorlevel of a program started with /C.
--
UUCP: {ucbvax,harvard}!cs.cmu.edu!ralf -=- 412-268-3053 (school) -=- FAX: ask
ARPA: ralf@cs.cmu.edu  BIT: ralf%cs.cmu.edu@CMUCCVMA  FIDO: Ralf Brown 1:129/46
"How to Prove It" by Dana Angluin              Disclaimer? I claimed something?
16. proof by cosmology:
    The negation of the proposition is unimaginable or meaningless.  Popular
    for proofs of the existence of God.

cjveenm@cs.vu.nl (Veenman CJ) (03/22/90)

In article <26029AB4.25754@maccs.dcss.mcmaster.ca> cs4g6ag@maccs.dcss.mcmaster.ca (Stephen M. Dunn) writes:

>    Please note that a batch file can not return an errorlevel at all.
>Only .COM and .EXE programs return errorlevels, and to get their return
>codes they can not be executed using the system () call, since that
>invokes COMMAND.COM first and COMMAND.COM doesn't pass back the errorlevel
>of any program it spawns.
>
>   There are programs that can update copies of the environment table other
>than their own; you may want to get your hands on one or two of those and
>see if you can implement some form of return codes using them.

The reason you do a system () call is to let your shell use your PATH in finding
the program you want to execute, but I can be wrong. In this case you can do a
exec call and use your PATH yourself to find the program. Then _you_ will get
the possible error code. In Turbo Pascal the procedure Exec can be used and in C
something simalar might exist. I wrote a procedure to do this and it works.

				Cor.

miller@b-mrda.ca.boeing.com (Mark Miller) (03/23/90)

This may seem a little messy, but you could try writing the result you
want to "pass back" to a file, then read the file with TC to get the 
value.

======================================================================
Mark R. Miller                     +    Disclaimer: Opinions expressed  
Boeing Commercial Airplane Co.     +    are mine and mine alone, and    
Seattle, WA                        +    not necessarily those of my     
Internet: miller@b-mrda.boeing.com +    employer.                       
Voicenet: (206) 237-0960           +                                   
                                   +       +++ Phillipians 4:13 +++    
======================================================================