/// <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) { // 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 (DataGram dataGram = DataGram.FromPointer(msg.LParam)) { if (MessageReceived != null && dataGram.IsValid) { MessageReceived.Invoke(this, new XDMessageEventArgs(dataGram)); } } } }
/// <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 DataGram(IntPtr lpParam) { this.allocatedMemory = false; this.dataStruct = (Native.COPYDATASTRUCT)Marshal.PtrToStructure(lpParam, typeof(Native.COPYDATASTRUCT)); byte[] bytes = new byte[this.dataStruct.cbData]; Marshal.Copy(this.dataStruct.lpData, bytes, 0, this.dataStruct.cbData); string rawmessage; using (MemoryStream stream = new MemoryStream(bytes)) { BinaryFormatter b = new BinaryFormatter(); rawmessage = (string)b.Deserialize(stream); } // use helper method to expand the raw message using (DataGram dataGram = DataGram.ExpandFromRaw(rawmessage)) { this.channel = dataGram.Channel; this.message = dataGram.Message; } }
/// <summary> /// Constructor used to create a new instance from a DataGram struct. /// </summary> /// <param name="dataGram">The DataGram instance.</param> internal XDMessageEventArgs(DataGram dataGram) { this.dataGram = dataGram; }
/// <summary> /// Constructor used to create a new instance from a DataGram struct. /// </summary> /// <param name="dataGram">The DataGram instance.</param> internal XDMessageEventArgs(DataGram dataGram) { this.dataGram = dataGram; }