Thread: how to properly call an executable from C code?

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    4

    how to properly call an executable from C code?

    Hi,

    I have an external executable program( eg. prog.exe) that I will like to call from within my main program consisting of C code.

    prog.exe will do some work and prints the output to a temp.txt file for my main program to use for analysis later.

    Currently I have a function like:

    Code:
    int dowork()
    {
         ......
         int retv;
    
         //in command prompt it is executed as C:> prog.exe options
         retv = system("prog.exe options");  
         if(retv != 0)
             return 1;
    }
    So far it is able to print the results to the text file. However there are issues which I need some advice.

    1. My main program that calls prog.exe is a GUI program.If I call it this way a command prompt window will pop-up promptly then closes,which I think not so nice to be like this. Therefore,is there another better way which I can do the same without having the command prompt window popping up and close?

    2. I need to ensure also that prog.exe has finished writing to the text file before my main program reads the file.Will there be any issue using system()?

    3. How can I properly check if prog.exe has been executed successfully or having errors,since it is an external executable program?

    fyi,I am using windows xp and mingw.
    Thanks in advanced.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You need to use something with a bit more control for your actual OS
    Cprogramming.com FAQ > Run a program from within a program

    system(), whilst portable, is pretty stupid and offers no actual control over what happens next.

    > 3. How can I properly check if prog.exe has been executed successfully or having errors,since it is an external executable program?
    Well if the programmer of prog.exe was a "void main" dimwit, you're screwed.
    But if it returns a proper success/fail status, then you'll be able to get that and work out whether it succeeded or not.

    > 2. I need to ensure also that prog.exe has finished writing to the text file before my main program reads the file
    Wait for it to exit, which system() does.
    But unlike on real operating systems, the system() function rarely returns the actual exit status of the called program (more often than not, you just get the exit status of the shell).

    Like I said, use something like CreateProcess to get detailed control and a useful status back.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    How about
    Code:
    _spawnl(P_WAIT, path, path, filename, buffer, NULL);
    (Microsoft Visual Studio C++)
    I believe it returns whatever the executable program returns as a condition code.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    system() is crappy and must not never be called
    Use any Win32 api (ShellExecute, CreateProcess, etc... (some dozens))

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM