C++,在windows下调用子进程,并获得子进程的返回值 (在Qt平台实现)

copy自

c++,在windows下调用子进程,并获得子进程的返回值。

只是为了自己学习整理一下。

侵删

被调用的进程

在windows下,将子进程函数(subapp.cpp)封装成exe。封装成exe的方法见打包的文章。

subapp.cpp

代码:
我注释掉了 getchar(); 这样不会卡在子进程。
//主线程传入的类型是 char* 类型的,如果需要传入浮点型或者string 类型,就需要 ,char[] 转 string

#pragma execution_character_set("utf-8")
#include
#include
using namespace std;
int main(int argc,char* argv[])
{
    std::cout << "this is subApp messagez" << std::endl;

    std::cout << "argc"<= 2)
    {
        std::cout << "work... pass,the return code will be 0" << std::endl;
        char a = *argv[1];//传进来的是 -1 为什么是符号
        char b = *argv[2];
        char c = *argv[3];
        string c1 = argv[3];
        char d = *argv[4];
        // char*转换为string
        char e = *argv[5];
        string st1 = argv[5];
//        QString qstr;
//        qstr = QString::fromStdString(str1);
        std::cout <

写好代码后,直接运行会显示 "work... failed,the return code will be 1",因为总参数数量小于2。将其打包成exe,拷贝出其所需要的dll库。放在工程文件夹中。(Qt工程的debug/release文件夹中)。

主进程

在主进程中,使用for循环,调用子进程。这里for循环5次,也就是调用5次子进程。

main.cpp

代码:

#include
#include
int main(int argc, char*argv[])
{
   for(int i = 0 ;i<5 ; i++){
   STARTUPINFO si = { sizeof(STARTUPINFO) };//在产生子进程时,子进程的窗口相关信息
   PROCESS_INFORMATION pi;                  //子进程的ID/线程相关信息
   DWORD returnCode;//用于保存子程进的返回值;

   wchar_t commandLine1[] = L"subapp.exe -l 1 hello 3 www.helllp=.cn";  //测试命令行参数一
   wchar_t commandLine2[] = L"subapp.exe";     //测试命令行参数二

   //
   BOOL bRet = CreateProcess(              //调用失败,返回0;调用成功返回非0;
       NULL,                               //一般都是空;(另一种批处理情况:此参数指定"cmd.exe",下一个命令行参数 "/c otherBatFile")
       commandLine1,                       //命令行参数
       NULL,                               //_In_opt_    LPSECURITY_ATTRIBUTES lpProcessAttributes,
       NULL,                               //_In_opt_    LPSECURITY_ATTRIBUTES lpThreadAttributes,
       FALSE,                              //_In_        BOOL                  bInheritHandles,
       CREATE_NEW_CONSOLE,                 //新的进程使用新的窗口。
       NULL,                               //_In_opt_    LPVOID                lpEnvironment,
       NULL,                               //_In_opt_    LPCTSTR               lpCurrentDirectory,
       &si,                                //_In_        LPSTARTUPINFO         lpStartupInfo,
       &pi);                               //_Out_       LPPROCESS_INFORMATION lpProcessInformation

   if (bRet)
   {
       std::cout << "process is running..." << std::endl;
       //等待子进程结束
       WaitForSingleObject(pi.hProcess, -1);
       std::cout << "process is finished"  << std::endl;
       //获取子进程的返回值
       GetExitCodeProcess(pi.hProcess, &returnCode);
       std::cout << "process return code:" << returnCode << std::endl;
   }
   else
   {
       std::cout << "Create Process error!"<

直接运行主进程,会出现五个框。显示

process is running...
process is finished
process return code:0
process is running...
process is finished
process return code:0
process is running...
process is finished
process return code:0
process is running...
process is finished
process return code:0
process is running...
process is finished
process return code:0

也就是调用了5次子进程。

你可能感兴趣的:(Qt)