Отображаемый в память файл – записать и считать информацию
unit MMapForm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
BtnWrite: TButton;
EditWrite: TEdit;
BtnRead: TButton;
EditRead: TEdit;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure BtnWriteClick(Sender: TObject);
procedure BtnReadClick(Sender: TObject);
private
hMapFile: THandle;
MapFilePointer: Pointer;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
hMapFile := CreateFileMapping(
$FFFFFFFF, // file handle ... or memory
nil, // security
Page_ReadWrite, // access rights
0, // high memory size
10000, // low memory size
'DdhDemoMappedFile'); // mapped file name
if hMapFile <> 0 then
MapFilePointer := MapViewOfFile(
hMapFile, // handle returned above
File_Map_All_Access, // access rights
0, 0, 0) // access the entire mapped file
else
ShowMessage('hMapFile = 0');
if MapFilePointer = nil then
ShowMessage('MapFilePointer = nil');
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
UnMapViewOfFile(MapFilePointer);
CloseHandle(hMapFile);
end;
procedure TForm1.BtnWriteClick(Sender: TObject);
begin
StrCopy(PChar(MapFilePointer),
PChar(EditWrite.Text));
end;
procedure TForm1.BtnReadClick(Sender: TObject);
var
S: string;
begin
S := PChar(MapFilePointer);
EditRead.Text := S;
end;
end.
|