接受端源代码:
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);
}