2011-11-10 49 views
2

launchApplcation調用可以在非Cocoa應用中使用嗎? 我需要相當於Windows spawnl(),它從另一個執行應用程序。是否有一個OSX等同於spawn()或exec()?在非Cocoa應用程序中啓動應用程序?

我可以使用system()調用,但我需要向它添加命令行參數。

謝謝!

Bill

回答

0

您確實有能力fork和exec其他進程。

例如:

int myPipe[2]; 
int err, child_pid; 

err = pipe(&myPipe); //creates the pipe - data written to myPipe[1] can be read from myPipe[0] 

child_pid = fork(); 

if(child_pid == 0) 
{ 
    err = dup2(myPipe[1], 1); //set myprogram's standard output to the input of the pipe 

    execl("/path/to/myprogram", "arg1", "arg2"); 
} 

int pipefd = myPipe[0]; 
char buffer[255]; 
err read(pipefd, buffer, 255); // etc etc 

// Ideally you would check that err and child_pid != -1 
+0

我不需要用催生程序進行通信。是否需要fork()? – Bill

+0

可能不是,但我不確定(因爲我被寵壞了可以訪問NSTask和線程等等)。試試execl,看看它是如何發展的。 –

+0

由於某種原因不起作用。我試過:execl(「/ Application/Myprog.app」)和execl(「Macintosh HD/Application/Myprog.app」)任何想法我做錯了什麼? – Bill