We know now how to receive messages through the message loop and how to send them using SendMessage(...). However, we can only send the little Windows message record defined in section 6.2. This is not enough, we would like to send our command string for the virtual machine [3].
Memory Mapped Files is a way of sending an arbitrary block of data between two cooperating applications. This is done by obtaining a handle to a common piece of memory space from the operating system. When both applications have a handle to this space, they may write information back and forth to each other. The concept of Memory Mapped Files is actually very similar to creating a disk file and writing out the data while letting the other application open it up and read it.
We then define an ObtainMappingHandle(...) routine, that asks the operating system for memory. If the request for memory, done through CreateFileMapping(..) fails with ERROR_ALREADY_EXISTS, someone else reserved memory before. In this case, a call to OpenFileMapping(...) returns a handle to the common memory block. Since both applications will use the same string (GPU_Comm_Space), they will ultimately obtain a handle to the same memory mapped file [6].
function TForm1.ObtainMappingHandle: THandle; begin Result := CreateFileMapping(\$FFFFFFFF,nil,PAGE_READWRITE, 0,2000, GPU_Comm_Space); if Result <> 0 then // Did it return a valid handle? // Did it already exist? if GetLastError \= ERROR_ALREADY_EXISTS then begin CloseHandle(Result); // Close this one - we will // open existing one instead Result := OpenFileMapping(FILE_MAP_WRITE,False, GPU_Comm_Space); end; end;
GPU_Comm_Space is a string constant. The FileMappings routines hash the value to get a memory block. All frontends and GPU hash the same GPU_Comm_Space string constant to the same integer value, so that they all share the same piece of memory.