c ++ pipe realize his son-process communication

1, the parent and child communication pipe programming process

- to create a pipe

- output settings to process piping

- create a process

- closed pipe write handle

- Reading pipe read handle, to read a data buffer in

2, Notes

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

- When the parent and child communication, pass the handle of multi done by inheritance, the parent process to allow them to handle child processes inherit; create a child process, whether inherited property to be set to true ;

 

// pdfprintconsole.cpp: This file contains the "main" function. Program execution will begin and end here.
//
  #include " pch.h " #include <the iostream> #include <the 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 ; SA = {the SECURITY_ATTRIBUTES0 }; 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"); }

 

Guess you like

Origin www.cnblogs.com/freedomworld/p/11703447.html