.NET inter process shared memory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phreak008
    New Member
    • Oct 2007
    • 1

    .NET inter process shared memory

    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(str eam, str);
    stream.Flush();
    int dataSize =(int) stream.Length;
    byte[] data = new byte[dataSize];
    stream.Seek(0, SeekOrigin.Begi n);
    stream.Read(dat a, 0, dataSize);
    IntPtr ptrData = Marshal.AllocHG lobal(dataSize) ;

    Marshal.Copy(da ta, 0, ptrData, dataSize);


    SendMessage(0xf fff, lMsg, ptrData, Convert.ToInt64 (dataSize));


    In the other process i use this


    protected override void WndProc(ref Message m)
    {
    base.WndProc(re f m);

    if ((UInt32)m.Msg == theCloseMsgId)
    {
    int bufferLenght = m.LParam.ToInt3 2();
    byte[] data = new byte[bufferLenght];
    Marshal.Copy(m. WParam, data, 0, bufferLenght);

    BinaryFormatter b = new BinaryFormatter ();
    MemoryStream stream = new MemoryStream();
    stream.Write(da ta, 0, data.Length);
    stream.Seek(0, SeekOrigin.Begi n);
    string str = (string)b.Deser ialize(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.AllocHG lobal() has changed. Anyone can help me ? Maybe another way to create the memory ?
Working...