Search code examples
cwindowsexec

How does execv does it?


Perhaps this is an issue of magic. I'm trying to create a function that has the following signature(or equivalent const char *const *args)

void ExecuteJavaVM(char** args){
 
}

I want to emulate how execv handles an array of parameters, as you may know execv doesn't requires us to put the max number of elements of the array. It just magically counts the number of elements in the array we pass as its second parameter.

This is the execv signature.

intptr_t _execv( 
   const char *cmdname,
   const char *const *argv 
);

Somehow execv manages to "count" the number of elements in the array argv. This is exactly what I'm trying to do.

sizeof is almost useless, since the datatype of the array decays to a simple pointer, so sizeof will most probably always return 4.

Any ideas? Anyone knows where to find the sources of these functions for Windows?


Solution

  • From the POSIX docs (emphasis mine):

    The argument argv is an array of character pointers to null-terminated strings. The application shall ensure that the last member of this array is a null pointer. These strings shall constitute the argument list available to the new process image. The value in argv[0] should point to a filename that is associated with the process being started by one of the exec functions.