0x01 核心代码
父进程: HANDLE hRead; HANDLE hWrite; SECURITY_ATTRIBUTES sa; sa.bInheritHandle = TRUE; sa.lpSecurityDescriptor = NULL; sa.nLength = sizeof(SECURITY_ATTRIBUTES); if(!CreatePipe(&hRead,&hWrite,&sa,0)) { MessageBox(0,TEXT("创建匿名管道失败!"),TEXT("Error"),MB_OK); } STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory(&si,sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); si.dwFlags = STARTF_USESTDHANDLES; si.hStdInput = hRead; si.hStdOutput = hWrite; si.hStdError = GetStdHandle(STD_ERROR_HANDLE); if(!CreateProcess("E:\\Project\\zzzzzzz\\Debug\\zzzzzzz.exe",NULL,NULL,NULL,TRUE,0,NULL,NULL,&si,&pi)) { CloseHandle(hRead); CloseHandle(hWrite); hRead = NULL; hWrite = NULL; MessageBox(0,TEXT("创建子进程失败!"),TEXT("Error"),MB_OK); } else { CloseHandle(pi.hProcess); CloseHandle(pi.hThread); } //写数据 TCHAR szBuffer[] = "http:\\www.dtdebug.com"; DWORD dwWrite; if(!WriteFile(hWrite,szBuffer,strlen(szBuffer)+1,&dwWrite,NULL)) { MessageBox(0,TEXT("写数据失败!"),TEXT("Error"),MB_OK); } //读数据 TCHAR szBuffer[100]; DWORD dwRead; if(!ReadFile(hRead,szBuffer,100,&dwRead,NULL)) { MessageBox(NULL,TEXT("读取数据失败!"),TEXT("Error"),MB_OK); } else { MessageBox(NULL,szBuffer,TEXT("[读取数据]"),MB_OK); } 子进程: //初始化 HANDLE hRead = GetStdHandle(STD_INPUT_HANDLE); HANDLE hWrite = GetStdHandle(STD_OUTPUT_HANDLE); //读数据 TCHAR szBuffer[100]; DWORD dwRead; if(!ReadFile(hRead,szBuffer,100,&dwRead,NULL)) { MessageBox(NULL,TEXT("读取数据失败!"),TEXT("Error"),MB_OK); } else { MessageBox(NULL,szBuffer,TEXT("[读取数据]"),MB_OK); } //写数据 TCHAR szBuffer[100] = "匿名管道"; DWORD dwWrite; if(!WriteFile(hWrite,szBuffer,strlen(szBuffer)+1,&dwWrite,NULL)) { MessageBox(NULL,TEXT("写入数据失败!"),TEXT("Error"),MB_OK); }
0x02 项目代码下载
0x03 测试出现的问题
父进程读取内容,一直显示卡死状态,无法读取!
待解决
0则评论给“进程通信 - 匿名管道”