How can I make a program in c++ that executes another program and receives information from that program on windows?

huangapple go评论83阅读模式
英文:

How can I make a program in c++ that executes another program and receives information from that program on windows?

问题

是的,这在C/C++中是可能的。程序1可以使用系统调用或库函数来执行程序2,并且程序2可以返回一个值,程序1可以捕获并处理它。这种通信方式在操作系统编程和进程控制中很常见。

英文:

I was wondering if it was possible to make a program that executes another program, the program that is being executed returns a value that the first program catches, like if they were communicating, an example of this: Program 1 executes Program 2, then Program 2 returns/sends to Program 1 whatever it has to send and then closes

Is this possible in c/c++ ?

答案1

得分: 3

最简单的方法是使用管道(即 popenpclose)。这里提供了详细的示例。以下代码提供了一个简单的示例。

示例代码

#include <stdio.h>
#include <iostream>

int main(int argc, const char *argv[]) {
    FILE *pipe = popen("ls", "r");

    char c;
    while ((c = fgetc(pipe)) != EOF)
        putchar(c);

    pclose(pipe);

    return 0;
}

输出

CMakeCache.txt
CMakeFiles
CPackConfig.cmake
CPackSourceConfig.cmake
Makefile
...

你可能也想看看这个 答案

英文:

The simplest approach is to use pipes (i.e. popen, pclose). Here is a detailed example. The following code provides a simple example.

Sample Code

#include &lt;stdio.h&gt;
#include &lt;iostream&gt;

int main(int argc, const char *argv[]) {
    FILE *pipe = popen(&quot;ls&quot;, &quot;r&quot;);

    char c;
    while ((c = fgetc(pipe)) != EOF)
        putchar(c);

    pclose(pipe);

    return 0;
}

Output

CMakeCache.txt
CMakeFiles
CPackConfig.cmake
CPackSourceConfig.cmake
Makefile
...

You may also want to look at this answer.

答案2

得分: 1

以下是您要的内容的翻译:

每次执行程序都发生在一个进程内。您的代码可以生成另一个进程,它将成为其子进程,并可以等待其完成执行并获取返回值。我建议您更多地了解进程。实现这一点的方法是使用fork()系统调用。它会创建进程的两个副本,并在两个副本中返回不同的值,以便您可以区分哪个进程是哪个。它在子进程中返回0,在父进程中返回子进程的进程ID。然后,在子进程中,您可以使用exec系列中的一个系统调用,比如execlp(exec系列中的所有函数在您提供给它们的参数方面都不同,我建议您阅读man手册)来执行您的第二个程序。在父进程中,您可以使用wait()系统调用等待子进程完成执行,然后在一个变量中获取它的返回值。下面的代码片段应该有帮助:

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main() {
    pid_t childPid = fork();

    if (childPid == -1) {
        // 处理fork错误
    } else if (childPid == 0) {
        // 子进程的代码
        execlp("program", "program", arg1, arg2, ..., (char*) NULL);
        // 这里的"program"是您要运行的程序的路径
    } else {
        // 父进程的代码
        int status;
        wait(&status);
        if (WIFEXITED(status)) {
            int returnValue = WEXITSTATUS(status);
            // 根据需要使用returnValue
        } else {
            // 子进程异常退出
        }
    }

    return 0;
}
英文:

Every execution of a program happens inside a process. Your code can spawn another process, which will become its child process, and can wait on it to complete execution and get back the return value. I would suggest reading more on processes. How you achieve this is using the fork() system call. It makes two copies of the process and returns different values in both copies so you can distinguish which process is which. It returns 0 in the child process and process id of the child process in the parent process. Then in the child process you can use one of the system calls from the exec family like execlp(all the functions in exec family differ in the arguments you provide to them, I would suggest reading manpages) to execute your second program. In the parent process, you can wait on the child process to finish execution using the wait() system call and then get back it's return value in a variable. The below code snippet should be helpful

#include &lt;sys/types.h&gt;
#include &lt;sys/wait.h&gt;
#include &lt;unistd.h&gt;

int main() {
    pid_t childPid = fork();

if (childPid == -1) {
    // Handle fork error
} else if (childPid == 0) {
    // Code for the child process
    execlp(&quot;program&quot;, &quot;program&quot;, arg1, arg2, ..., (char*) NULL);
    //Program here is the path to the program you want to run
} else {
    // Code for the parent process
    int status;
    wait(&amp;status);
    if (WIFEXITED(status)) {
        int returnValue = WEXITSTATUS(status);
        // Use the returnValue as needed
    } else {
        // Child process exited abnormally
    }
}

return 0;

}

huangapple
  • 本文由 发表于 2023年5月21日 08:42:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定