Memory Mapped Files - Server

//
// Memory Mapped Files Server.
// Creates memory mapped file and "writes" current
// mouse coordinates into it
//
unit MMapUnit;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
type
  TForm1 = class(TForm)
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
 Form1    : TForm1;
 hMapObj  : THandle;    // mapped file handle
 PMapView : PLongInt;   // memory region
implementation
{$R *.DFM}
//
// When form is created
//
procedure TForm1.FormCreate(Sender: TObject);
begin
//
// create memory mapped file
//
 hMapObj := CreateFileMapping(
            $FFFFFFFF,                  // not a disk file
            Nil,                                // attributes
            PAGE_READWRITE,             // read and write
            0,                          //
            SizeOf(DWORD),              // size
            'shared_memory');           // "name"
 if hMapObj = 0 Then
  begin
   ShowMessage('MapFileError');
  // Error, exit
  end;
 GetMem(PMapView, SizeOf(LongInt)); // allocate memory
//
// Get address of the region
//
 PMapView := (MapViewOfFile(hMapObj, FILE_MAP_WRITE, 0, 0, 0));
end;
//
// When form is closed
//
procedure TForm1.FormDestroy(Sender: TObject);
begin
 UnmapViewOfFile(PMapView);             // free memory
 CloseHandle(hMapObj);                  // close "file"
end;
//
// Write mouse coordinates
//
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState;
                                X,Y: Integer);
begin
 PMapView^ := X SHL 8 + Y;      // Write
 Label1.Caption := IntToStr(Hi(PMapView^)) + ':' +
                   IntToStr(Lo(PMapView^));
end;
end.