I'm using SendMessage(...) to send a message to all other process that might run. It works well. My problem is when I try to pass data using shared memory. Here is my code.
In the 1st process, I use this to send the msg
BinaryFormatter b = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
b.Serialize(stream, str);
stream.Flush();
int dataSize =(int) stream.Length;
byte[] data = new byte[dataSize];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(data, 0, dataSize);
IntPtr ptrData = Marshal.AllocHGlobal(dataSize);
Marshal.Copy(data, 0, ptrData, dataSize);
SendMessage(0xffff, lMsg, ptrData, Convert.ToInt64(dataSize));
In the other process i use this
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if ((UInt32)m.Msg == theCloseMsgId)
{
int bufferLenght = m.LParam.ToInt32();
byte[] data = new byte[bufferLenght];
Marshal.Copy(m.WParam, data, 0, bufferLenght);
BinaryFormatter b = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
stream.Write(data, 0, data.Length);
stream.Seek(0, SeekOrigin.Begin);
string str = (string)b.Deserialize(stream, null);
forceClose = true;
Close();
}
} //WndProc
I tried this in the same process and it works but in two differents process, the shared memory that was allocated with Marshal.AllocHGlobal() has changed. Anyone can help me ? Maybe another way to create the memory ?