HANDLE hReadPipe; HANDLE hWritePipe; void CChildView::OnPipeCreate() { SECURITY_ATTRIBUTES sa; sa.bInheritHandle = TRUE; sa.lpSecurityDescriptor = NULL; sa.nLength = sizeof(SECURITY_ATTRIBUTES); if (!CreatePipe(&hReadPipe, &hWritePipe, &sa, 0)) { MessageBox(_T("匿名管道创建失败")); return; } STARTUPINFO strStartupInfo; memset(&strStartupInfo, 0, sizeof(strStartupInfo)); strStartupInfo.cb = sizeof(strStartupInfo); strStartupInfo.dwFlags = STARTF_USESTDHANDLES; strStartupInfo.hStdInput = hReadPipe; strStartupInfo.hStdOutput = hWritePipe; strStartupInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE); PROCESS_INFORMATION szProcessInformation; memset(&szProcessInformation, 0, sizeof(szProcessInformation)); int iRet = CreateProcess( _T("MailSlotClient.exe"), NULL, NULL, NULL, TRUE, 0, NULL, NULL, &strStartupInfo, &szProcessInformation ); if (iRet) { CloseHandle(szProcessInformation.hProcess); CloseHandle(szProcessInformation.hThread); szProcessInformation.dwProcessId = 0; szProcessInformation.dwThreadId = 0; szProcessInformation.hThread = NULL; szProcessInformation.hProcess = NULL; } else { CloseHandle(hReadPipe); CloseHandle(hWritePipe); hReadPipe = NULL; hWritePipe = NULL; MessageBox(_T("创建子进程失败")); return; } } void CChildView::OnPipeRead() { char szBuf[100] = { 0 }; DWORD dwRead; TRACE("Begin ReadFile"); if (!ReadFile(hReadPipe, szBuf, 100, &dwRead, NULL)) { MessageBox(_T("读取数据失败")); return; } TRACE("End PipeReadFile"); MessageBox((CStringW)szBuf); } void CChildView::OnPipeWrite() { char szBuf[] = "Bingo hen shuai"; DWORD dwWrite; if (!WriteFile(hWritePipe, szBuf, strlen(szBuf) + 1, &dwWrite, NULL)) { MessageBox(_T("写入数据失败")); return; } }
|