Search code examples
qt4vista64

QProcess setEnvironment has no effect?


I'm trying to start a program using QProcess (Qt4.4 on vista64bit, developing in MSVC2005). The program is not in the path so I tried to add the directory the program is in to the QProcess environment, in a way similar to what gets demonstrated in the documentation of QProcess::setEnvironment.

Unfortunately the program doesn't get started, apparently because the executable cannot be found in the path. Is this a known issue? Do I do something wrong?

I have verified that the path gets set by printing the QProcess environment after adding the path.

There are two workarounds: the first is to start the program with the full path, the second is to add the directory to the path before starting the executable, but I don't want to use either.


Solution

  • QProcess::setEnvironment() only affects the environment of the process being spawned, not the context in which the spawning is handled. You need to alter the current environment so that the app you're spawning can be found (using ::SetEnvironmentVariable() for starters).

    Application file lookup is outlined in the documentation of CreateProcess API.

    Check this pointer for an example scenario.