进程间通信之命名管道

命名管道的关键步骤

  • 【1】创建命名管道
  • 【2】创建事件
  • 【3】连接命名管道
  • 【4】读管道
  • 【5】写管道

进程1(创建命名管道)服务端

void CNamedPipeSrvView::OnPipeCreate() 
{
    // TODO: Add your command handler code here
    hPipe=CreateNamedPipe("\\\\.\\pipe\\MyPipe",
        PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
        0,1,1024,1024,0,NULL);
    if(INVALID_HANDLE_VALUE==hPipe)
    {
        MessageBox("创建命名管道失败!");
        hPipe=NULL;
        return;
    }
    HANDLE hEvent;
    hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
    if(!hEvent)
    {
        MessageBox("创建事件对象失败!");
        CloseHandle(hPipe);
        hPipe=NULL;
        return;
    }
    OVERLAPPED ovlap;
    ZeroMemory(&ovlap,sizeof(OVERLAPPED));
    ovlap.hEvent=hEvent;
    if(!ConnectNamedPipe(hPipe,&ovlap))
    {
        if(ERROR_IO_PENDING!=GetLastError())
        {
            MessageBox("等待客户端连接失败!");
            CloseHandle(hPipe);
            CloseHandle(hEvent);
            hPipe=NULL;
            return;
        }
    }
    if(WAIT_FAILED==WaitForSingleObject(hEvent,INFINITE))
    {
        MessageBox("等待对象失败!");
        CloseHandle(hPipe);
        CloseHandle(hEvent);
        hPipe=NULL;
        return;
    }
    CloseHandle(hEvent);
}

void CNamedPipeSrvView::OnPipeRead() 
{
    // TODO: Add your command handler code here
    char buf[100];
    DWORD dwRead;
    if(!ReadFile(hPipe,buf,100,&dwRead,NULL))
    {
        MessageBox("读取数据失败!");
        return;
    }
    MessageBox(buf);
}

void CNamedPipeSrvView::OnPipeWrite() 
{
    // TODO: Add your command handler code here
    char buf[]="http://www.sunxin.org";
    DWORD dwWrite;
    if(!WriteFile(hPipe,buf,strlen(buf)+1,&dwWrite,NULL))
    {
        MessageBox("写入数据失败!");
        return;
    }
}

进程2(连接命名管道)客户端

void CNamedPipeCltView::OnPipeConnect() 
{
    // TODO: Add your command handler code here
    if(!WaitNamedPipe("\\\\.\\pipe\\MyPipe",NMPWAIT_WAIT_FOREVER))
    {
        MessageBox("当前没有可利用的命名管道实例!");
        return;
    }
    hPipe=CreateFile("\\\\.\\pipe\\MyPipe",GENERIC_READ | GENERIC_WRITE,
        0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
    if(INVALID_HANDLE_VALUE==hPipe)
    {
        MessageBox("打开命名管道失败!");
        hPipe=NULL;
        return;
    }
}

void CNamedPipeCltView::OnPipeRead() 
{
    // TODO: Add your command handler code here
    char buf[100];
    DWORD dwRead;
    if(!ReadFile(hPipe,buf,100,&dwRead,NULL))
    {
        MessageBox("读取数据失败!");
        return;
    }
    MessageBox(buf);
}

void CNamedPipeCltView::OnPipeWrite() 
{
    // TODO: Add your command handler code here
    char buf[]="命名管道测试程序";
    DWORD dwWrite;
    if(!WriteFile(hPipe,buf,strlen(buf)+1,&dwWrite,NULL))
    {
        MessageBox("写入数据失败!");
        return;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值