An application uses DosAllocSharedMem to allocate shared memory. When allocating the shared memory, an application can assign a unique name to the memory. Any application that has the name of the shared memory can use DosGetNamedSharedMem to retrieve a pointer to the memory. This makes it possible for two or more applications to share memory at the same time.
The name of a shared memory object has the following form:
\sharemem\name
The "\sharemem\" is required. The "name" parameter can be any name that conforms to the rules for an OS/2 file name. No file is actually created for the memory object. There is no actual "\sharemem\" subdirectory.
The following code fragment allocates 65536 bytes of named shared memory with the name "\sharemem\mymem".
#define INCL_DOSMEMMGR /* Memory Manager values */ #include <os2.h> APIRET ulrc; CHAR szMem[] = { "\\sharemem\\mymem" }; PULONG pulPb; ulrc = DosAllocSharedMem((PVOID *) &pulPb, szMem, 65536, fALLOC); *pulPb = 2762;
Once the named memory is allocated, any other process can retrieve a pointer to the named memory by using DosGetNamedSharedMem.
The following code fragment retrieves a pointer to the named memory allocated above:
#define INCL_DOSMEMMGR /* Memory Manager values */ #include <os2.h> #define HF_STDOUT 1 /* Standard output handle */ APIRET ulrc; CHAR szMem[] = { "\\sharemem\\mymem" }; PULONG pulPb2; ULONG ulWritten; ulrc = DosGetNamedSharedMem((PVOID *) &pulPb2, szMem, PAG_READ | PAG_WRITE); if (*pulPb2 == 2762) ulrc = DosWrite(HF_STDOUT, "\r\n Success!\r\n", 13, &ulWritten);