c++ - 在非 Cocoa 应用程序中启动应用程序?

标签 c++ macos

能否在非 Cocoa 应用程序中使用 launchApplcation 调用? 我需要 Windows spawnl() 的等价物,它从另一个应用程序中执行一个应用程序。是否有等同于 spawn() 或 exec() 的 OSX?

我可以使用 system() 调用,但我需要向它添加命令行参数。

谢谢!

比尔

最佳答案

您确实有能力 fork 和执行其他进程。

例如:

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

关于c++ - 在非 Cocoa 应用程序中启动应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8086949/

相关文章:

c++ - vector 构造函数的另一个违反直觉的行为

c++ - 跟踪实例化模板类型 C++

C++ 使用文本文件中的数据存储映射对

ios - 比较包含 NSStrings 的 NSDictionaries

c++ - 如何使用 C++ 将一串二进制数转换为带符号的十进制表示形式?

c++ - IMFActivate::ActivateObject 返回错误代码 "CoInitialize has not been called."

objective-c - 如何制作一个出现在全屏窗口顶部的 Cocoa 窗口?

c++ - 需要帮助追踪 MacOS 上函数入口的 EXC_BAD_ACCESS

cocoa - 如何从 CVDisplayLink 回调内部绘制到 NSView 图形上下文?

c++ - 为 Mac 10.9 10.11 下编译