命名管道例子

Server:

// CNamedPipeClientView 消息处理程序

void CNamedPipeClientView::OnNamedpipeConnect()
{
    
// TODO: Add your command handler code here

    if (!WaitNamedPipe("//./pipe/mypipe", NMPWAIT_WAIT_FOREVER))
    {
        MessageBox(
"there is no usable named pipe"
);

        
return
;
    }

    m_hPipe 
= CreateFile("//./pipe/mypipe", GENERIC_READ | GENERIC_WRITE, 0
, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    
if (INVALID_HANDLE_VALUE ==
 m_hPipe)
    {
        MessageBox(
"open pipe failed!"
);

        
return
;
    }
}

void
 CNamedPipeClientView::OnNamedpipeRead()
{
    
// TODO: Add your command handler code here

    char    buf[100]    = "

 client:

void CNamePipeSrvView::OnNamepipeCreate()
{
    
// TODO: Add your command handler code here

    m_hPipe = ::CreateNamedPipe("//./pipe/MyPipe", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, 01102410240, NULL);

    
if (INVALID_HANDLE_VALUE ==
 m_hPipe)
    {
        MessageBox(
"Create named pipe failed!"
);

        
return
;
    }

    HANDLE    hEvent    
=
 NULL;

    hEvent 
=
 CreateEvent(NULL, TRUE, FALSE, NULL);
    
    
if (!
hEvent)
    {
        MessageBox(
"Create event failed!"
);

        
return
;
    }

    OVERLAPPED    ovlap;

    ZeroMemory(
&ovlap, sizeof
(OVERLAPPED));
    ovlap.hEvent 
=
 hEvent;

    
if (!ConnectNamedPipe(m_hPipe, &
ovlap))
    {
        
if (ERROR_IO_PENDING !=
 GetLastError())
        {
            MessageBox(
"wait for client failed"
);

            CloseHandle(m_hPipe);
            CloseHandle(hEvent);

            
return
;
        }
    }

    
if (WAIT_FAILED ==
 WaitForSingleObject(hEvent, INFINITE))
    {
        MessageBox(
"wait for object failed!"
);

        CloseHandle(m_hPipe);
        m_hPipe 
=
 NULL;

        CloseHandle(hEvent);
        hEvent 
=
 NULL;

        
return
;
    }

        CloseHandle(hEvent);
}

void
 CNamePipeSrvView::OnNamepipeRead()
{
    
// TODO: Add your command handler code here

    char    buf[100]    = "

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值