By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,184 Members | 1,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,184 IT Pros & Developers. It's quick & easy.

.NET inter process shared memory

P: 1
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
Share this question for a faster answer!
Share on Google+

Post your reply

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