/// <summary> /// Constructor which creates the data gram from a message and channel name. /// </summary> /// <param name="channel">The channel through which the message will be sent.</param> /// <param name="message">The string message to send.</param> public DataGram(string channel, string message) { this.allocatedMemory = false; this.dataStruct = new Native.COPYDATASTRUCT(); this.channel = channel; this.message = message; }
/// <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; } }