Example #1
0
        public string GetNextMessage()
        {
            int messageBytes;
            int bytesRead;
            int messages;

            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[messageBytes];

            if (!Mailslot.ReadFile(_handle, bBuffer, messageBytes, out bytesRead,
                                   IntPtr.Zero) || bytesRead == 0)
            {
                throw new Win32Exception();
            }

            return(Encoding.Unicode.GetString(bBuffer));
        }
Example #2
0
        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();
            }
        }
Example #3
0
        public MailslotServer(string name)
        {
            _handle = Mailslot.CreateMailslot(@"\\.\mailslot\" + name, 0, 0, IntPtr.Zero);

            if (_handle.IsInvalid)
            {
                throw new Win32Exception();
            }
        }
Example #4
0
        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();
            }
        }