c++ pipe for parent-child process communication

Keywords: C++ Programming Windows

1. pipe programming process of parent-child process communication

-Create pipe

-Set the output of the process to the pipeline

-Create process

-Close pipeline write handle

-Read the pipeline read handle, read the data into a buffer

2. Precautions

-When reading the pipeline data, be sure to close the write handle;

-When a parent-child process communicates, the transfer of handles is mostly done by inheritance. The parent process allows these handles to inherit for the child process. When creating a child process, the inherited property should be set to true.

 

 // pdfprintconsole.cpp : This file contains "main" Function. Program execution will start and end here.
//

#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 The pipeline can be inherited by child processes  
    sa.lpSecurityDescriptor = NULL; // Default is 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");
    //Three times in total
    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("Defect pdfprint.exe file",0,0);        
            break;
        }
        else
        {
            HANDLE hProcess = pi.hProcess;
            //Wait for process to exit    //CloseHandle(hPipeOutputRead);
            while (WaitForSingleObject(hProcess, INFINITE) != WAIT_OBJECT_0);
            GetExitCodeProcess(hProcess, &ret);
            //If ret!=0,Abnormal exit;

            //
            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");
}

Posted by GoNz0 on Sat, 19 Oct 2019 07:30:30 -0700