These 2 freeware C++ classes provides a clean interface to the Win32 IPC mechanism called mailslots. They are very similar to the more common "Named Pipe" IPC mechanism. Unlike named pipes, mailslots use datagrams and do not offer any form of integrated security like Named Pipes do. A datagram is a small packet of information that the network sends along the wire. Like a radio or television broadcast, a datagram offers no confirmation of receipt; there is no way to guarantee that a datagram has been received.
Mailslots can broadcast messages within a domain. If several processes in a domain each create a mailslot using the same name, every message that is addressed to that mailslot and sent to the domain is received by the participating processes. Because one process can control both a server and client mailslot handle, applications can easily implement a simple message-passing facility within a domain.
The sample application contains a demonstration program called WinNotify. It is based on the old MS WinPopup utility.
Features |
Usage |
Copyright |
History |
API Reference |
Contacting the Author |
v1.24 (9 April 2022)
v1.23 (3 May 2020)
v1.22 (26 December 2019)
v1.21 (21 September 2019)
v1.20 (3 June 2019)
v1.19 (14 October 2018)
v1.18 (30 April 2017)
v1.17 (16 January 2016)
v1.16 (10 May 2015)
v1.15 (7 September 2008)
v1.14 (8 September 2007)
v1.13 (23 December 2006)
v1.12 (22 December 2006)
v1.11 (26 July 2003)
v1.1 (26 July 1998)
The API consists of the 2 classes CServerMailslot and CClientMailslot and their public members
Server Side
CServerMailslot::CServerMailslot
CServerMailslot::~CServerMailslot
CServerMailslot::Open
CServerMailslot::Close
CServerMailslot::Read
CServerMailslot::GetInfo
CServerMailslot::SetReadTimeout
CServerMailslot::IsOpen
CServerMailslot::operator Handle
Client Side
CClientMailslot::CClientMailslot
CClientMailslot::~CClientMailslot
CClientMailslot::Open
CClientMailslot::Close
CClientMailslot::Write
CClientMailslot::IsOpen
CClientMailslot::operator Handle
CServerMailslot::CServerMailslot
CServerMailslot();
Remarks
Standard constructor which initializes member variables to a default value
See Also
CServerMailslot::~CServerMailslot
CServerMailslot::~CServerMailslot
~CServerMailslot();
Remarks
Standard destructor which closes the mailslot if open
See Also
CServerMailslot::CServerMailslot
bool CServerMailslot::Open(LPCTSTR lpszName, DWORD dwMaxMessageSize = 0, DWORD dwReadTimeout = 0, LPSECURITY_ATTRIBUTES lpSecurityAttributes = nullptr);
Return Value
true if the mailslot was successfully created otherwise false. If the call fails, you should use GetLastError to determine the reason.
Parameters
lpszName Pointer to a null-terminated string specifying the name of the mailslot.
dwMaxMessageSize Specifies the maximum size, in bytes, of a single message that can be written to the mailslots. To specify that the message can be of any size, set this value to zero.
dwReadTimeout Specifies the amount of time, in milliseconds, a read operation can wait for a message to be written to the mailslot before a time-out occurs. The following values have special meanings:
Value | Meaning |
---|---|
0 | Returns immediately if no message is present. (The system does not treat an immediate return as an error.) |
MAILSLOT_WAIT_FOREVER | Waits forever for a message. |
This time-out value applies to all subsequent read operations and all inherited mailslot handles.
lpSecurityAttributes Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpSecurityAttributes is NULL, the handle cannot be inherited.
Remarks
Creates a server side mailslot
bool CServerMailslot::Close();
Return Value
true if the mailslot was successfully closed otherwise false. If the call fails, you should use GetLastError to determine the reason.
bool CServerMailslot::Read(LPVOID lpBuffer, DWORD dwNumberOfBytesToRead, DWORD* lpNumberOfBytesRead);
Parameters
lpBuffer pointer to buffer that receives data
dwNumberOfBytesToRead number of bytes to read
lpNumberOfBytesRead pointer to number of bytes read
Remarks
Allows data to be read from the mailslot
bool CServerMailslot::GetInfo(LPDWORD lpMaxMessageSize, LPDWORD lpNextSize, LPDWORD lpMessageCount, LPDWORD lpReadTimeout);
Return Value
true if the function succeeded otherwise false. If the call fails, you should use GetLastError to determine the reason.
Parameters
lpMaxMessageSize Pointer to the maximum size in bytes allowed for this mailslot which is returned on a successful call to this method.
lpNextSize Pointer to the size of the next message in bytes which is returned on a successful call to this method.
lpMessageCount Pointer to the total number of messages waiting to be read which is returned on a successful call to this method.
lpReadTimeout Pointer to the amount of time in milliseconds a read operation waits for a message to be written to the mailslot before a time-out occur which is returned on a successful call to this method.
Remarks
Simple wrapper for the GetMailslotInfo API
CServerMailslot::SetReadTimeout
bool CServerMailslot::SetReadTimeout(DWORD dwReadTimeout);
Return Value
true if the mailslot timeout was successfully changed otherwise false. If the call fails, you should use GetLastError to determine the reason.
Parameters
dwReadTimeout Specifies the amount of time, in milliseconds, a read operation can wait for a message to be written to the mailslot before a time-out occurs. The following values have special meanings:
Value | Meaning |
---|---|
0 | Returns immediately if no message is present. (The system does not treat an immediate return as an error.) |
MAILSLOT_WAIT_FOREVER | Waits forever for a message. |
This time-out value applies to all subsequent read operations and to all inherited mailslot handles.
Remarks
The initial time-out value used by a mailslot for a read operation is typically set by Open when the mailslot is created.
bool CServerMailslot::IsOpen();
Return Value
true if the mailslot is open otherwise false.
CServerMailslot::operator HANDLE()
HANDLE CServerMailslot::operator HANDLE();
Return Value
The underlying handle which the class encapsulates.
CClientMailslot::CClientMailslot
CClientMailslot();
Remarks
Standard constructor which initializes member variables to a default value
See Also
CClientMailslot::~CClientMailslot
CClientMailslot::~CClientMailslot
~CClientMailslot();
Remarks
Standard destructor which closes the mailslot if open
See Also
CClientMailslot::CClientMailslot
bool CClientMailslot::Open(LPCTSTR lpszName, LPSECURITY_ATTRIBUTES lpSecurityAttributes = nullptr);
Return Value
true if the mailslot was successfully opened otherwise false. If the call fails, you should use GetLastError to determine the reason.
Parameters
lpszName Name of the mailslot to open.
lpSecurityAttributes Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpSecurityAttributes is NULL, the handle cannot be inherited.
Remarks
Opens the client connection to a mailslot on a specified machine
bool CClientMailslot::Close();
Return Value
true if the mailslot was successfully closed otherwise false. If the call fails, you should use GetLastError to determine the reason.
bool CClientMailslot::Write(LPVOID lpBuffer, DWORD dwNumberOfBytesToWrite, DWORD& dwNumberOfBytesWrite);
Parameters
lpBuffer pointer to data that is to be written
dwNumberOfBytesToWrite number of bytes to write
dwNumberOfBytesRead pointer to number of bytes written
Remarks
Allows data to be written to the mailslot
bool CClientMailslot::IsOpen();
Return Value
true if the mailslot is open otherwise false.
CClientMailslot::operator HANDLE()
HANDLE CClientMailslot::operator HANDLE();
Return Value
The underlying handle which the class encapsulates.
PJ Naughter
Email: pjna@naughter.com
Web: http://www.naughter.com
9 April 2022