学习VC++深入浅出——匿名管道的使用
HANDLE hWrite;
HANDLE hRead;
void CParentPipeView::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.hStdInput = hWrite;
sui.hStdError = GetStdHandle(STD_ERROR_HANDLE);
if(!CreateProcess("../ChildPipe/Debug/ChildPipe.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 CParentPipeView::OnPipeWrite()
... {
// TODO: Add your command handler code here
char buf[]="http://zhangdali.org";
DWORD dwWrite;
if(!WriteFile(hWrite,buf,strlen(buf)+1,&dwWrite,NULL))
...{
MessageBox("写入数据失败!");
return ;
}
}
void CParentPipeView::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 CChildPipeView::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 CChildPipeView::OnPipeWrite()
... {
// TODO: Add your command handler code here
char buf[]= "匿名管道测试程序";
DWORD dwWrite;
if(!WriteFile(hWrite,buf,strlen(buf)+1,&dwWrite,NULL))
...{
MessageBox("写入数据失败!");
return ;
}
}
HANDLE hRead;
void CParentPipeView::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.hStdInput = hWrite;
sui.hStdError = GetStdHandle(STD_ERROR_HANDLE);
if(!CreateProcess("../ChildPipe/Debug/ChildPipe.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 CParentPipeView::OnPipeWrite()
... {
// TODO: Add your command handler code here
char buf[]="http://zhangdali.org";
DWORD dwWrite;
if(!WriteFile(hWrite,buf,strlen(buf)+1,&dwWrite,NULL))
...{
MessageBox("写入数据失败!");
return ;
}
}
void CParentPipeView::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 CChildPipeView::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 CChildPipeView::OnPipeWrite()
... {
// TODO: Add your command handler code here
char buf[]= "匿名管道测试程序";
DWORD dwWrite;
if(!WriteFile(hWrite,buf,strlen(buf)+1,&dwWrite,NULL))
...{
MessageBox("写入数据失败!");
return ;
}
}