C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-13-2009, 09:41 AM   #1
Registered User
 
Join Date: May 2009
Posts: 3
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.
remy06 is offline   Reply With Quote
Old 05-13-2009, 10:20 AM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,650
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.

Salem is offline   Reply With Quote
Old 05-13-2009, 04:07 PM   #3
Registered User
 
Join Date: Sep 2008
Location: Toronto, Canada
Posts: 518
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.
nonoob is offline   Reply With Quote
Old 05-14-2009, 03:48 AM   #4
Registered User
 
Join Date: Apr 2007
Posts: 136
system() is crappy and must not never be called
Use any Win32 api (ShellExecute, CreateProcess, etc... (some dozens))
Alex31 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 08:06 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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