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

Deadly 提交于 2019-12-12 02:48:31

问题


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:


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


来源:https://stackoverflow.com/questions/9110445/execute-external-program-with-specific-parameters-from-windows-c-c-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!