Jump to content

DosGiveSharedMem: Difference between revisions

From EDM2
Line 75: Line 75:
DosGiveSharedMem is used to give another process access to a shared memory
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
block.  This will allocate the virtual address of the memory block into the
virtual-address space of the process that recieves the access of the memory
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
block.  This is very similar to the process using [[OS2 API:CPI:DosGetSharedMem|DosGetSharedMem]] by itself to gain access to
the shared memory block.
the shared memory block.


Line 82: Line 82:
The shared memory block that is specified with the virtual address must be
The shared memory block that is specified with the virtual address must be
a giveable block. That is, it must have been created using
a giveable block. That is, it must have been created using
[[DosAllocSharedMem]] with the
[[OS2 API:CPI:DosAllocSharedMem|DosAllocSharedMem]] with the
'''OBJ_GIVEABLE''' attribute set.
'''OBJ_GIVEABLE''' attribute set.


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


=== Relevant Structures ===
=== Relevant Structures ===

Revision as of 00:21, 17 May 2016

Syntax

rc = 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

Setting PAG_READ (0x00000001) tells the system that read access is desired.

Setting PAG_WRITE (0x00000002) tells the system that write access is desired.

Setting 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

Include Info

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


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.

Relevant Structures

Gotchas

Sample Code

#define INCL_DOSMEMMGR
#include 
#include 

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

CPI:DosAllocSharedMem, CPI:DosGetNamedSharedMem, CPI:DosGetSharedMem