public uint Read(out byte[] data) { if (!mailslot.IsValid()) { throw new Exception("Mailslot handle is invalid."); } uint bytesRead = 0; try { var info = mailslot.GetMailslotInfo(); if (info.MessageCount == 0) { data = new byte[] { }; return(0); } data = new byte[info.NextMessageSize]; WinFunctions.ReadFile(mailslot.Handle, data, (uint)data.Length, out bytesRead, IntPtr.Zero); } catch (Exception ex) { if (!IsRunning) { data = new byte[] {}; return(bytesRead); } data = new byte[] { }; WinFunctions.ThrowException("Read failed.", ex); } return(bytesRead); }
private uint Send(byte[] data) { uint bytesWritten = 0; try { mailslot.Handle = WinFunctions.CreateFile(target, WinFunctions.FileDesiredAccess.GenericWrite, 0, IntPtr.Zero, WinFunctions.FileCreationDisposition.OpenExisting, 0, IntPtr.Zero); WinFunctions.WriteFile(mailslot.Handle, data, (uint)data.Length, out bytesWritten, IntPtr.Zero); } catch (Exception ex) { WinFunctions.ThrowException("Send failed.", ex); } finally { if (mailslot.IsValid()) { mailslot.Dispose(); } } return(bytesWritten); }