Creating a Mailslot

Mailslots are supported by three specialized functions: CreateMailslot, GetMailslotInfo, and SetMailslotInfo. These functions are used by the mailslot server.

The following code sample uses the CreateMailslot function to retrieve the handle to a mailslot named sample_mailslot.

BOOL FAR PASCAL Makeslot(HWND hwnd, HDC hdc) 
{ 
    LPSTR lpszSlotName = "\\\\.\\mailslot\\sample_mailslot"; 
 
    // The mailslot handle "hSlot1" is declared globally. 
 
    hSlot1 = CreateMailslot(lpszSlotName, 
        0,                             // no maximum message size 
        MAILSLOT_WAIT_FOREVER,         // no time-out for operations 
        (LPSECURITY_ATTRIBUTES) NULL); // no security attributes 
 
    if (hSlot1 == INVALID_HANDLE_VALUE) 
    { 
        ErrorHandler(hwnd, "CreateMailslot"); // local error handler 
        return FALSE; 
    } 
 
    TextOut(hdc, 10, 10, "CreateMailslot successful.", 26); 
    return TRUE; 
} 
 

To create a mailslot that can be inherited by child processes, an application should change the SECURITY_ATTRIBUTES structure passed as the last parameter of CreateMailslot. To do this, the application sets the bInheritHandle member of this structure to TRUE (the default setting is FALSE).