0

I want to call Program1 from Program2 with exact same parameters which I called Program2 with. In Linux, I can do it like this:

int main(char argc, char* argv[]){
execv("./Program1", argv); 
}

In windows, I tried CreateProcess

but as the first post says there is potential issue: "argv[0] Doesn't Contain the Module Name as Expected". I do want to send proper argv[0] to Program1. What should I do?

1
  • On Windows, it's called _execv. Commented Feb 2, 2012 at 10:34

1 Answer 1

1

argv[0] is the name of the program itself.

You should do :

int main(char argc, char **argv)
{
  char* argvForProgram1[] = { "./Program1", 0 }
  execv(argvForProgram1[0], argvForProgram1);
}

or to keep your previous args :

int main(char argc, char **argv)
{
  char** argvForProgram1 = argv;
  argvForProgram1[0] = "./Program1";
  execv(argvForProgram1[0], argvForProgram1);
}

Using execve is better too because you keep the environment:

int main(char argc, char **argv, char **envp)
{
  char** argvForProgram1 = argv;
  argvForProgram1[0] = "./Program1";
  execve(argvForProgram1[0], argvForProgram1, envp);
}
Sign up to request clarification or add additional context in comments.

1 Comment

My bad. I thought execv is linux-specific function, but it is POSIX.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.