使用C+;从线程启动应用程序+; 我需要在一个线程中启动一个第三方程序,等待从STDUT/STDER中得到结果,用C++。 有哪些方法可用 他们是跨站台的吗?我的意思是,我可以在cl/gcc中同时使用它们吗

使用C+;从线程启动应用程序+; 我需要在一个线程中启动一个第三方程序,等待从STDUT/STDER中得到结果,用C++。 有哪些方法可用 他们是跨站台的吗?我的意思是,我可以在cl/gcc中同时使用它们吗,c++,process,launching-application,C++,Process,Launching Application,有一组posix函数用于启动外部可执行文件(请参阅),它们是跨平台的。要在windows上执行某些特定任务,可能需要使用windows特定的 这些线程通常会阻塞,因此您必须在新线程中启动它们。线程通常不是跨平台的,尽管您可以在windows上使用posix(pthreads) 另一种方法是使用类似于或wxWidgets的跨平台库。系统应该是独立于平台的,但如果您担心使用相同的安全特权运行程序,则可能需要使用createprocess(win)/exec(其他)。在Unix上: #包括 #包括

有一组posix函数用于启动外部可执行文件(请参阅),它们是跨平台的。要在windows上执行某些特定任务,可能需要使用windows特定的

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


另一种方法是使用类似于或wxWidgets的跨平台库。

系统应该是独立于平台的,但如果您担心使用相同的安全特权运行程序,则可能需要使用createprocess(win)/exec(其他)。

在Unix上:

#包括
#包括
无效运行_进程(常量字符*路径){
pid_t child_pid;
/*重复此过程*/
child_pid=fork();
如果(子pid!=0){
/*这是父进程*/
int-ret=waitpid(child_-pid,NULL,0);
如果(ret==-1){
printf(“waitpid中发生错误”);
中止();
}
}
否则{
execl(路径,路径);
/*execvp函数仅在发生错误时返回*/
printf(“execl中发生错误”);
中止();
}
}
在Windows上:

#包括
无效运行_进程(常量字符*路径){
STARTUPINFO si;
处理信息;
零内存(&si,sizeof(si));
si.cb=sizeof(si);
零内存(&pi,sizeof(pi));
bool ret==CreateProcess(
NULL,//没有模块名称(使用命令行)
路径,//命令行
NULL,//进程句柄不可继承
NULL,//线程句柄不可继承
false,//将句柄继承设置为false
0,//没有创建标志
NULL,//使用父级的环境块
NULL,//使用父级的起始目录
&si,//指向STARTUPINFO结构的指针
&pi//指向进程信息结构的指针
)
如果(!ret){
printf(“错误”);
中止();
}
WaitForSingleObject(pi.hProcess,无限);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
似乎在谈论同一件事。。。
#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 ();
    }

}
# 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);

}