| Platform SDK: Interprocess Communications |
Writing to a mailslot is similar to writing to a standard disk file. The following code uses the CreateFile, WriteFile, and CloseHandle functions to put a short message in a mailslot. The message is broadcast to every computer in the primary domain of the system.
LPSTR lpszMessage = "Message for sample_mailslot in primary domain.";
BOOL fResult;
HANDLE hFile;
DWORD cbWritten;
hFile = CreateFile("\\\\*\\mailslot\\sample_mailslot",
GENERIC_WRITE,
FILE_SHARE_READ, // required to write to a mailslot
(LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
ErrorHandler(hwnd, "Primary domain"); // local error handler
return FALSE;
}
fResult = WriteFile(hFile,
lpszMessage,
(DWORD) lstrlen(lpszMessage) + 1, // include terminating null
&cbWritten,
(LPOVERLAPPED) NULL);
if (!fResult)
{
ErrorHandler(hwnd, "WriteFile");
return FALSE;
}
TextOut(hdc, 10, 10, "WriteFile successful.", 21);
fResult = CloseHandle(hFile);
if (!fResult)
{
ErrorHandler(hwnd, "CloseHandle");
return FALSE;
}
TextOut(hdc, 10, 30, "CloseHandle successful.", 23);
return TRUE;