How to use _spawn or _exec for bootstrapping?
-
21-09-2019 - |
Question
After writing the following program, it does not appear to pass arguments to the called application. While researching _spawnv and what it can do, _execvp was found as what appeared to be a suitable alternative. Does anyone see the problem in the source code and know what needs to be done to fix it?
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
int main(int argc, char** argv)
{
int index;
char** args;
args = (char**) malloc((argc + 1) * sizeof(char*));
args[0] = "boids.py";
for (index = 1; index < argc; index++)
{
args[index - 1] = argv[index];
}
args[argc] = NULL;
return _execvp("python", args);
}
Solution
The first argument in the argv
vector is conventionally the fully qualified name of the executable to be started:
The _spawnv, _spawnve, _spawnvp, and _spawnvpe calls are useful when there is a variable number of arguments to the new process. Pointers to the arguments are passed as an array, argv. The argument argv[0] is usually a pointer to a path in real mode or to the program name in protected mode, and argv1 through argv[n] are pointers to the character strings forming the new argument list. The argument argv[n +1] must be a NULL pointer to mark the end of the argument list.
(From MSDN)
Likewise:
The _execv, _execve, _execvp, and _execvpe calls are useful when the number of parameters to the new process is variable. Pointers to the parameters are passed as an array, argv. The parameter argv[0] is usually a pointer to cmdname. The parameters argv1 through argv[n] point to the character strings forming the new parameter list. The parameter argv[n+1] must be a NULL pointer to mark the end of the parameter list.
(MSDN)