vc 匿名管道 出错

来源:百度知道 编辑:UC知道 时间:2024/06/30 16:08:09
父进程:
#include "stdio.h"
#include "iostream.h"
#include "windows.h"
#include "process.h"
#include "tchar.h"
const int BUF_SIZE=1000;
void main()
{
HANDLE PipeReadHandle;
HANDLE PipeWriteHandle;
PROCESS_INFORMATION PrdcessInfo;
SECURITY_ATTRIBUTES SecurityAttributes;
STARTUPINFO StartupInfo;
BOOL Success;
ZeroMemory(&StartupInfo,sizeof(StartupInfo));
ZeroMemory(&PrdcessInfo,sizeof(PrdcessInfo));
ZeroMemory(&SecurityAttributes,sizeof(SecurityAttributes));
SecurityAttributes.nLength=sizeof(SECURITY_ATTRIBUTES);
SecurityAttributes.bInheritHandle=TRUE;
SecurityAttributes.lpSecurityDescriptor=NULL;
Success=CreatePipe(&PipeReadHandle,&PipeWriteHandle,&SecurityAttributes,0);
if (!Success)
{
cout<<"error creating pipe"<<endl;
return;
}
StartupInfo.cb=sizeof(STARTUP

这么理解管道不正确。
应该是
1. 父向子管道写数据之后,关闭管道(写一个 FEOF)
2. 子收到数据完成后,再向父写数据,并关闭管道
3. 父收到数据后,显示,关闭所有句柄,结束

比如控制台命令C:\>
C:\>DIR 可以显示当前目录
C:\>DIR | MORE 逐页显示目录,就是DIR先产生数据,通过匿名管道送给MORE程序,MORE显示在控制台上,等待用户翻页,如果DIR完成,则MORE收到匿名管道FEOF数据,关闭管道并退出

这个命令里是先启动的DIR(父),后启动MORE(子),DIR其实已经一口气把所有内容都写入管道了,并且写完后关闭了管道。MORE取到数据,然后执行完毕,退出。