get the return value of a program

This is a discussion on get the return value of a program within the C++ Programming forums, part of the General Programming Boards category; How can I get the return value of a program that I launched from my program?...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Post get the return value of a program

    How can I get the return value of a program that I launched from my program?

  2. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    How are you calling the program from within a program?

    popen(), system(), fork()/exec() ?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Which of these are you using to run your sub-program?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    echo %errorlevel%
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    CreateProcess().

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    I dunno, something imaginative like reading the manual page for CreateProcess(), which has a handy "see also" section.
    Obviously, this is the place to look for related functions, and gosh, there it is.
    http://msdn.microsoft.com/library/de...odeprocess.asp
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Code:
    BOOL WINAPI GetExitCodeProcess(
      HANDLE hProcess,
      LPDWORD lpExitCode
    );
    Does this mean that
    Code:
    DWORD main()
    is proper and valid too?


    Also, what happens when I try to get the exit code on a
    Code:
    void main()
    program?

    Thanks for link , kinda stupid question thought I suppose, seeing how I have USED THE STUPID FUNC BEFORE! (Seeing the function prototype again brought it back.)
    Last edited by Queatrix; 10-10-2006 at 11:19 AM.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Also, what happens when I try to get the exit code on a
    Code:
    void main()
    program?
    Why do you think you're not supposed to use void main()?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Yea, that's what I figured, but still, what would happen? Would the function fail? Or would it just return 0x00000000?

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It's undefined!
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    True, but a really old compiler like turbo C++ or MSVC++6.0 would accept it as being a valid statement. Nowdays, most people put int main ( void )

  12. #12
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Is DWORD main(void) valid too?

  13. #13
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    There is a saying on IRC channels...

    TIAS!

    (Try it and see!)

  14. #14
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Quote Originally Posted by Dev-C++
    5 test.cpp `main' must return `int'
    Nope.

    Why is Win32 sooo... hung up on DWORD!?

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    > DWORD main()
    int main is the portable answer.

    It's just that a DWORD is capable of storing all possible integer values on that particular platform. I guess win32 GUI programs which begin with winmain() also return a DWORD as the exit status.

    Besides, very few operating system shells can cope with process exit status values which exceed 8 bits, it isn't that much of a deal.

    > Also, what happens when I try to get the exit code on a
    <clint>Well punk, do you feel lucky</clint>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-05-2009, 10:32 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. string class errors
    By CodeMonkey in forum C++ Programming
    Replies: 13
    Last Post: 07-20-2003, 11:20 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21