protected override void WndProc(ref Message m) { if (m.Msg == 0x4a) { COPYDATASTRUCT copydatastruct = new COPYDATASTRUCT(); copydatastruct = (COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, typeof(COPYDATASTRUCT)); if (copydatastruct.cbData > 0) { byte[] destination = new byte[copydatastruct.cbData]; Marshal.Copy(copydatastruct.lpData, destination, 0, copydatastruct.cbData); MemoryStream serializationStream = new MemoryStream(destination); BinaryFormatter formatter = new BinaryFormatter(); CopyDataObjectData data = (CopyDataObjectData)formatter.Deserialize(serializationStream); if (this.channels.Contains(data.Channel)) { DataReceivedEventArgs e = new DataReceivedEventArgs(data.Channel, data.Data, data.Sent); this.OnDataReceived(e); m.Result = (IntPtr)1; } } } else if (m.Msg == 2) { this.channels.OnHandleChange(); base.OnHandleChange(); } base.WndProc(ref m); }
public int Send(object obj) { int num = 0; if (this.disposed) { throw new InvalidOperationException("Object has been disposed"); } if (this.recreateChannel) { this.addChannel(); } CopyDataObjectData graph = new CopyDataObjectData(obj, this.channelName); BinaryFormatter formatter = new BinaryFormatter(); MemoryStream serializationStream = new MemoryStream(); formatter.Serialize(serializationStream, graph); serializationStream.Flush(); int length = (int)serializationStream.Length; if (length > 0) { byte[] buffer = new byte[length]; serializationStream.Seek(0L, SeekOrigin.Begin); serializationStream.Read(buffer, 0, length); IntPtr destination = Marshal.AllocCoTaskMem(length); Marshal.Copy(buffer, 0, destination, length); EnumWindows windows = new EnumWindows(); windows.GetWindows(); foreach (EnumWindowsItem item in windows.Items) { if (!item.Handle.Equals(this.owner.Handle) && (GetProp(item.Handle, this.channelName) != 0)) { COPYDATASTRUCT lParam = new COPYDATASTRUCT { cbData = length, dwData = IntPtr.Zero, lpData = destination }; SendMessage(item.Handle, 0x4a, (int)this.owner.Handle, ref lParam); num += (Marshal.GetLastWin32Error() == 0) ? 1 : 0; } } Marshal.FreeCoTaskMem(destination); } serializationStream.Close(); return(num); }