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);
                }
            }
        }
Esempio n. 2
0
 /// <summary>
 ///     The native window message filter used to catch our custom WM_COPYDATA
 ///     messages containing cross AppDomain messages. All other messages are ignored.
 /// </summary>
 /// <param name = "msg">A representation of the native Windows Message.</param>
 protected override void WndProc(ref Message msg)
 {
     base.WndProc(ref msg);
     if (msg.Msg != Native.WM_COPYDATA)
     {
         return;
     }
     // we can free any unmanaged resources immediately in the dispose, managed channel and message
     // data will still be retained in the object passed to the event
     using (var dataGram = WinMsgDataGram.FromPointer(msg.LParam))
     {
         if (MessageReceived != null && dataGram.IsValid)
         {
             MessageReceived.Invoke(this, new MessageEventArgs(dataGram));
         }
     }
 }