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: Rush Manbert <email@hidden>
- Date: Fri, 20 Mar 2009 14:39:31 -0700
Hi David,
Attached is a header file that implements a ProcessWrapper class that
we use to launch and control command line apps.
Here is a little code snippet that uses it (not compiled):
std::vector<std::string> argvVec;
boost::shared_ptr<boost::filesystem::path> pTargetExecutablePath (new
boost::filesystem::path (boost::filesystem::system_complete
(boost::filesystem::path(targetProgramName))));
argvVec.push_back (pTargetExecutablePath); // argv[0] is path to
executable
// Assume an int command line argument
int portNumArg = 30000;
argvVec.push_back(boost::lexical_cast<std::string>(portNumArg));
ProcessWrapper wrapper (argvVec, true);
bool result = wrapper.run();
if (result)
{ // Note we assume that we are starting a long running program, and
that if there is some
// problem (bad args, bad executable path, etc., the program will
terminate within one second.
sleep (1);
if (wrapper.hasTerminated())
{
// Hande failure to start target rogram
}
}
We wrap this in a loop that implements retries. If run() returns true,
then we have started the target program enough for it to look at its
arguments, etc. Later you can check hasTerminated(), and you can call
getChildExitStatus() to see how it finished.
I wrote these in order to run automated tests against a database
server. I actually wrote a LicenseDatabaseServerProcess class that
uses ProcessWrapper and encapsulates the knowledge about the program
command line arguments, etc. This works well enough for us, although
I'm sure there are many possible improvements. As always, YMMV and
it's worth what you paid for it. :-)
Best regards,
Rush
Attachment:
ProcessUtils.h.zip
Description: Zip archive
On Mar 20, 2009, at 1:48 PM, David Gagnon wrote:
Hi,
Our program is a C++ plug-in for Acrobat. So, we need to launch a
command-line program to send HTTP message (sending HTTP message
within a
plug-in cause many problems). Our small command line program take 1
parameter (path) and return an error value. So, I tried "exec" but
it does
not work, because Acrobat is multi-process. See this link:
http://factor-language.blogspot.com/2007/07/execve-returning-enotsup-on-mac-
os-x.html
Also, posix_spawn could be a nice choice but it is available in 10.5
only.
So I think that "system" or "popen" are the most credible candidate
now.
Have a better/simpler idea?
Thanks!
David
On 09-03-19 16:57, "Dave Carrigan" <email@hidden> wrote:
On Mar 19, 2009, at 1:48 PM, David Gagnon wrote:
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... ;-)
If it's objective-c++ you could use NSTask.
There is also Launch Services, which I think comes closest to
ShellExecute, depending on how you're using ShellExecute.
For straight C++ with a true command line program you can likely use
fork/exec.
Give us a better idea of what exactly you need to do, and we can tell
you which of those options is better.
_______________________________________________
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
_______________________________________________
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