Reputation: 6707
Say I have an application I write, that relies for some task on an externat app (lets call it "tool") that is installed on my machine. In my program, I call it with system( "tool myarguments" );
, works fine.
Now, I want to distribute my app. Of course, the end-user might not have "tool" installed on his machine, so I would like my app to check this, and printout a message for the user. So my question is:
Is there a portable way to check for the existence of an app on the machine? (assuming we know its name and it is accessible through the machine's shell).
Additional information: First idea was to check the existence of the binary file, but:
My first opinion on this question is "No", but maybe somebody has an idea ?
Reference: system()
Related: stackoverflow.com/questions/7045879
Upvotes: 3
Views: 3102
Reputation: 42899
If running the tool without argument has no side-effect, and is expected to return an exit code of 0, you can use system("tool")
to check tool's existence.
You can check whether the command has been found by checking system
's return value like this:
int ret = system("tool");
if (ret != 0) {
std::cout << "tool is not here, move along\n";
}
It is portable in the sense that system is expected to return 0 if all goes well and the command return status is 0 too.
For example, on Linux, running system("non_existing_command")
returns 0x7F00 (same type of value as returned by wait()
).
On Windows, it returns 1 instead.
Upvotes: 1
Reputation: 907
If you use the Qt toolkit, QProcess may help you.
Edit: and look for QProcess::error() return value: if it is QProcess::FailedToStart
, then either it is not installed, or you have insufficient permissions.
Upvotes: 2