使用 C++ 从线程启动应用程序

发布于 2024-10-13 05:28:01 字数 129 浏览 2 评论 0原文

我需要在线程内启动第 3 方程序,等待使用 C++ 从 stdout/stderr 获取结果。

  • 有哪些方法可用?
  • 它们是跨平台的吗?我的意思是,我可以将它们同时用于 cl/gcc 吗?

I need to launch a 3rd party program inside a thread, wait to get the results both from stdout/stderr with C++.

  • What methods are available?
  • Are they cross-platform? I mean, can I use them both for cl/gcc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦明 2024-10-20 05:28:01

http://linux.die.net/man/3/execl

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

void run_process (const char* path){
    pid_t child_pid;

    /* Duplicate this process.  */
    child_pid = fork ();

    if (child_pid != 0){
        /* This is the parent process.  */

        int ret = waitpid(child_pid, NULL, 0);

        if (ret == -1){
            printf ("an error occurred in waitpid\n");
            abort ();
        }
    }
    else {
        execl (path, path);
        /* The execvp function returns only if an error occurs.  */
        printf ("an error occurred in execl\n");
        abort ();
    }

}

在 Unix上 Windows:

http://msdn.microsoft.com /en-us/library/ms682425%28v=vs.85%29.aspx

# include <windows.h>

void run_process (const char* path){
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    bool ret = = CreateProcess(
            NULL,          // No module name (use command line)
            path,          // Command line
            NULL,          // Process handle not inheritable
            NULL,          // Thread handle not inheritable
            false,         // Set handle inheritance to FALSE
            0,             // No creation flags
            NULL,          // Use parent's environment block
            NULL,          // Use parent's starting directory 
            &si,           // Pointer to STARTUPINFO structure
            &pi            // Pointer to PROCESS_INFORMATION structure
        )

    if (!ret){
        printf("Error");
        abort();
    }

    WaitForSingleObject(pi.hProcess, INFINITE);

    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);

}

On Unix:

http://linux.die.net/man/3/execl

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

void run_process (const char* path){
    pid_t child_pid;

    /* Duplicate this process.  */
    child_pid = fork ();

    if (child_pid != 0){
        /* This is the parent process.  */

        int ret = waitpid(child_pid, NULL, 0);

        if (ret == -1){
            printf ("an error occurred in waitpid\n");
            abort ();
        }
    }
    else {
        execl (path, path);
        /* The execvp function returns only if an error occurs.  */
        printf ("an error occurred in execl\n");
        abort ();
    }

}

On Windows:

http://msdn.microsoft.com/en-us/library/ms682425%28v=vs.85%29.aspx

# include <windows.h>

void run_process (const char* path){
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    bool ret = = CreateProcess(
            NULL,          // No module name (use command line)
            path,          // Command line
            NULL,          // Process handle not inheritable
            NULL,          // Thread handle not inheritable
            false,         // Set handle inheritance to FALSE
            0,             // No creation flags
            NULL,          // Use parent's environment block
            NULL,          // Use parent's starting directory 
            &si,           // Pointer to STARTUPINFO structure
            &pi            // Pointer to PROCESS_INFORMATION structure
        )

    if (!ret){
        printf("Error");
        abort();
    }

    WaitForSingleObject(pi.hProcess, INFINITE);

    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);

}
残月升风 2024-10-20 05:28:01

系统应该是平台独立的,但如果担心使用相同的安全权限运行程序,您可能希望坚持使用 createprocess (win)/ exec (other)。

system should be platform independant, but you might want to stick with createprocess (win)/ exec (others) if there is a concern about running the program with the same security privledges.

我不是你的备胎 2024-10-20 05:28:01

有一组 posix 函数可以启动外部可执行文件 - 请参阅 exec - 其中是跨平台的。要在 Windows 上执行某些特定任务,您可能需要使用特定于 Windows 的 创建进程

这些通常会阻塞,因此您必须在新线程中启动它们。线程通常不是跨平台的,尽管您可以在 Windows 上使用 posix (pthreads)。

另一种方法是使用诸如 Qt 或 wxWidgets 跨平台库之类的东西。

There is a set of posix functions to launch an external executable - see exec - which are cross platform. To do some specific tasks on windows you may need to use windows specific createprocess.

These generally block so you would have to start them in a new thread. Threading is generally not cross platform, although you can use posix (pthreads) on windows.

An alternative is to use somthing like Qt or wxWidgets cross platform libraries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文