static void Main(string[] args) { NativeMethods.SafeMailslotHandle hMailslot = null; try { // Prepare the security attributes (the lpSecurityAttributes parameter // in CreateMailslot) for the mailslot. This is optional. If the // lpSecurityAttributes parameter of CreateMailslot is NULL, the // mailslot gets a default security descriptor and the handle cannot // be inherited. The ACLs in the default security descriptor of a // mailslot grant full control to the LocalSystem account, (elevated) // administrators, and the creator owner. They also give only read // access to members of the Everyone group and the anonymous account. // However, if you want to customize the security permission of the // mailslot, (e.g. to allow Authenticated Users to read from and // write to the mailslot), you need to create a SECURITY_ATTRIBUTES // structure. NativeMethods.SECURITY_ATTRIBUTES sa = null; sa = CreateMailslotSecurity(); // Create the mailslot. hMailslot = NativeMethods.CreateMailslot( MailslotName, // The name of the mailslot 0, // No maximum message size NativeMethods.MAILSLOT_WAIT_FOREVER, // Waits forever for a message sa // Mailslot security attributes ); if (hMailslot.IsInvalid) { throw new Win32Exception(); } Console.WriteLine("The mailslot ({0}) is created.", MailslotName); // Check messages in the mailslot. Console.Write("Press ENTER to check new messages or press Q to quit ..."); string cmd = Console.ReadLine(); while (!cmd.Equals("Q", StringComparison.OrdinalIgnoreCase)) { Console.WriteLine("Checking new messages..."); ReadMailslot(hMailslot); Console.Write("Press ENTER to check new messages or press Q to quit ..."); cmd = Console.ReadLine(); } } catch (Win32Exception ex) { Console.WriteLine("The server throws the error: {0}", ex.Message); } finally { if (hMailslot != null) { hMailslot.Close(); hMailslot = null; } } }
/// <summary> /// Read the messages from a mailslot by using the mailslot handle in a call /// to the ReadFile function. /// </summary> /// <param name="hMailslot">The handle of the mailslot</param> /// <returns> /// If the function succeeds, the return value is true. /// </returns> static bool ReadMailslot(NativeMethods.SafeMailslotHandle hMailslot) { int cbMessageBytes = 0; // Size of the message in bytes int cbBytesRead = 0; // Number of bytes read from the mailslot int cMessages = 0; // Number of messages in the slot int nMessageId = 0; // Message ID bool succeeded = false; // Check for the number of messages in the mailslot. succeeded = NativeMethods.GetMailslotInfo( hMailslot, // Handle of the mailslot IntPtr.Zero, // No maximum message size out cbMessageBytes, // Size of next message out cMessages, // Number of messages IntPtr.Zero // No read time-out ); if (!succeeded) { Console.WriteLine("GetMailslotInfo failed w/err 0x{0:X}", Marshal.GetLastWin32Error()); return(succeeded); } if (cbMessageBytes == NativeMethods.MAILSLOT_NO_MESSAGE) { // There are no new messages in the mailslot at present Console.WriteLine("No new messages."); return(succeeded); } // Retrieve the messages one by one from the mailslot. while (cMessages != 0) { nMessageId++; // Declare a byte array to fetch the data byte[] bBuffer = new byte[cbMessageBytes]; succeeded = NativeMethods.ReadFile( hMailslot, // Handle of mailslot bBuffer, // Buffer to receive data cbMessageBytes, // Size of buffer in bytes out cbBytesRead, // Number of bytes read from mailslot IntPtr.Zero // Not overlapped I/O ); if (!succeeded) { Console.WriteLine("ReadFile failed w/err 0x{0:X}", Marshal.GetLastWin32Error()); break; } // Display the message. Console.WriteLine("Message #{0}: {1}", nMessageId, Encoding.Unicode.GetString(bBuffer)); // Get the current number of un-read messages in the slot. The number // may not equal the initial message number because new messages may // arrive while we are reading the items in the slot. succeeded = NativeMethods.GetMailslotInfo( hMailslot, // Handle of the mailslot IntPtr.Zero, // No maximum message size out cbMessageBytes, // Size of next message out cMessages, // Number of messages IntPtr.Zero // No read time-out ); if (!succeeded) { Console.WriteLine("GetMailslotInfo failed w/err 0x{0:X}", Marshal.GetLastWin32Error()); break; } } return(succeeded); }