#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    10
    Rep Power
    0

    Newbie Capture system() return code


    I'd like to know how to capture the return code from the system function and have my program return it to the shell.

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    int main()
    {
    
      system("Net Use J: \\\\Server\\Share /persistent:no");
      return 0;
    }
    Take this program for example. I'd like to be able to run this .exe from the command line and check the errorlevel using %errorlevel%

    So, from the command line

    Code:
    C:\>MyProg.exe
    C:\>echo %errorlevel%
    85
    C:\>
    In this case the net use command returned error 85, "drive already in use."
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    If system() runs successfully, then the return value should be the %errorlevel% you want.

    From Visual C++6 help page for system (my emphasis):
    Return Value

    If command is NULL and the command interpreter is found, the function returns a nonzero value. If the command interpreter is not found, it returns 0 and sets errno to ENOENT. If command is not NULL, system returns the value that is returned by the command interpreter. It returns the value 0 only if the command interpreter returns the value 0. A return value of 1 indicates an error, and errno is set to one of the following values:

    E2BIG

    Argument list (which is system-dependent) is too big.

    ENOENT

    Command interpreter cannot be found.

    ENOEXEC

    Command-interpreter file has invalid format and is not executable.

    ENOMEM

    Not enough memory is available to execute command; or available memory has been corrupted; or invalid block exists, indicating that process making call was not allocated properly.
    The Remarks section says that if system is passed a NULL, then it will only check that the command interpreter exists.
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    Why not just return the value from system() instead of returning 0 in main().
    Code:
    int main()
    {
      return system("Net Use J: \\\\Server\\Share /persistent:no");
    }
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    10
    Rep Power
    0
    Yes, that's what I was looking for! Like I said, I'm very new to C++, and the obvious still isn't so obvious. Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo