using System; using System.Runtime.InteropServices; namespace Vanara.PInvoke { public static partial class Kernel32 { /// Waits forever for a message. public const uint MAILSLOT_WAIT_FOREVER = unchecked((uint)-1); /// /// Creates a mailslot with the specified name and returns a handle that a mailslot server can use to perform operations on the /// mailslot. The mailslot is local to the computer that creates it. An error occurs if a mailslot with the specified name already exists. /// /// /// The name of the mailslot. This name must have the following form: /// \\.\mailslot\[path]name /// /// The name field must be unique. The name may include multiple levels of pseudo directories separated by backslashes. For example, /// both \\.\mailslot\example_mailslot_name and \\.\mailslot\abc\def\ghi are valid names. /// /// /// /// The maximum size of a single message that can be written to the mailslot, in bytes. To specify that the message can be of any /// size, set this value to zero. /// /// /// /// The time a read operation can wait for a message to be written to the mailslot before a time-out occurs, in milliseconds. 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((DWORD)-1) /// Waits forever for a message. /// /// /// /// This time-out value applies to all subsequent read operations and all inherited mailslot handles. /// /// /// A pointer to a SECURITY_ATTRIBUTES structure. The bInheritHandle member of the structure determines whether the /// returned handle can be inherited by child processes. If lpSecurityAttributes is NULL, the handle cannot be inherited. /// /// /// /// If the function succeeds, the return value is a handle to the mailslot, for use in server mailslot operations. The handle /// returned by this function is asynchronous, or overlapped. /// /// If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError. /// // HANDLE WINAPI CreateMailslot( _In_ LPCTSTR lpName, _In_ DWORD nMaxMessageSize, _In_ DWORD lReadTimeout, _In_opt_ // LPSECURITY_ATTRIBUTES lpSecurityAttributes); https://msdn.microsoft.com/en-us/library/windows/desktop/aa365147(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)] [PInvokeData("Winbase.h", MSDNShortId = "aa365147")] public static extern SafeMailslotHandle CreateMailslot(string lpName, uint nMaxMessageSize, uint lReadTimeout, [In, Optional] SECURITY_ATTRIBUTES lpSecurityAttributes); /// Retrieves information about the specified mailslot. /// A handle to a mailslot. The CreateMailslot function must create this handle. /// /// The maximum message size, in bytes, allowed for this mailslot. This value can be greater than or equal to the value specified in /// the cbMaxMsg parameter of the CreateMailslot function that created the mailslot. This parameter can be NULL. /// /// /// The size of the next message, in bytes. The following value has special meaning. /// /// /// /// Value /// Meaning /// /// /// MAILSLOT_NO_MESSAGE((DWORD)-1) /// There is no next message. /// /// /// /// This parameter can be NULL. /// /// /// The total number of messages waiting to be read, when the function returns. This parameter can be NULL. /// /// /// 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. This parameter is filled in when the function returns. This parameter can be NULL. /// /// /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. To get extended error information, call GetLastError. /// // BOOL WINAPI GetMailslotInfo( _In_ HANDLE hMailslot, _Out_opt_ LPDWORD lpMaxMessageSize, _Out_opt_ LPDWORD lpNextSize, _Out_opt_ // LPDWORD lpMessageCount, _Out_opt_ LPDWORD lpReadTimeout); https://msdn.microsoft.com/en-us/library/windows/desktop/aa365435(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa365435")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetMailslotInfo([In] SafeMailslotHandle hMailslot, out uint lpMaxMessageSize, out uint lpNextSize, out uint lpMessageCount, out uint lpReadTimeout); /// Sets the time-out value used by the specified mailslot for a read operation. /// A handle to a mailslot. The CreateMailslot function must create this handle. /// /// /// The time a read operation can wait for a message to be written to the mailslot before a time-out occurs, in milliseconds. 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((DWORD)-1) /// Waits forever for a message. /// /// /// /// This time-out value applies to all subsequent read operations and to all inherited mailslot handles. /// /// /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. To get extended error information, call GetLastError. /// // BOOL WINAPI SetMailslotInfo( _In_ HANDLE hMailslot, _In_ DWORD lReadTimeout); https://msdn.microsoft.com/en-us/library/windows/desktop/aa365786(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa365786")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SetMailslotInfo([In] SafeMailslotHandle hMailslot, uint lReadTimeout); /// /// Provides a to a mailslot that releases a created MailslotHandle instance at disposal using CloseHandle. /// public class SafeMailslotHandle : SafeKernelHandle { /// Initializes a new instance of the class and assigns an existing handle. /// An object that represents the pre-existing handle to use. /// /// to reliably release the handle during the finalization phase; otherwise, (not recommended). /// public SafeMailslotHandle(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { } /// Initializes a new instance of the class. private SafeMailslotHandle() : base() { } } } }