Exemplo n.º 1
0
        private void SendToChannel(string channelName, string message)
        {
            Validate.That(channelName).IsNotNullOrEmpty();
            Validate.That(message).IsNotNullOrEmpty();

            // create a DataGram instance, and ensure memory is freed
            using (var dataGram = new WinMsgDataGram(channelName, message))
            {
                // Allocate the DataGram to a memory address contained in COPYDATASTRUCT
                Native.COPYDATASTRUCT dataStruct = dataGram.ToStruct();

                // Use a filter with the EnumWindows class to get a list of windows containing
                // a property name that matches the destination channel. These are the listening
                // applications.
                var filter = new WindowEnumFilter(MessageListener.GetChannelKey(channelName));

                var winEnum = new WindowsEnum(filter.WindowFilterHandler);

                foreach (var hWnd in winEnum.Enumerate())
                {
                    IntPtr outPtr;
                    // For each listening window, send the message data. Return if hang or unresponsive within 1 sec.
                    Native.SendMessageTimeout(hWnd, Native.WM_COPYDATA, IntPtr.Zero, ref dataStruct,
                                              Native.SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 1000, out outPtr);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///   Constructor creates an instance of the class from a pointer address, and expands
        ///   the data packet into the originating channel name and message.
        /// </summary>
        /// <param name = "lpParam">A pointer the a COPYDATASTRUCT containing information required to
        ///   expand the DataGram.</param>
        private WinMsgDataGram(IntPtr lpParam)
        {
            allocatedMemory = false;

            dataStruct = (Native.COPYDATASTRUCT)Marshal.PtrToStructure(lpParam, typeof(Native.COPYDATASTRUCT));
            var bytes = new byte[dataStruct.cbData];

            Marshal.Copy(dataStruct.lpData, bytes, 0, dataStruct.cbData);

            string rawmessage;

            using (var stream = new MemoryStream(bytes))
            {
                var b = new BinaryFormatter();
                rawmessage = (string)b.Deserialize(stream);
            }

            dataGram = JsonConvert.DeserializeObject <DataGram>(rawmessage);
        }
Exemplo n.º 3
0
 /// <summary>
 ///   Constructor which creates the data gram from a message and channel name.
 /// </summary>
 /// <param name = "serializer"></param>
 /// <param name = "channel">The channel through which the message will be sent.</param>
 /// <param name = "message">The string message to send.</param>
 internal WinMsgDataGram(string channel, string message)
 {
     allocatedMemory = false;
     dataStruct      = new Native.COPYDATASTRUCT();
     dataGram        = new DataGram(channel, message);
 }