public void SendMessage(string msg) { if (_handle == null) { CreateHandle(); } int bytesWritten; byte[] bMessage = Encoding.Unicode.GetBytes(msg); bool succeeded = Mailslot.WriteFile(_handle, bMessage, bMessage.Length, out bytesWritten, IntPtr.Zero); if (!succeeded || bMessage.Length != bytesWritten) { if (_handle != null) { _handle.Close(); } _handle = null; throw new Win32Exception(); } }
public MailslotServer(string name) { _handle = Mailslot.CreateMailslot(@"\\.\mailslot\" + name, 0, unchecked ((int)0xFFFFFFFF), IntPtr.Zero); if (_handle.IsInvalid) { throw new Win32Exception(); } }
private void CreateHandle() { _handle = Mailslot.CreateFile( @"\\" + _machine + @"\mailslot\" + _name, Mailslot.FileDesiredAccess.GenericWrite, Mailslot.FileShareMode.FileShareRead, IntPtr.Zero, Mailslot.FileCreationDisposition.OpenExisting, 0, IntPtr.Zero); if (_handle.IsInvalid) { throw new Win32Exception(); } }
public string GetNextMessage() { const int maxMessageBytes = 4096; int bytesRead; // For non-blocking read, get message info first, and return null // if no message is available. For our purposes, we want to block // until a message is available. // //if (!Mailslot.GetMailslotInfo(_handle, IntPtr.Zero, out messageBytes, // out messages, IntPtr.Zero)) throw new Win32Exception(); // //if (messageBytes == Mailslot.MailslotNoMessage) return null; var bBuffer = new byte[maxMessageBytes]; if (!Mailslot.ReadFile(_handle, bBuffer, maxMessageBytes, out bytesRead, IntPtr.Zero) || bytesRead == 0) { throw new Win32Exception(); } return(Encoding.Unicode.GetString(bBuffer, 0, bytesRead)); }