欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c++ pipe实现父子进程通信

程序员文章站 2022-05-31 18:53:52
1、父子进程通信pipe编程流程 -创建管道 -设置进程的输出到管道 -创建进程 -关闭管道写句柄 -读管道读句柄,把数据读到一个buffer里 2、注意事项 -读管道数据的时候,一定要关闭写句柄; -父子进程通信时,句柄的传递多通过继承来完成,父进程允许这些句柄为子进程继承;创建子进程,是否继承的 ......

1、父子进程通信pipe编程流程

-创建管道

-设置进程的输出到管道

-创建进程

-关闭管道写句柄

-读管道读句柄,把数据读到一个buffer

2、注意事项

-读管道数据的时候,一定要关闭写句柄;

-父子进程通信时,句柄的传递多通过继承来完成,父进程允许这些句柄为子进程继承;创建子进程,是否继承的属性要设置为true

 

 // pdfprintconsole.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <iostream>
#include <windows.h>
#include<tchar.h>
#include <string.h>

int main()
{



    int page_index = 0;
    char currentbuff[1000] = { 0 };
    char cachfilename[1000] = { 0 };
    char printcommand[1000] = { 0 };
    char command[500] = { 0 };
    handle handle = 0;
    bool                btest = 0;
    security_attributes sa = { 0 };
    dword               dwnumberofbytesread = 0;
    char szbuffer[10000] = { 0 };
    dword ret = 0;
    handle   hpipeoutputread = null;
    handle    hpipeoutputwrite = null;
    
    startupinfoa si = { 0 };
    process_information pi = { 0 };    

    sa.binherithandle = true; // true为管道可以被子进程所继承  
    sa.lpsecuritydescriptor = null; // 默认为null
    sa.nlength = sizeof(security_attributes);
    // create pipe for standard output redirection.
    createpipe(&hpipeoutputread,  // read handle
        &hpipeoutputwrite, // write handle
        &sa,      // security attributes
        0      // number of bytes reserved for pipe - 0 default
    );
    // make child process use hpipeoutputwrite as standard out,
        // and make sure it does not show on screen.
    si.cb = sizeof(si);
    si.dwflags = startf_useshowwindow | startf_usestdhandles;
    si.wshowwindow = sw_hide;
    //si.hstdinput = hpipeinputread;
    si.hstdoutput = hpipeoutputwrite;
    si.hstderror = hpipeoutputwrite;
    //strcpy_s(command, " -printer \"fx docucentre s2011\" -paper 9 -printermargins c:\\users\\qj\\desktop\\f3044688ce88a4b0a78c16ba85076570-5378-0010-0.png");
    strcpy_s(command," -printer \"fx docucentre s2011\" -listpapers");
    //一共执行三次
    for (int i = 0; i < 3; i++)
    {

        if (!createprocessa("c:\\users\\qj\\source\\repos\\windowsformsapp1\\x64\\debug\\pdfprint.exe", command, null, null, true, null, null, null, &si, &pi))
        {
            //afxmessagebox("缺失pdfprint.exe文件",0,0);        
            break;
        }
        else
        {
            handle hprocess = pi.hprocess;
            //等待进程退出    //closehandle(hpipeoutputread);
            while (waitforsingleobject(hprocess, infinite) != wait_object_0);
            getexitcodeprocess(hprocess, &ret);
            //如果ret!=0,异常退出;

            //
            closehandle(hpipeoutputwrite);
            while (true)
            {
                btest = readfile(
                    hpipeoutputread,      // handle of the read end of our pipe
                    &szbuffer,            // address of buffer that receives data
                    sizeof(szbuffer),                  // number of bytes to read
                    &dwnumberofbytesread, // address of number of bytes read
                    null                  // non-overlapped.
                );
            
                if (!btest)
                {
                    break;
                }
                // do something with data.
                szbuffer[dwnumberofbytesread] = 0;  // null terminate
            }
            if (!ret)
            {
                printf("123%s456\nbtest:%d\n", szbuffer, btest);
                closehandle(hprocess);            
                closehandle(hpipeoutputread);
                break;
            }




        }
    }
    //std::cout << "hello world!\n"; 
    system("pause");
}