What the Clipboard Does

The clipboard works primarily by altering the memory allocation flags of global memory blocks. When a program allocates a global memory block using the GHND flag (a combination of the GMEM_MOVEABLE and GMEM_ZEROINIT flags), the memory block is marked as belonging to the program (more precisely, the particular instance of the program). Normally, Windows deletes this memory block when the instance terminates. When a program uses SetClipboardData to give the global handle to the clipboard, Windows must transfer ownership of the memory block from the program to itself. This action requires that Windows modify the memory allocation flags of the global memory block by calling:

GlobalReAlloc (hMem, 0L, GMEM_MODIFY | GMEM_DDESHARE) ;

The USER module establishes ownership of the memory block. After the SetClipboardData call, the global memory handle no longer belongs to the program that allocated it, and the block won't be freed when the program terminates. The program that created the memory block can't continue to use it except when the clipboard gives the program access to the block. Now the USER module must explicitly free the memory block, which it does when a program calls EmptyClipboard.

When a program calls GetClipboardData, Windows gives the program making the call the handle to the global memory block and allows the program temporary access to the memory block. The program can then copy this data into another global memory block or a local memory block. Thus, the clipboard is really just a manager of shared memory segments. One program gives the clipboard a block of global memory. Other programs can get access to the block. The clipboard retains ownership of it.