2005/08/06

Passing arguments to Mac applications

I'm building a suite of cooperating, cross-platform GUI applications. They're written in C++, using Qt. I'd like to be able to pass command-line arguments from one app to another, but on OS X I've had trouble doing so.

Mac GUI applications typically are launched via NSWorkspace (or, for non-Cocoa apps like the ones I'm building, via the underlying CoreFoundation functions). They're launched with no argv entries. Arguments typically consist only of pathnames to open or print, etc. Accessing them is no picnic.

The simplest solution I've found uses environment variables to pass the arguments around. It works on Unixes including OS X. It should work on Windows.

One gotcha is that you have to explicitly retrieve the environment arguments when your GUI app starts up, instead of having them initialized for you by the C/C++ runtime.

Another gotcha is namespace collisions. The names of the environment variables conveying the arguments must be chosen carefully to prevent them being used/modified by unrelated applications. Maybe some sort of Java-like "com.dmoonc.my_app_suite..." convention would be appropriate.

Anyway, this is probably good enough for now:

envargs.h


#ifndef ENVARGS_H
#define ENVARGS_H

#include <string>
#include <vector>

typedef std::vector< std::string > ArgList;

// Retrieve and clear the environment arguments
// passed to this process.
// Should be done as soon as possible
// after application launch.
void getEnvArgs(ArgList& args);
ArgList envArgs();

// Set environment arguments for passing
// to a process about to be launched.
void setEnvArgs(ArgList& newArgs);

#endif // ENVARGS_H


envargs.cpp


#include "envargs.h"
#include <iostream>
#include <sstream>
#include <stdlib.h>

using namespace std;

static const string C_NumArgsName("_appsuite_numargs");
static const string C_ArgPrefix("_appsuite_arg_");

void getEnvArgs(ArgList& args)
{
args.clear();

char *numArgsCStr = getenv(C_NumArgsName.c_str());
if (numArgsCStr)
{
istringstream ins(numArgsCStr);
int numArgs;
if (ins >> numArgs)
{
for (int i = 0; i < numArgs; ++i)
{
ostringstream argname;
argname << C_ArgPrefix << i;

const char *argnameCStr = argname.str().c_str();
char *argCStr = getenv(argnameCStr);
if (argCStr)
{
string arg(argCStr);
args.push_back(arg);
}
else
{
args.push_back("");
}
}
}
}
}

ArgList envArgs()
{
ArgList result;
getEnvArgs(result);
return result;
}

static bool cppsetenv(string name, string value)
{
bool result = true;
ostringstream spec;
spec << name << "=" << value;

result = (0 == putenv((char *)(spec.str().c_str())));
if (!result)
{
cerr << "Could not set environment variable:" << spec << endl;
// TO DO: Ditch the bool return value and throw an exception
}
return result;
}

static bool cppsetenv(string name, unsigned int ivalue)
{
ostringstream svalue;
svalue << ivalue;
return cppsetenv(name, svalue.str());
}

void setEnvArgs(ArgList& newArgs)
{
if (cppsetenv(C_NumArgsName, newArgs.size()))
{
for (unsigned int i = 0; i < newArgs.size(); ++i)
{
ostringstream name;
name << C_ArgPrefix << i;
cppsetenv(name.str(), newArgs[i]);
}
}
}

No comments: