Beispiel #1
0
        public string ReadMessage()
        {
            if (_handelMailslot == null)
            {
                throw new NullReferenceException("Не инициализирована работа с mailslot");
            }

            int messageBytes;
            int bytesRead;
            int messages;

            if (!MailSlotFunctions.GetMailslotInfo(_handelMailslot, IntPtr.Zero, out messageBytes, out messages, IntPtr.Zero))
            {
                return("");
            }

            if (messageBytes == -1)
            {
                return("");
            }

            var bBuffer = new byte[messageBytes];

            if (!MailSlotFunctions.ReadFile(_handelMailslot, bBuffer, messageBytes, out bytesRead, IntPtr.Zero) || bytesRead == 0)
            {
                return("");
            }

            string result = Encoding.Unicode.GetString(bBuffer);

            return(result);
        }
Beispiel #2
0
        public Int32 CreateMailSlot(string mailslotName)
        {
            string fullMailslotName = $@"\\.\mailslot\{mailslotName}";

            IntPtr handle = MailSlotFunctions.CreateMailslot(fullMailslotName, 0, 0, IntPtr.Zero);

            _handelMailslot = handle;

            return(handle.ToInt32());
        }
Beispiel #3
0
        public int SendMessage(string message)
        {
            if (_handelMailslot == null)
            {
                throw new NullReferenceException("Не инициализировано подключение к mailslot");
            }

            Byte[] bMessage = Encoding.Unicode.GetBytes(message);
            Int32  writenBytes;

            bool isSucceeded = MailSlotFunctions.WriteFile(_handelMailslot, bMessage, bMessage.Length, out writenBytes, IntPtr.Zero);

            return((int)writenBytes);
        }
Beispiel #4
0
        public Int32 OpenMailslot(string mailslotName, string remoteComputer = ".")
        {
            string fullMailslotName = $@"\\{remoteComputer}\mailslot\{mailslotName}";

            IntPtr handle = MailSlotFunctions.CreateFile(
                fullMailslotName,
                DesiredAccess.GenericWrite,
                ShareMode.FILE_SHARE_READ,
                IntPtr.Zero,
                CreationDisposition.OPEN_EXISTING,
                0,
                IntPtr.Zero);

            _handelMailslot = handle;

            return(handle.ToInt32());
        }