Listing 2: PipedProcess.cpp : implementation file
#include "stdafx.h" BOOL CPipedProcess::CreateProc(char* ExeFileName) { SECURITY_ATTRIBUTES saAttr; BOOL fSuccess; /* Set the bInheritHandle flag so pipe handles are inherited. */ saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); saAttr.bInheritHandle = TRUE; saAttr.lpSecurityDescriptor = NULL; /* Create a pipe for the child's STDOUT. */ if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, BUFSIZE)) { ErrorExit("Stdout pipe creation failed"); return FALSE; } /* Set a write handle to the pipe to be STDOUT. */ if (! SetStdHandle(STD_OUTPUT_HANDLE, hChildStdoutWr)) { ErrorExit("Redirecting STDOUT failed"); return FALSE; } /* Set a write handle to the pipe to be STDERROR */ if (! SetStdHandle(STD_ERROR_HANDLE, hChildStdoutWr)) { ErrorExit("Redirecting STDOUT failed"); return FALSE; } /* Create a pipe for the child's STDIN. */ if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, BUFSIZE)) { ErrorExit("Stdin pipe creation failed"); return FALSE; } /* Set a read handle to the pipe to be STDIN. */ if (! SetStdHandle(STD_INPUT_HANDLE, hChildStdinRd)) { ErrorExit("Redirecting Stdin failed"); return FALSE; } /* Duplicate the write handle to the pipe so it is not inherited. */ fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, GetCurrentProcess(), &hChildStdinWrDup, 0, FALSE, DUPLICATE_SAME_ACCESS); if (! fSuccess) { ErrorExit("DuplicateHandle failed"); return FALSE; } CloseHandle(hChildStdinWr); /* Now create the child process. */ if (! CreateChildProcess(ExeFileName)) { ErrorExit("Create process failed"); return FALSE; } return TRUE; } void CPipedProcess::CloseChildProcess(void) { CloseHandle(hChildStdinRd); CloseHandle(hChildStdinWrDup)) CloseHandle(hChildStdoutRd)) CloseHandle(hChildStdoutWr)) } BOOL CPipedProcess::CreateChildProcess(char *ChildName) { PROCESS_INFORMATION piProcInfo; STARTUPINFO siStartInfo; char TitleProc [280]; *TitleProc = '\x0'; strcat(TitleProc, "Pipe \x0"); strcat(TitleProc, ChildName); if(strlen(TitleProc) > 20) TitleProc[18] = '\x0'; /* Set up members of STARTUPINFO structure. */ siStartInfo.cb = sizeof(STARTUPINFO); siStartInfo.lpReserved = NULL; siStartInfo.lpReserved2 = NULL; siStartInfo.cbReserved2 = 0; siStartInfo.lpDesktop = NULL; siStartInfo.dwFlags = STARTF_USESHOWWINDOW; siStartInfo.lpTitle = TitleProc; siStartInfo.wShowWindow = SW_MINIMIZE; /* Create the child process. */ return CreateProcess(NULL, ChildName, /* command line */ NULL, /* process security attributes */ NULL, /* primary thread security attributes */ TRUE, /* handles are inherited */ 0, /* creation flags */ NULL, /* use parent's environment */ NULL, /* use parent's current directory */ &siStartInfo, /* STARTUPINFO pointer */ &piProcInfo); /* receives PROCESS_INFORMATION */ } DWORD CPipedProcess::ProcessCommand(CString Command, CString &ResultBuf) { DWORD dwRead; WriteToPipe((char*) LPCTSTR(Command)); dwRead = ReadFromPipe(ResultBuf); return dwRead; } void CPipedProcess::ErrorExit (char *ErrorMsg) { ::MessageBox(::GetActiveWindow(), ErrorMsg, "Pipe Error", MB_OK); CloseChildProcess(); } //End of File