Memory Mapped Files - Client

//
// Memory Mapped File Client.
// Reads mouse coordinates from mapped file
//
unit MMCli;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;
type
  TForm1 = class(TForm)
    Label1: TLabel;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1    : TForm1;
  hMapObj  : THandle;   // mapped file handle
  PMapView : PLongInt;  // address of region
implementation
{$R *.DFM}
//
// When form is created
//
procedure TForm1.FormCreate(Sender: TObject);
begin
//
// Open mapped file
//
  hMapObj := OpenFileMapping(FILE_MAP_READ, False, 'shared_memory');
  if hMapObj = 0 Then
  begin
   ShowMessage('MapFileError');
  // Error, exit
  end;
//
// Allocate memory
//
  GetMem(PMapView, SizeOf(LongInt));
//
// Get address of the region
//
  PMapView := MapViewOfFile(hMapObj, FILE_MAP_READ, 0, 0, 0);
// Now PMapView allows us to get data from "file"
end;
//
// Timer handler
//
procedure TForm1.Timer1Timer(Sender: TObject);
begin
// Read data and show it
 Label1.Caption := IntToStr(Hi(PMapView^)) + ':' +
                   IntToStr(Lo(PMapView^));
end;
end.