Re: Best way to launch command-line program from C++ application
Re: Best way to launch command-line program from C++ application
- Subject: Re: Best way to launch command-line program from C++ application
- From: Tommy Nordgren <email@hidden>
- Date: Fri, 20 Mar 2009 01:12:19 +0100
On Mar 19, 2009, at 9:48 PM, David Gagnon wrote:
Hi,
What is the best/easiest way to launch a command-line program from
our C++ application? I know how to do it using “System.framework”
but it is a bit complicated. I need a function like “ShellExecute”
on PC... ;-)
Thanks!
Depends on what you wants to do. If you just want to call the
program, and wait for it's return, then the
system call is easiest to use. If you want to read the standard out or
write to standard in of the called program,
then the call popen is easiest to use.
Note that both system and popen takes the command line as a single c
string, and uses the shell to interprete
the command line, so instead I suggest you learn the fork call, and
the exec family of call, (do the man fork,
and man exec to get the docs). Note that the source code to system and
popen is available. See it to implement
better C++ versions that allows passing the command line as an array,
and avoids using the shell
The basic usage is as follows:
pid_t result = fork();
if (result != -1) {
if result (!= 0) {
//In parent , result == the process ID of the child
} else {
//In child
//call a function from the exec family of calls, to replace the
current (child) process with the command you want to execute
//note: exec functions wont return unless an error prevents the
program from being launched
} else {
//Handle errors here
}
-----------------------------------
See the amazing new SF reel: Invasion of the man eating cucumbers from
outer space.
On congratulations for a fantastic parody, the producer replies :
"What parody?"
Tommy Nordgren
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden