//Autor: Wiesław Dudek
//

unit MainFrm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, ExtCtrls;

type
  TMainForm = class(TForm)
    Memo1: TMemo;
    BitBtn1: TBitBtn;
    Timer1: TTimer;
    procedure BitBtn1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Memo1Change(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    FHandleOfMap : THandle ;
    FSharedData : PChar ;
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;
const FNazwa : PChar='DaneDaneDaneDaneDane' ;

implementation

{$R *.DFM}

procedure TMainForm.BitBtn1Click(Sender: TObject);
begin
  Close ;
end;

procedure TMainForm.FormShow(Sender: TObject);
begin
  FHandleOfMap:=OpenFileMapping(FILE_MAP_ALL_ACCESS, false, FNazwa) ;
  if FHandleOfMap=0 then
  begin
Showmessage('wwww') ;
    FHandleOfMap:=CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE,0, 1000,FNazwa) ;
    if FHandleOfMap=0 then
      raise Exception.Create('Nie udało się utworzenie ani otworzenie obiektu odwzorowania '#13'Błąd: '+SysErrorMessage(GetLastError)) ;
  end ;
  try
    FSharedData:=MapViewOfFile(FHandleOfMap,FILE_MAP_ALL_ACCESS,0,0,1000);
    if FSharedData=nil then
      raise Exception.Create('Nie udało się utworzenie widoku odwzorowania '#13'Błąd: '+SysErrorMessage(GetLastError)) ;
  finally
    CloseHandle(FHandleOfMap) ;
  end ;
end;

procedure TMainForm.Memo1Change(Sender: TObject);
begin
  StrPCopy(FSharedData,Memo1.Text) ;
end;

procedure TMainForm.Timer1Timer(Sender: TObject);
begin
  Memo1.Text:=FSharedData ;
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
  UnMapViewOfFile(FSharedData) ;
end;

end.
