MFC 应用程序中使用管道代码示意


STARTUPINFO sinf = {0};
PROCESS_INFORMATION pinf = {0};
SECURITY_ATTRIBUTES sa = {0};
HANDLE hPipeORead = NULL;
HANDLE hPipeOWrite = NULL;
HANDLE hPipeIRead = NULL;
HANDLE hPipeIWrite = NULL;

sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;

CreatePipe(&hPipeORead, &hPipeOWrite, &sa, 0);
CreatePipe(&hPipeIRead, &hPipeIWrite, &sa, 0);

sinf.cb = sizeof(sinf);
sinf.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
sinf.wShowWindow = SW_HIDE;
sinf.hStdInput = hPipeIRead;
sinf.hStdOutput = hPipeOWrite;
sinf.hStdError = hPipeOWrite;

char szBuf[] = {"python \"E:\\WorkSpace\\TestData\\iLog\\utils_ilog_info_from_bugzilla_id.py\" 432686"};
if (!CreateProcess(NULL, szBuf, NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &sinf, &pinf))
{
MessageBox("Create Process Error!");
return;
}

std::auto_ptr spBuf(new BYTE[1024 * 512]); // 512K
DWORD dwReadBytes = 0;
if (!ReadFile(hPipeORead, spBuf.get(), 1024 * 512, &dwReadBytes, NULL))
{
CloseHandle(hPipeOWrite);
CloseHandle(hPipeORead);

CloseHandle(hPipeIWrite);
CloseHandle(hPipeIRead);
return;
}

CString strResult((char*)spBuf.get());

CloseHandle(hPipeOWrite);
CloseHandle(hPipeORead);

CloseHandle(hPipeIWrite);
CloseHandle(hPipeIRead);

相关