Now we have everything and can start putting pieces together. The packet string we send to GPU will have the following format: we specify first the name of our application (here TForm1) so that GPU can knows where to send the result back, then the Job ID and finally the GPU command (as in [3]).
We separate the three fields using ":".
Packet := 'TForm1'+':'+JobID+':'+ Command;
The first code line below ensures that we have a valid handle to the other application and also have a valid handle to our file mapping. Then we open up a view to the file with MapViewOfFile.
Think of creating the file mapping like using the AssignFile method on a disk file and the MapViewOfFile function like using textttReset or Rewrite on that file. The return result from MapViewOfFile though is an actual pointer to the memory space used by the file mapping. All we need to do now is put the string in it and let the other application know it is there. We first copy the string to the mapped area using StrPLCopy (which just copies the string up to a maximum number of characters).
Next we send a message to the other application telling it that a string is waiting for it in the Memory Mapped File. You will note that we do not actually pass any data in the lParam field of our "string" message. The message here is simply used to notify the other application that a string is available. After the message is sent, we close our view to the memory mapped file with UnmapViewOfFile [6].
procedure TForm1.SendJob(JobID, Command: String); var ThePtr : PChar; Packet : String; begin if (GPUHWnd = 0) or (GPUCommSpace = 0) then Exit; Packet := 'TForm1'+':'+JobID+':'+ Command; {send message} ThePtr := MapViewOfFile(GPUCommSpace, FILE_MAP_WRITE,0,0,0); StrPLCopy(ThePtr, Command, MaxMapLen); SendMessage(GPUHWnd, WMsgGPURequest, 0, 0); UnmapViewOfFile(ThePtr); end;