邮槽使用例子

本文介绍了使用Windows邮件槽(mailslot)进行进程间通信的方法。通过示例代码展示了如何创建邮件槽并从邮件槽读取数据(接受端)以及如何向邮件槽写入数据(发送端)。

接受端源代码:

void CMailslotSrvView::OnMailslotsrv()
{
 // TODO: Add your command handler code here
 HANDLE hMailslot = NULL;
 char buf[100] = "/0";
 DWORD dwRead  = 0;

 hMailslot = CreateMailslot("////.//mailslot//mymailslot", 0, MAILSLOT_WAIT_FOREVER, NULL);

 if (INVALID_HANDLE_VALUE == hMailslot)
 {
  MessageBox("create mail slot failed...");

  return;
 }

  if (!ReadFile(hMailslot, buf, 100, &dwRead, NULL))
 {
  MessageBox("read data failed");

  return;
 }

 MessageBox(buf);

 CloseHandle(hMailslot);
}

发送端源代码: 


void CMailSlotSendView::OnMailslotsend()
{
 // TODO: Add your command handler code here

 HANDLE hMailslot = NULL;
 char buf[]  = "hello, andylin!/r/nI love my baby so much!";
 DWORD dwWrite  = 0;

        hMailslot = CreateFile("////.//mailslot//mymailslot", GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

 if (hMailslot == INVALID_HANDLE_VALUE)
 {
  MessageBox("open mail slot failed...");

  return;
 }

 if (!WriteFile(hMailslot, buf, strlen(buf) + 1, &dwWrite, NULL))
 {
  MessageBox("Write file failed...");
  CloseHandle(hMailslot);

  return;
 }

 CloseHandle(hMailslot);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值