DosGiveSharedMem

From EDM2
Jump to: navigation, search

Syntax

DosGiveSharedMem( pBaseAddress, idProcessId, ulAttributeFlags )

Parameters

PVOID pBaseAddress (input)
This should contain the base address of the shared and giveable memory block. It should be the same address as the one returned when the block was created, using DosAllocSharedMem.
PID idProcessId (input)
The identifier of the process that should get access to the shared memory block.
ULONG ulAttributeFlags (input)
Attribute flags describing the desired access protection of the shared memory block.

Access protection

PAG_READ (0x00000001) tells the system that read access is desired.
PAG_WRITE (0x00000002) tells the system that write access is desired.
PAG_EXECUTE (0x00000004) tells the system that execute access is desired.

Setting PAG_GUARD (0x00000008) tells the system that access to the memory object should cause a guard page exception to be raised, in the subject process.

At least one of PAG_READ, PAG_WRITE or PAG_EXECUTE must be set.

Returns

APIRET rc
The following values can be returned:
0 NO_ERROR
5 ERROR_ACCESS_DENIED
8 ERROR_NOT_ENOUGH_MEMORY
87 ERROR_INVALID_PARAMETER
95 ERROR_INTERRUPT
212 ERROR_LOCKED
303 ERROR_INVALID_PROCID
487 ERROR_INVALID_ADDRESS

Usage Explanation

DosGiveSharedMem is used to give another process access to a shared memory block. This will allocate the virtual address of the memory block into the virtual-address space of the process that receives the access of the memory block. This is very similar to the process using DosGetSharedMem by itself to gain access to the shared memory block.

The shared memory block that is specified with the virtual address must be a giveable block. That is, it must have been created using DosAllocSharedMem with the OBJ_GIVEABLE attribute set.

The desired access protection must also be compatible with the access protection the shared memory block received when it was created.

Sample Code

#define INCL_DOSMEMMGR
#include <os2.h>
#include <bsememf.h>

PVOID BaseAddress;
      /* Pointer to the shared memory block. */

ULONG AttributeFlags;
      /* Flags describing the memory block */

PID ProcessID;
      /* Identificarion of the process that should be */
      /* granted access to the shared and giveable    */
      /* memory block.                                */

APIRET rc;
      /* Just for taking care of the return code */

/* So we create another process, and store the identification in */
/* ProcessID. We also use DosAllocSharedMem to create a          */
/* shared and givable memory block, storing the base address     */
/* of this block in BaseAddress. For this example we assume the  */
/* memory block was created as giveable, readable and writeable. */

AttributeFlags = PAG_WRITE | PAG_READ;

rc = DosGiveSharedMem( BaseAddress, ProcessID, AttributeFlags);

if (rc != 0)
{
   /* We have an error we must take care of. */
}

See Also