Shared Memory

From version 4.71 onwards, SHLWAPI provides a set of functions for sharing memory among processes:

The facility may have been intended originally just for transferring data from the address space of one process (the source) to that of another process (the target), and this seems the kindest interpretation of what may be intended by Microsoft’s documentation. However, the model generalises readily.

Of the processes that are to participate in the sharing, identify one as the originating process and one as a reference process. The originating process and reference process may be the same. The originating process, armed with a 32-bit process identifier (PID) for the reference process, calls SHAllocShared to create a shared memory area and, optionally, to copy data into it. The result is a handle in the context of the reference process. Thereafter, the shared memory area is represented by the combination of the handle and the PID for the reference process.

Any process that knows the handle and the reference PID may call SHLockShared or SHLockSharedEx to get the shared memory mapped into that process’s address space. The pointer so obtained remains valid for that process’s access to the shared memory area until the process passes the pointer to SHUnlockShared. Any process that knows the handle and the reference PID may call SHFreeShared to ask that the shared memory area be destroyed when all its mappings into address spaces have eventually been released.

Within SHLWAPI, the SHMapHandle function is just a helper for the others, but it is exported and could be used generally for duplicating a handle from one process into another when both processes are known by PID.

The original five were first implemented in the NT releases of SHELL32 version 4.00 in 1996, but with SHMapHandle left as internal (i.e., not exported). They were relocated to SHLWAPI for version 4.71 in 1997. It is as exports from SHELL32 that these functions remain most easily accessible, given that Microsoft eventually documented them as Settlement Program Interfaces in 2002 and has recently arranged that these newly documented functions be exported from SHELL32 by name.