In the App A a integer gets written to the ram.
var intBytes = BitConverter.GetBytes(123);
var allocatedMemory = Marshal.AllocHGlobal(intBytes.Length);
Marshal.Copy(intBytes, 0, allocatedMemory, intBytes.Length);
The pointer is then send to App B where the integer gets read again.
byte[] buffer = new byte[Marshal.SizeOf<int>()];
Marshal.Copy(allocatedMemory, buffer, 0, buffer.Length);
var myRecievedInteger = BitConverter.ToInt32(buffer, 0);
The problem is that App B is getting a wrong random value and sometimes the Marshal.Copy
method in the App B is throwing an System.AccessViolationException
.
- Using the
AllocCoTaskMem
method doesn't change anything. - Setting a fixed size for the memory like
4
is not helping either. - The pointer is in both Apps the same.
- The read buffer in App B contains wrong data. (Not 123,0,0,0)
Does somebody know what I'm doing wrong?