471,601 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,601 software developers and data experts.

.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(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 ?
Mar 18 '08 #1
0 1181

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Steve T | last post: by
4 posts views Thread by Viper Venom | last post: by
7 posts views Thread by A.M | last post: by
8 posts views Thread by =?ISO-8859-15?B?TnXxbw==?= I.G | last post: by
28 posts views Thread by Jon Davis | last post: by
8 posts views Thread by JamesHoward | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.