创建 Mailslot

三个专用函数支持 Mailslot: CreateMailslotGetMailslotInfoSetMailslotInfo。 这些函数由 mailslot 服务器使用。

下面的代码示例使用 CreateMailslot 函数检索名为“sample_mailslot”的 mailslot 的句柄。 写入 Mailslot 中的代码示例演示客户端应用程序如何写入此 mailslot。

#include <windows.h>
#include <stdio.h>

HANDLE hSlot;
LPCTSTR SlotName = TEXT("\\\\.\\mailslot\\sample_mailslot");

BOOL WINAPI MakeSlot(LPCTSTR lpszSlotName) 
{ 
    hSlot = CreateMailslot(lpszSlotName, 
        0,                             // no maximum message size 
        MAILSLOT_WAIT_FOREVER,         // no time-out for operations 
        (LPSECURITY_ATTRIBUTES) NULL); // default security
 
    if (hSlot == INVALID_HANDLE_VALUE) 
    { 
        printf("CreateMailslot failed with %d\n", GetLastError());
        return FALSE; 
    } 
    else printf("Mailslot created successfully.\n"); 
    return TRUE; 
}

void main()
{ 
   MakeSlot(SlotName);
}

若要创建可由子进程继承的 mailslot,应用程序应更改作为 CreateMailslot 的最后一个参数传递的SECURITY_ATTRIBUTES结构。 为此,应用程序将此结构的 bInheritHandle 成员设置为 TRUE , (默认设置为 FALSE) 。