internal WinMsgDataGram(ISerializer serializer, string channel, string dataType, string message) { serializer.Requires("serializer").IsNotNull(); this.serializer = serializer; allocatedMemory = false; dataStruct = new Native.COPYDATASTRUCT(); dataGram = new DataGram(channel, dataType, message); }
/// <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 = "dataType">The type of the message used for deserialization hints.</param> /// <param name = "message">The string message to send.</param> internal WinMsgDataGram(ISerializer serializer, string channel, string dataType, string message) { Validate.That(serializer).IsNotNull(); this.serializer = serializer; allocatedMemory = false; dataStruct = new Native.COPYDATASTRUCT(); dataGram = new DataGram(channel, dataType, message); }
private WinMsgDataGram(IntPtr lpParam, ISerializer serializer) { serializer.Requires("serializer").IsNotNull(); this.serializer = serializer; 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 = serializer.Deserialize<DataGram>(rawmessage); }
/// <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> /// <param name = "serializer">Serializer to deserialize raw DataGram message.</param> private WinMsgDataGram(IntPtr lpParam, ISerializer serializer) { Validate.That(serializer).IsNotNull(); this.serializer = serializer; 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); } // use helper method to expand the raw message dataGram = serializer.Deserialize <DataGram>(rawmessage); }
private void SendToChannel(string channelName, string dataType, string message) { Validate.That(channelName).IsNotNullOrEmpty(); Validate.That(message).IsNotNullOrEmpty(); // create a DataGram instance, and ensure memory is freed using (var dataGram = new WinMsgDataGram(serializer, channelName, dataType, 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(XDWinMsgListener.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); } } }