15.2.2 Writing to a Mailslot

Writing to a mailslot is similar to writing to a standard file. The following code fragment uses the CreateFile, WriteFile, and _lclose 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 slot1.slt in primary domain.";

BOOL fResult;

HANDLE hFile;

DWORD cbWritten;

hFile = CreateFile("\\\\*\\mailslot\\slot1.slt",

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");

return FALSE;

}

fResult = WriteFile(hFile,

lpszMessage,

(DWORD) lstrlen(lpszMessage) + 1, /* include null terminator */

&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;

Messages that are broadcast to a domain must be less than or equal to 400 bytes in length.