1

I've asked a related question in (http://stackoverflow.com/questions/10969488/why-does-windows-spawn-process-sometimes-trigger-error-status-sxs-assembly-not-f) but I'm afraid its getting muddled by the complexity of the question, so, here's a very simple version:

Here's an example of calling _spawnvpe, manually passing the PATH value.

It doesn't work. It errors and will not run notepad.

Changing to _spawnv or not passing the PATH value makes it work. However, the documentation of _putenv clearly states the format for an env value is KEY=VALUE.

How do I make it work?

Please be specific, and provide either a diff or a full copy of the code below including a fix.

#include <stdio.h>
#include <windows.h>
#include <process.h>
#include <errno.h>

int main(int argc, char *argv[]) {

  char *path_value;
  char buffer[4000];
  const char *env[2];
  const char *args[1];
  char *command;
  int result;
  intptr_t procHandle;

  path_value = getenv("PATH");
  sprintf(buffer, "PATH=%s", path_value);
  env[0] = buffer;
  env[1] = NULL;

  args[0] = NULL;

  int offset = 0;
  while (env[offset] != NULL) {
    printf("env %d: %s\n", offset, env[offset]);
    ++offset;
  }

  offset = 0;
  while (args[offset] != NULL) {
    printf("arg %d: %s\n", offset, args[offset]);
    ++offset;
  }

  command = "C:\\windows\\system32\\notepad.exe";

  procHandle = _spawnvpe(_P_NOWAIT, command, args, NULL);
  if (procHandle == -1) {
    printf("Failed to invoke command: %s\n", strerror(errno));
    exit(1);
  }

  _cwait(&result, procHandle, 0);
  if (result != 0)
    printf("Command exited with error code %d\n", result);
}
Doug
  • 32,844
  • 38
  • 166
  • 222

1 Answers1

2

It works for me with the following code (only changed lines are shown):

...
const char *args[2];
...
args[0] = "notepad.exe";
args[1] = NULL;
...
procHandle = _spawnvpe(_P_NOWAIT, command, args, env);
...

Visual Studio 2010, Windows HPC Server 2008 R2.

Note that Windows searches for programs AND dynamic libraries in the PATH in contrast to most Unix systems that have separate variables for executable and library paths.

Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186