2

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?

NtFreX
  • 10,379
  • 2
  • 43
  • 63
  • 1
    Processes do not share memory. Marshalling is for bridging the gap between managed and unmanaged memory. Neither managed nor unmanaged memory is shared between processes. To communicate between processes, there are various techniques. You can use the file system as the easiest and most basic. You can also use named pipes, or WCF. – Guillaume CR Jan 09 '17 at 17:03
  • shared memory is also one technique that is available – Paweł Łukasik Jan 09 '17 at 17:04
  • Im trying to send a struct with the `lParam` argument over the `SendMessage` funtion from user32.dll to another process. Aren't they using the same technique? – NtFreX Jan 09 '17 at 17:10
  • This sounds like a [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) you should instead ask about the problem you are trying to solve that you think using shared memory will fix. There may be better solutions than shared memory, and if not you will definitely get better answers on how to do the shared memory correctly. – Scott Chamberlain Jan 09 '17 at 17:19
  • @ScottChamberlain You are right I'll as you said create a new question with the real problem description. As far as I know the answer bellow is quiete compleate and therefore I'll accept it. – NtFreX Jan 09 '17 at 17:22

1 Answers1

3

As already mentioned in the comments, Marshaling cannot be use in this way.

Applications cannot go freely writing and reading each others memory. That's a great feature of the O.S. that allows applications not to crash each others.

If you want to share data among applications in Windows, you may use the following options, as explained here:

Community
  • 1
  • 1
El Marce
  • 3,144
  • 1
  • 26
  • 40
  • Do you know how the message loop works with this issue? With the `SendMessage` function of `user32.dll` you can send pointers to another process. Are they using COM? – NtFreX Jan 09 '17 at 17:16
  • Maybe, or maybe they are using `ReadProcessMemory` and `WriteProcessMemory` from `kernal32.dll`. it depends on the specific message. – Scott Chamberlain Jan 09 '17 at 17:20