进程间通信之匿名管道
来源:互联网 发布:java怎么做游戏 编辑:程序博客网 时间:2024/05/17 21:48
关键步骤
- 【1】创建管道(一定是主进程创建)
- 【2】创建子进程
- 【3】两个进程都有读写匿名管道的功能
主进程的主要处理函数(MFC)
void CParentView::OnPipeCreate() { // TODO: Add your command handler code here SECURITY_ATTRIBUTES sa; sa.bInheritHandle=TRUE; sa.lpSecurityDescriptor=NULL; sa.nLength=sizeof(SECURITY_ATTRIBUTES); if(!CreatePipe(&hRead,&hWrite,&sa,0)) { MessageBox("创建匿名管道失败!"); return; } STARTUPINFO sui; PROCESS_INFORMATION pi; ZeroMemory(&sui,sizeof(STARTUPINFO)); sui.cb=sizeof(STARTUPINFO); sui.dwFlags=STARTF_USESTDHANDLES; sui.hStdInput=hRead; sui.hStdOutput=hWrite; sui.hStdError=GetStdHandle(STD_ERROR_HANDLE); if(!CreateProcess("..\\Child\\Debug\\Child.exe",NULL,NULL,NULL, TRUE,0,NULL,NULL,&sui,&pi)) { CloseHandle(hRead); CloseHandle(hWrite); hRead=NULL; hWrite=NULL; MessageBox("创建子进程失败!"); return; } else { CloseHandle(pi.hProcess); CloseHandle(pi.hThread); }}void CParentView::OnPipeRead() { // TODO: Add your command handler code here char buf[100]; DWORD dwRead; if(!ReadFile(hRead,buf,100,&dwRead,NULL)) { MessageBox("读取数据失败!"); return; } MessageBox(buf);}void CParentView::OnPipeWrite() { // TODO: Add your command handler code here char buf[]="http://www.sunxin.org"; DWORD dwWrite; if(!WriteFile(hWrite,buf,strlen(buf)+1,&dwWrite,NULL)) { MessageBox("写入数据失败!"); return; }}
子进程的主要处理函数(MFC)
void CChildView::OnPipeRead() { // TODO: Add your command handler code here char buf[100]; DWORD dwRead; if(!ReadFile(hRead,buf,100,&dwRead,NULL)) { MessageBox("读取数据失败!"); return; } MessageBox(buf);}void CChildView::OnPipeWrite() { // TODO: Add your command handler code here char buf[]="匿名管道测试程序"; DWORD dwWrite; if(!WriteFile(hWrite,buf,strlen(buf)+1,&dwWrite,NULL)) { MessageBox("写入数据失败!"); return; }}void CChildView::OnInitialUpdate() { CView::OnInitialUpdate(); // TODO: Add your specialized code here and/or call the base class hRead=GetStdHandle(STD_INPUT_HANDLE); hWrite=GetStdHandle(STD_OUTPUT_HANDLE);}
0 0
- 进程间通信之匿名管道通信
- linux进程间通信之匿名管道
- Python进程间通信之匿名管道
- Linux进程间通信之匿名管道
- 进程间通信之匿名管道
- 进程间通信之--匿名管道
- 进程间通信之匿名管道
- 进程间通信之管道通信(匿名管道)
- 进程间通信之匿名管道和命名管道
- Linux--进程间通信之匿名管道及命名管道
- 进程间通信-匿名管道
- 进程间通信 - 匿名管道
- 进程间通信匿名管道
- 进程间通信-匿名管道
- 进程间通信:匿名管道通信
- linux进程间通信方式之匿名管道
- linux进程间通信方式之匿名管道
- 孙鑫 第十七课进程间通信之二 匿名管道
- 【GDB调试学习笔记】GDB查看运行时数据
- java笔记--day09--关于多态的两个案例(猫狗和老师学生)
- [leetcode] 1. Two Sum
- 入木三分,一针见血
- 意识不仅存在人类---疯狂的想法
- 进程间通信之匿名管道
- Factorized Graph Matching
- SDWebImage原理学习
- tomcat配置虚拟目录
- Linux的粘滞位
- MySQL两种存储引擎
- 自定义ListView下拉刷新上拉加载功能(面试)
- Android_OkHttp+Picasso+RecyclerView实现网络图片下载瀑布流
- JAVA 学习模块六: 面向对象