进程通信系列-匿名管道
2013-04-18 18:03
375 查看
匿名管道只能在本机由父进程至子进程,优点在于子进程方便重定向,常用于应用程序内部
注意判断此进程是父类还是子类,代码长度一般
匿名管道类
注意判断此进程是父类还是子类,代码长度一般
匿名管道类
#include "stdafx.h" #include "niming.h" #include <iostream> using namespace std; niming::niming(void) { } niming::~niming(void) { } int niming::build() { SECURITY_ATTRIBUTES sa; sa.bInheritHandle=TRUE; sa.lpSecurityDescriptor=NULL; sa.nLength=sizeof(SECURITY_ATTRIBUTES); if(!CreatePipe(&hRead,&hWrite,&sa,0)) { MessageBox(0,"创建失败","error",0); return 0; } STARTUPINFO sui; ZeroMemory(&sui,sizeof(STARTUPINFO)); sui.cb=sizeof(STARTUPINFO); sui.dwFlags=STARTF_USESTDHANDLES; sui.hStdInput=hRead; sui.hStdOutput=hWrite; sui.hStdError=GetStdHandle(STD_ERROR_HANDLE); PROCESS_INFORMATION ProcessInfo; if(CreateProcess("jxyprocess.exe",NULL,NULL,NULL,TRUE,0,NULL,NULL,&sui,&ProcessInfo)) return 1; else return 0; } int niming::send(CString con) { DWORD d; if(WriteFile(hWrite,con,con.GetLength()+1,&d,NULL))return 1; return 0; } CString niming::recv() { DWORD dwRead; char ss[1024]; if(ReadFile(hRead , ss ,1024,&dwRead,NULL))return ss; return ""; } void niming::connect() { hRead=GetStdHandle(STD_INPUT_HANDLE); hWrite=GetStdHandle(STD_OUTPUT_HANDLE); return; } int niming::have() { bool s=cin.eof(); DWORD d; int pid; char ss[1024]; if(!s) { ReadFile(hRead , ss ,1024,&d,NULL); sscanf(ss,"pid-%d",&pid); if(pid==getpid())s=1; WriteFile(hWrite,ss,1024,&d,NULL); } return !s; }
相关文章推荐
- 孙鑫VC学习笔记:第十七讲 用匿名管道实现进程间的通信
- Linux下父子进程匿名管道通信
- [置顶] 【Linux】 进程通信--匿名管道
- Linux进程通信之管道(匿名,命名)
- 进程通信_匿名管道
- 通过匿名管道在进程间双向通信
- 进程通信之二 管道技术第二篇 匿名管道
- linux进程通信之使用匿名管道进行父子进程通信
- Linux(七):进程通信IPC(一)之简单的匿名管道编写
- Linux进程间的通信-基于有序文件(匿名管道)
- 进程通信-匿名管道
- 8、进程通信-匿名管道
- Linux进程通信----匿名管道
- LINUX 进程通信之匿名管道
- Windows进程通信方式之匿名管道
- C++进程通信之匿名管道
- 通过匿名管道在进程间双向通信
- 进程通信系列-命名管道
- C#使用匿名管道在本地进程之间进行通信
- 多进程通信(IPC)--匿名管道和命名管道