开发者

problem generating argv for spawnvp()

I need to run a command using spawnvp(), so I can redirect the output. My problem is, that I don't have argv, but just a string with the whole commnd, so I need to split it. Unfortunately I got an exception when I passed my generated argv into the function.

Doing it this way works:

char* argv[2];
argv[0] = "kzip";
argv[1] = NULL;
开发者_JAVA技巧

This is the way I am doing it, which is failing:

char** argv2 = (char**)malloc(sizeof(char*) * 2);
argv2[0] = "kzip";
argv2[1] = NULL;

This is how I call spawnvp():

hProcess = (HANDLE)spawnvp(P_NOWAIT, argv2[0], (const char* const*)&argv2);

I know there is some difference between a char[] and a char*, but I can't figure out how to create a dynamically created char*[] instead of a char**.


I won't put my hand in the fire for it but you should drop the & from this line.

hProcess = (HANDLE)spawnvp(P_NOWAIT, argv2[0], (const char* const*)&argv2);

argv == &argv but argv2 != &argv2


Your last argument to spawnvp is wrong, it should be just spawnvp(P_NOWAIT, argv2[0], argv2); , and not the address of your pointer.


Using spawnxx() to redirect output is not a good idea. It's better to use popen() and construct the command string to redirect appropriately, e.g. if you would like to redirect stdout and stderr from command.exe into somefile.txt:

popen("command.exe &> somefile.txt")


If you have a shell-style command line, you should spawn with this argv:

(char []){ "sh", "-c", command, 0 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜