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". The code sample in Writing to a Mailslot shows how client application can write to this 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);
}
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).