ov7a
ov7a

Reputation: 1594

Execute external program with specific parameters from windows c/c++ code

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?

Upvotes: 0

Views: 767

Answers (1)

Eregrith
Eregrith

Reputation: 4366

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);
}

Upvotes: 1

Related Questions