![]() |
| | #1 |
| Registered User Join Date: May 2009
Posts: 3
| how to properly call an executable from C code? 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;
}
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 | |
| | #2 |
| and the hat of Jobseeking 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. |
| Salem is offline | |
| | #3 |
| Registered User Join Date: Sep 2008 Location: Toronto, Canada
Posts: 518
| How about Code: _spawnl(P_WAIT, path, path, filename, buffer, NULL); I believe it returns whatever the executable program returns as a condition code. |
| nonoob is offline | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |