Author Topic: Shared Memory correct usage  (Read 530 times)

Martin Vieregg

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 8
  • Posts: 226
    • View Profile
Shared Memory correct usage
« on: March 21, 2020, 11:42:28 am »
I want to use one Shared Memory in both directions. Samples in the internet are poor.

In the server, I wrote:

rc := DosAllocSharedMem(SharedMem, NULL, 4096/*smaller has no effect*/, PAG_WRITE | PAG_READ | PAG_COMMIT | OBJ_GETTABLE);


SharedMem is an untyped pointer.

I transfer the pointer value via commandline parameter to the client.

The pointer value is hold in ulong4.

In the client, I wrote:

      SharedMem = (VOID*) ulong4;
      SharedMem2 = (VOID*) (ulong4+8);
      rc = DosGetSharedMem(SharedMem, PAG_READ | PAG_WRITE);
      PSharedULONG = (PULONG) SharedMem;
      PSharedULONG2 = (PULONG) SharedMem2;

     
I can write the values in the server and read the values in the client, but not reverse (crash). I wrote in the client:

    *PSharedULONG2 = 2;
    //Now it crashes. PSharedULONG also crashes.


Where is my mistake?


Martin Vieregg

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 8
  • Posts: 226
    • View Profile
Re: Shared Memory correct usage
« Reply #1 on: March 21, 2020, 12:21:47 pm »
I'm sorry. The code above is correct ! The reason why the program hangs was in the server. I use the shared memory together with event semaphores. I forgot to call DosResetEventSem after receiving the event semaphore with DosWaitEventSem. So the client always sends the same semaphore to the server and the whole computer falls onto its knees, it looked like the client hangs.
« Last Edit: March 21, 2020, 12:30:11 pm by Martin Vieregg »

Laurence Pithie

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 3
  • Posts: 59
    • View Profile
Re: Shared Memory correct usage
« Reply #2 on: March 21, 2020, 01:51:46 pm »
As a suggestion, for avoiding pitfalls such as you describe in future, you should abstract your shared memory usage and wrap it in an api.